OLD | NEW |
| (Empty) |
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 "core/platform/graphics/FloatSize.h" | |
31 #include "core/platform/graphics/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 FloatPoint { | |
57 public: | |
58 FloatPoint() : m_x(0), m_y(0) { } | |
59 FloatPoint(float x, float y) : m_x(x), m_y(y) { } | |
60 FloatPoint(const IntPoint&); | |
61 FloatPoint(const LayoutPoint&); | |
62 explicit FloatPoint(const FloatSize& size) : m_x(size.width()), m_y(size.hei
ght()) { } | |
63 | |
64 static FloatPoint zero() { return FloatPoint(); } | |
65 | |
66 static FloatPoint narrowPrecision(double x, double y); | |
67 | |
68 float x() const { return m_x; } | |
69 float y() const { return m_y; } | |
70 | |
71 void setX(float x) { m_x = x; } | |
72 void setY(float y) { m_y = y; } | |
73 void set(float x, float y) | |
74 { | |
75 m_x = x; | |
76 m_y = y; | |
77 } | |
78 void move(float dx, float dy) | |
79 { | |
80 m_x += dx; | |
81 m_y += dy; | |
82 } | |
83 void move(const IntSize& a) | |
84 { | |
85 m_x += a.width(); | |
86 m_y += a.height(); | |
87 } | |
88 void move(const LayoutSize&); | |
89 void move(const FloatSize& a) | |
90 { | |
91 m_x += a.width(); | |
92 m_y += a.height(); | |
93 } | |
94 void moveBy(const IntPoint& a) | |
95 { | |
96 m_x += a.x(); | |
97 m_y += a.y(); | |
98 } | |
99 void moveBy(const LayoutPoint&); | |
100 void moveBy(const FloatPoint& a) | |
101 { | |
102 m_x += a.x(); | |
103 m_y += a.y(); | |
104 } | |
105 void scale(float sx, float sy) | |
106 { | |
107 m_x *= sx; | |
108 m_y *= sy; | |
109 } | |
110 | |
111 void normalize(); | |
112 | |
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; | |
120 float lengthSquared() const | |
121 { | |
122 return m_x * m_x + m_y * m_y; | |
123 } | |
124 | |
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 | |
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 FloatPoint matrixTransform(const TransformationMatrix&) const; | |
147 FloatPoint matrixTransform(const AffineTransform&) const; | |
148 | |
149 private: | |
150 float m_x, m_y; | |
151 }; | |
152 | |
153 | |
154 inline FloatPoint& operator+=(FloatPoint& a, const FloatSize& b) | |
155 { | |
156 a.move(b.width(), b.height()); | |
157 return a; | |
158 } | |
159 | |
160 inline FloatPoint& operator+=(FloatPoint& a, const FloatPoint& b) | |
161 { | |
162 a.move(b.x(), b.y()); | |
163 return a; | |
164 } | |
165 | |
166 inline FloatPoint& operator-=(FloatPoint& a, const FloatSize& b) | |
167 { | |
168 a.move(-b.width(), -b.height()); | |
169 return a; | |
170 } | |
171 | |
172 inline FloatPoint operator+(const FloatPoint& a, const FloatSize& b) | |
173 { | |
174 return FloatPoint(a.x() + b.width(), a.y() + b.height()); | |
175 } | |
176 | |
177 inline FloatPoint operator+(const FloatPoint& a, const FloatPoint& b) | |
178 { | |
179 return FloatPoint(a.x() + b.x(), a.y() + b.y()); | |
180 } | |
181 | |
182 inline FloatSize operator-(const FloatPoint& a, const FloatPoint& b) | |
183 { | |
184 return FloatSize(a.x() - b.x(), a.y() - b.y()); | |
185 } | |
186 | |
187 inline FloatPoint operator-(const FloatPoint& a, const FloatSize& b) | |
188 { | |
189 return FloatPoint(a.x() - b.width(), a.y() - b.height()); | |
190 } | |
191 | |
192 inline FloatPoint operator-(const FloatPoint& a) | |
193 { | |
194 return FloatPoint(-a.x(), -a.y()); | |
195 } | |
196 | |
197 inline bool operator==(const FloatPoint& a, const FloatPoint& b) | |
198 { | |
199 return a.x() == b.x() && a.y() == b.y(); | |
200 } | |
201 | |
202 inline bool operator!=(const FloatPoint& a, const FloatPoint& b) | |
203 { | |
204 return a.x() != b.x() || a.y() != b.y(); | |
205 } | |
206 | |
207 inline float operator*(const FloatPoint& a, const FloatPoint& b) | |
208 { | |
209 // dot product | |
210 return a.dot(b); | |
211 } | |
212 | |
213 inline IntPoint roundedIntPoint(const FloatPoint& p) | |
214 { | |
215 return IntPoint(clampToInteger(roundf(p.x())), clampToInteger(roundf(p.y()))
); | |
216 } | |
217 | |
218 inline IntPoint flooredIntPoint(const FloatPoint& p) | |
219 { | |
220 return IntPoint(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y()))
); | |
221 } | |
222 | |
223 inline IntPoint ceiledIntPoint(const FloatPoint& p) | |
224 { | |
225 return IntPoint(clampToInteger(ceilf(p.x())), clampToInteger(ceilf(p.y()))); | |
226 } | |
227 | |
228 inline IntSize flooredIntSize(const FloatPoint& p) | |
229 { | |
230 return IntSize(clampToInteger(floorf(p.x())), clampToInteger(floorf(p.y())))
; | |
231 } | |
232 | |
233 inline FloatSize toFloatSize(const FloatPoint& a) | |
234 { | |
235 return FloatSize(a.x(), a.y()); | |
236 } | |
237 | |
238 float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c); | |
239 | |
240 // Find point where lines through the two pairs of points intersect. Returns fal
se if the lines don't intersect. | |
241 bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoi
nt& d1, const FloatPoint& d2, FloatPoint& intersection); | |
242 | |
243 } | |
244 | |
245 #endif | |
OLD | NEW |