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 NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES
39 typedef struct CGPoint NSPoint;
40 #else
41 typedef struct _NSPoint NSPoint;
42 #endif
43 #endif
44
45 struct SkPoint;
46
47 namespace WebCore {
48
49 class AffineTransform;
50 class TransformationMatrix;
51 class IntPoint;
52 class IntSize;
53 class LayoutPoint;
54 class LayoutSize;
55
56 class PLATFORM_EXPORT FloatPoint {
57 public:
FloatPoint()58 FloatPoint() : m_x(0), m_y(0) { }
FloatPoint(float x,float y)59 FloatPoint(float x, float y) : m_x(x), m_y(y) { }
60 FloatPoint(const IntPoint&);
61 FloatPoint(const LayoutPoint&);
FloatPoint(const FloatSize & size)62 explicit FloatPoint(const FloatSize& size) : m_x(size.width()), m_y(size.height()) { }
63
zero()64 static FloatPoint zero() { return FloatPoint(); }
65
66 static FloatPoint narrowPrecision(double x, double y);
67
x()68 float x() const { return m_x; }
y()69 float y() const { return m_y; }
70
setX(float x)71 void setX(float x) { m_x = x; }
setY(float y)72 void setY(float y) { m_y = y; }
set(float x,float y)73 void set(float x, float y)
74 {
75 m_x = x;
76 m_y = y;
77 }
move(float dx,float dy)78 void move(float dx, float dy)
79 {
80 m_x += dx;
81 m_y += dy;
82 }
move(const IntSize & a)83 void move(const IntSize& a)
84 {
85 m_x += a.width();
86 m_y += a.height();
87 }
88 void move(const LayoutSize&);
move(const FloatSize & a)89 void move(const FloatSize& a)
90 {
91 m_x += a.width();
92 m_y += a.height();
93 }
moveBy(const IntPoint & a)94 void moveBy(const IntPoint& a)
95 {
96 m_x += a.x();
97 m_y += a.y();
98 }
99 void moveBy(const LayoutPoint&);
moveBy(const FloatPoint & a)100 void moveBy(const FloatPoint& a)
101 {
102 m_x += a.x();
103 m_y += a.y();
104 }
scale(float sx,float sy)105 void scale(float sx, float sy)
106 {
107 m_x *= sx;
108 m_y *= sy;
109 }
110
111 void normalize();
112
dot(const FloatPoint & a)113 float dot(const FloatPoint& a) const
114 {
115 return m_x * a.x() + m_y * a.y();
116 }
117
118 float slopeAngleRadians() const;
119 float length() const;
lengthSquared()120 float lengthSquared() const
121 {
122 return m_x * m_x + m_y * m_y;
123 }
124
expandedTo(const FloatPoint & other)125 FloatPoint expandedTo(const FloatPoint& other) const
126 {
127 return FloatPoint(std::max(m_x, other.m_x), std::max(m_y, other.m_y));
128 }
129
transposedPoint()130 FloatPoint transposedPoint() const
131 {
132 return FloatPoint(m_y, m_x);
133 }
134
135 #if OS(MACOSX)
136 FloatPoint(const CGPoint&);
137 operator CGPoint() const;
138 #if !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
139 FloatPoint(const NSPoint&);
140 operator NSPoint() const;
141 #endif
142 #endif
143
144 operator SkPoint() const;
145
146 // FIXME: These are only used by SVGPoint and should probably move there.
147 // http://crbug.com/302829
148 FloatPoint matrixTransform(const TransformationMatrix&) const;
149 FloatPoint matrixTransform(const AffineTransform&) const;
150
151 private:
152 float m_x, m_y;
153 };
154
155
156 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b)
157 {
158 a.move(b.width(), b.height());
159 return a;
160 }
161
162 inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b)
163 {
164 a.move(b.x(), b.y());
165 return a;
166 }
167
168 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b)
169 {
170 a.move(-b.width(), -b.height());
171 return a;
172 }
173
174 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b)
175 {
176 return FloatPoint(a.x() + b.width(), a.y() + b.height());
177 }
178
179 inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b)
180 {
181 return FloatPoint(a.x() + b.x(), a.y() + b.y());
182 }
183
184 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b)
185 {
186 return FloatSize(a.x() - b.x(), a.y() - b.y());
187 }
188
189 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b)
190 {
191 return FloatPoint(a.x() - b.width(), a.y() - b.height());
192 }
193
194 inline FloatPoint operator-(const FloatPoint& a)
195 {
196 return FloatPoint(-a.x(), -a.y());
197 }
198
199 inline bool operator==(const FloatPoint& a, const FloatPoint& b)
200 {
201 return a.x() == b.x() && a.y() == b.y();
202 }
203
204 inline bool operator!=(const FloatPoint& a, const FloatPoint& b)
205 {
206 return a.x() != b.x() || a.y() != b.y();
207 }
208
209 inline float operator*(const FloatPoint& a, const FloatPoint& b)
210 {
211 // dot product
212 return a.dot(b);
213 }
214
roundedIntPoint(const FloatPoint & p)215 inline IntPoint roundedIntPoint(const FloatPoint& p)
216 {
217 return IntPoint(clampToInteger(roundf(p.x())), clampToInteger(roundf(p.y())));
218 }
219
flooredIntPoint(const FloatPoint & p)220 inline IntPoint flooredIntPoint(const FloatPoint& p)
221 {
222 return IntPoint(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y())));
223 }
224
ceiledIntPoint(const FloatPoint & p)225 inline IntPoint ceiledIntPoint(const FloatPoint& p)
226 {
227 return IntPoint(clampToInteger(ceilf(p.x())), clampToInteger(ceilf(p.y())));
228 }
229
flooredIntSize(const FloatPoint & p)230 inline IntSize flooredIntSize(const FloatPoint& p)
231 {
232 return IntSize(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y())));
233 }
234
toFloatSize(const FloatPoint & a)235 inline FloatSize toFloatSize(const FloatPoint& a)
236 {
237 return FloatSize(a.x(), a.y());
238 }
239
240 PLATFORM_EXPORT float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c);
241
242 // Find point where lines through the two pairs of points intersect. Returns false if the lines don't intersect.
243 PLATFORM_EXPORT bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection);
244
245 }
246
247 #endif
248