Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / geometry / FloatPoint.h
1 /*
2  * Copyright (C) 2004, 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2005 Nokia.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef FloatPoint_h
28 #define FloatPoint_h
29
30 #include "platform/geometry/FloatSize.h"
31 #include "platform/geometry/IntPoint.h"
32 #include "wtf/MathExtras.h"
33 #include <algorithm>
34
35 #if OS(MACOSX)
36 typedef struct CGPoint CGPoint;
37
38 #ifdef __OBJC__
39 #import <Foundation/Foundation.h>
40 #endif
41 #endif
42
43 struct SkPoint;
44
45 namespace WebCore {
46
47 class IntPoint;
48 class IntSize;
49 class LayoutPoint;
50 class LayoutSize;
51
52 class PLATFORM_EXPORT FloatPoint {
53 public:
54     FloatPoint() : m_x(0), m_y(0) { }
55     FloatPoint(float x, float y) : m_x(x), m_y(y) { }
56     FloatPoint(const IntPoint&);
57     FloatPoint(const LayoutPoint&);
58     explicit FloatPoint(const FloatSize& size) : m_x(size.width()), m_y(size.height()) { }
59
60     static FloatPoint zero() { return FloatPoint(); }
61
62     static FloatPoint narrowPrecision(double x, double y);
63
64     float x() const { return m_x; }
65     float y() const { return m_y; }
66
67     void setX(float x) { m_x = x; }
68     void setY(float y) { m_y = y; }
69     void set(float x, float y)
70     {
71         m_x = x;
72         m_y = y;
73     }
74     void move(float dx, float dy)
75     {
76         m_x += dx;
77         m_y += dy;
78     }
79     void move(const IntSize& a)
80     {
81         m_x += a.width();
82         m_y += a.height();
83     }
84     void move(const LayoutSize&);
85     void move(const FloatSize& a)
86     {
87         m_x += a.width();
88         m_y += a.height();
89     }
90     void moveBy(const IntPoint& a)
91     {
92         m_x += a.x();
93         m_y += a.y();
94     }
95     void moveBy(const LayoutPoint&);
96     void moveBy(const FloatPoint& a)
97     {
98         m_x += a.x();
99         m_y += a.y();
100     }
101     void scale(float sx, float sy)
102     {
103         m_x *= sx;
104         m_y *= sy;
105     }
106
107     void normalize();
108
109     float dot(const FloatPoint& a) const
110     {
111         return m_x * a.x() + m_y * a.y();
112     }
113
114     float slopeAngleRadians() const;
115     float length() const;
116     float lengthSquared() const
117     {
118         return m_x * m_x + m_y * m_y;
119     }
120
121     FloatPoint expandedTo(const FloatPoint& other) const
122     {
123         return FloatPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y));
124     }
125
126     FloatPoint shrunkTo(const FloatPoint& other) const
127     {
128         return FloatPoint(std::min(m_x, other.m_x), std::min(m_y, other.m_y));
129     }
130
131     FloatPoint transposedPoint() const
132     {
133         return FloatPoint(m_y, m_x);
134     }
135
136 #if OS(MACOSX)
137     FloatPoint(const CGPoint&);
138     operator CGPoint() const;
139 #if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
140     FloatPoint(const NSPoint&);
141     operator NSPoint() const;
142 #endif
143 #endif
144
145     SkPoint data() const;
146
147 private:
148     float m_x, m_y;
149 };
150
151
152 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b)
153 {
154     a.move(b.width(), b.height());
155     return a;
156 }
157
158 inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b)
159 {
160     a.move(b.x(), b.y());
161     return a;
162 }
163
164 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b)
165 {
166     a.move(-b.width(), -b.height());
167     return a;
168 }
169
170 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b)
171 {
172     return FloatPoint(a.x() + b.width(), a.y() + b.height());
173 }
174
175 inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b)
176 {
177     return FloatPoint(a.x() + b.x(), a.y() + b.y());
178 }
179
180 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b)
181 {
182     return FloatSize(a.x() - b.x(), a.y() - b.y());
183 }
184
185 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b)
186 {
187     return FloatPoint(a.x() - b.width(), a.y() - b.height());
188 }
189
190 inline FloatPoint operator-(const FloatPoint& a)
191 {
192     return FloatPoint(-a.x(), -a.y());
193 }
194
195 inline bool operator==(const FloatPoint& a, const FloatPoint& b)
196 {
197     return a.x() == b.x() && a.y() == b.y();
198 }
199
200 inline bool operator!=(const FloatPoint& a, const FloatPoint& b)
201 {
202     return a.x() != b.x() || a.y() != b.y();
203 }
204
205 inline float operator*(const FloatPoint& a, const FloatPoint& b)
206 {
207     // dot product
208     return a.dot(b);
209 }
210
211 inline IntPoint roundedIntPoint(const FloatPoint& p)
212 {
213     return IntPoint(clampToInteger(roundf(p.x())), clampToInteger(roundf(p.y())));
214 }
215
216 inline IntPoint flooredIntPoint(const FloatPoint& p)
217 {
218     return IntPoint(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y())));
219 }
220
221 inline IntPoint ceiledIntPoint(const FloatPoint& p)
222 {
223     return IntPoint(clampToInteger(ceilf(p.x())), clampToInteger(ceilf(p.y())));
224 }
225
226 inline IntSize flooredIntSize(const FloatPoint& p)
227 {
228     return IntSize(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y())));
229 }
230
231 inline FloatSize toFloatSize(const FloatPoint& a)
232 {
233     return FloatSize(a.x(), a.y());
234 }
235
236 PLATFORM_EXPORT float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c);
237
238 // Find point where lines through the two pairs of points intersect. Returns false if the lines don't intersect.
239 PLATFORM_EXPORT bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection);
240
241 }
242
243 #endif