2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkPoint_DEFINED
9 #define SkPoint_DEFINED
16 SkIPoint holds two 32 bit integer coordinates
21 static SkIPoint Make(int32_t x, int32_t y) {
27 int32_t x() const { return fX; }
28 int32_t y() const { return fY; }
29 void setX(int32_t x) { fX = x; }
30 void setY(int32_t y) { fY = y; }
33 * Returns true iff fX and fY are both zero.
35 bool isZero() const { return (fX | fY) == 0; }
38 * Set both fX and fY to zero. Same as set(0, 0)
40 void setZero() { fX = fY = 0; }
42 /** Set the x and y values of the point. */
43 void set(int32_t x, int32_t y) { fX = x; fY = y; }
45 /** Rotate the point clockwise, writing the new point into dst
46 It is legal for dst == this
48 void rotateCW(SkIPoint* dst) const;
50 /** Rotate the point clockwise, writing the new point back into the point
53 void rotateCW() { this->rotateCW(this); }
55 /** Rotate the point counter-clockwise, writing the new point into dst.
56 It is legal for dst == this
58 void rotateCCW(SkIPoint* dst) const;
60 /** Rotate the point counter-clockwise, writing the new point back into
63 void rotateCCW() { this->rotateCCW(this); }
65 /** Negate the X and Y coordinates of the point.
67 void negate() { fX = -fX; fY = -fY; }
69 /** Return a new point whose X and Y coordinates are the negative of the
72 SkIPoint operator-() const {
79 /** Add v's coordinates to this point's */
80 void operator+=(const SkIPoint& v) {
85 /** Subtract v's coordinates from this point's */
86 void operator-=(const SkIPoint& v) {
91 /** Returns true if the point's coordinates equal (x,y) */
92 bool equals(int32_t x, int32_t y) const {
93 return fX == x && fY == y;
96 friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
97 return a.fX == b.fX && a.fY == b.fY;
100 friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
101 return a.fX != b.fX || a.fY != b.fY;
104 /** Returns a new point whose coordinates are the difference between
107 friend SkIPoint operator-(const SkIPoint& a, const SkIPoint& b) {
109 v.set(a.fX - b.fX, a.fY - b.fY);
113 /** Returns a new point whose coordinates are the sum of a and b (a + b)
115 friend SkIPoint operator+(const SkIPoint& a, const SkIPoint& b) {
117 v.set(a.fX + b.fX, a.fY + b.fY);
121 /** Returns the dot product of a and b, treating them as 2D vectors
123 static int32_t DotProduct(const SkIPoint& a, const SkIPoint& b) {
124 return a.fX * b.fX + a.fY * b.fY;
127 /** Returns the cross product of a and b, treating them as 2D vectors
129 static int32_t CrossProduct(const SkIPoint& a, const SkIPoint& b) {
130 return a.fX * b.fY - a.fY * b.fX;
134 struct SK_API SkPoint {
137 static SkPoint Make(SkScalar x, SkScalar y) {
143 SkScalar x() const { return fX; }
144 SkScalar y() const { return fY; }
147 * Returns true iff fX and fY are both zero.
149 bool isZero() const { return (0 == fX) & (0 == fY); }
151 /** Set the point's X and Y coordinates */
152 void set(SkScalar x, SkScalar y) { fX = x; fY = y; }
154 /** Set the point's X and Y coordinates by automatically promoting (x,y) to
157 void iset(int32_t x, int32_t y) {
158 fX = SkIntToScalar(x);
159 fY = SkIntToScalar(y);
162 /** Set the point's X and Y coordinates by automatically promoting p's
163 coordinates to SkScalar values.
165 void iset(const SkIPoint& p) {
166 fX = SkIntToScalar(p.fX);
167 fY = SkIntToScalar(p.fY);
170 void setAbs(const SkPoint& pt) {
171 fX = SkScalarAbs(pt.fX);
172 fY = SkScalarAbs(pt.fY);
175 // counter-clockwise fan
176 void setIRectFan(int l, int t, int r, int b) {
178 v[0].set(SkIntToScalar(l), SkIntToScalar(t));
179 v[1].set(SkIntToScalar(l), SkIntToScalar(b));
180 v[2].set(SkIntToScalar(r), SkIntToScalar(b));
181 v[3].set(SkIntToScalar(r), SkIntToScalar(t));
183 void setIRectFan(int l, int t, int r, int b, size_t stride);
185 // counter-clockwise fan
186 void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
193 void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b, size_t stride);
195 static void Offset(SkPoint points[], int count, const SkPoint& offset) {
196 Offset(points, count, offset.fX, offset.fY);
199 static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) {
200 for (int i = 0; i < count; ++i) {
201 points[i].offset(dx, dy);
205 void offset(SkScalar dx, SkScalar dy) {
210 /** Return the euclidian distance from (0,0) to the point
212 SkScalar length() const { return SkPoint::Length(fX, fY); }
213 SkScalar distanceToOrigin() const { return this->length(); }
216 * Return true if the computed length of the vector is >= the internal
217 * tolerance (used to avoid dividing by tiny values).
219 static bool CanNormalize(SkScalar dx, SkScalar dy) {
220 // Simple enough (and performance critical sometimes) so we inline it.
221 return (dx*dx + dy*dy) > (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
224 bool canNormalize() const {
225 return CanNormalize(fX, fY);
228 /** Set the point (vector) to be unit-length in the same direction as it
229 already points. If the point has a degenerate length (i.e. nearly 0)
230 then return false and do nothing; otherwise return true.
234 /** Set the point (vector) to be unit-length in the same direction as the
235 x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0)
236 then return false and do nothing, otherwise return true.
238 bool setNormalize(SkScalar x, SkScalar y);
240 /** Scale the point (vector) to have the specified length, and return that
241 length. If the original length is degenerately small (nearly zero),
242 do nothing and return false, otherwise return true.
244 bool setLength(SkScalar length);
246 /** Set the point (vector) to have the specified length in the same
247 direction as (x,y). If the vector (x,y) has a degenerate length
248 (i.e. nearly 0) then return false and do nothing, otherwise return true.
250 bool setLength(SkScalar x, SkScalar y, SkScalar length);
252 /** Same as setLength, but favoring speed over accuracy.
254 bool setLengthFast(SkScalar length);
256 /** Same as setLength, but favoring speed over accuracy.
258 bool setLengthFast(SkScalar x, SkScalar y, SkScalar length);
260 /** Scale the point's coordinates by scale, writing the answer into dst.
261 It is legal for dst == this.
263 void scale(SkScalar scale, SkPoint* dst) const;
265 /** Scale the point's coordinates by scale, writing the answer back into
268 void scale(SkScalar value) { this->scale(value, this); }
270 /** Rotate the point clockwise by 90 degrees, writing the answer into dst.
271 It is legal for dst == this.
273 void rotateCW(SkPoint* dst) const;
275 /** Rotate the point clockwise by 90 degrees, writing the answer back into
278 void rotateCW() { this->rotateCW(this); }
280 /** Rotate the point counter-clockwise by 90 degrees, writing the answer
281 into dst. It is legal for dst == this.
283 void rotateCCW(SkPoint* dst) const;
285 /** Rotate the point counter-clockwise by 90 degrees, writing the answer
288 void rotateCCW() { this->rotateCCW(this); }
290 /** Negate the point's coordinates
297 /** Returns a new point whose coordinates are the negative of the point's
299 SkPoint operator-() const {
306 /** Add v's coordinates to the point's
308 void operator+=(const SkPoint& v) {
313 /** Subtract v's coordinates from the point's
315 void operator-=(const SkPoint& v) {
321 * Returns true if both X and Y are finite (not infinity or NaN)
323 bool isFinite() const {
328 // accum is either NaN or it is finite (zero).
329 SkASSERT(0 == accum || !(accum == accum));
331 // value==value will be true iff value is not NaN
332 // TODO: is it faster to say !accum or accum==accum?
333 return accum == accum;
337 * Returns true if the point's coordinates equal (x,y)
339 bool equals(SkScalar x, SkScalar y) const {
340 return fX == x && fY == y;
343 friend bool operator==(const SkPoint& a, const SkPoint& b) {
344 return a.fX == b.fX && a.fY == b.fY;
347 friend bool operator!=(const SkPoint& a, const SkPoint& b) {
348 return a.fX != b.fX || a.fY != b.fY;
351 /** Return true if this point and the given point are far enough apart
352 such that a vector between them would be non-degenerate.
354 WARNING: Unlike the explicit tolerance version,
355 this method does not use componentwise comparison. Instead, it
356 uses a comparison designed to match judgments elsewhere regarding
357 degeneracy ("points A and B are so close that the vector between them
358 is essentially zero").
360 bool equalsWithinTolerance(const SkPoint& p) const {
361 return !CanNormalize(fX - p.fX, fY - p.fY);
364 /** WARNING: There is no guarantee that the result will reflect judgments
365 elsewhere regarding degeneracy ("points A and B are so close that the
366 vector between them is essentially zero").
368 bool equalsWithinTolerance(const SkPoint& p, SkScalar tol) const {
369 return SkScalarNearlyZero(fX - p.fX, tol)
370 && SkScalarNearlyZero(fY - p.fY, tol);
373 /** Returns a new point whose coordinates are the difference between
376 friend SkPoint operator-(const SkPoint& a, const SkPoint& b) {
378 v.set(a.fX - b.fX, a.fY - b.fY);
382 /** Returns a new point whose coordinates are the sum of a's and b's (a + b)
384 friend SkPoint operator+(const SkPoint& a, const SkPoint& b) {
386 v.set(a.fX + b.fX, a.fY + b.fY);
390 /** Returns the euclidian distance from (0,0) to (x,y)
392 static SkScalar Length(SkScalar x, SkScalar y);
394 /** Normalize pt, returning its previous length. If the prev length is too
395 small (degenerate), return 0 and leave pt unchanged. This uses the same
396 tolerance as CanNormalize.
398 Note that this method may be significantly more expensive than
399 the non-static normalize(), because it has to return the previous length
400 of the point. If you don't need the previous length, call the
401 non-static normalize() method instead.
403 static SkScalar Normalize(SkPoint* pt);
405 /** Returns the euclidian distance between a and b
407 static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
408 return Length(a.fX - b.fX, a.fY - b.fY);
411 /** Returns the dot product of a and b, treating them as 2D vectors
413 static SkScalar DotProduct(const SkPoint& a, const SkPoint& b) {
414 return a.fX * b.fX + a.fY * b.fY;
417 /** Returns the cross product of a and b, treating them as 2D vectors
419 static SkScalar CrossProduct(const SkPoint& a, const SkPoint& b) {
420 return a.fX * b.fY - a.fY * b.fX;
423 SkScalar cross(const SkPoint& vec) const {
424 return CrossProduct(*this, vec);
427 SkScalar dot(const SkPoint& vec) const {
428 return DotProduct(*this, vec);
431 SkScalar lengthSqd() const {
432 return DotProduct(*this, *this);
435 SkScalar distanceToSqd(const SkPoint& pt) const {
436 SkScalar dx = fX - pt.fX;
437 SkScalar dy = fY - pt.fY;
438 return dx * dx + dy * dy;
442 * The side of a point relative to a line. If the line is from a to b then
443 * the values are consistent with the sign of (b-a) cross (pt-a)
452 * Returns the squared distance to the infinite line between two pts. Also
453 * optionally returns the side of the line that the pt falls on (looking
454 * along line from a to b)
456 SkScalar distanceToLineBetweenSqd(const SkPoint& a,
458 Side* side = NULL) const;
461 * Returns the distance to the infinite line between two pts. Also
462 * optionally returns the side of the line that the pt falls on (looking
463 * along the line from a to b)
465 SkScalar distanceToLineBetween(const SkPoint& a,
467 Side* side = NULL) const {
468 return SkScalarSqrt(this->distanceToLineBetweenSqd(a, b, side));
472 * Returns the squared distance to the line segment between pts a and b
474 SkScalar distanceToLineSegmentBetweenSqd(const SkPoint& a,
475 const SkPoint& b) const;
478 * Returns the distance to the line segment between pts a and b.
480 SkScalar distanceToLineSegmentBetween(const SkPoint& a,
481 const SkPoint& b) const {
482 return SkScalarSqrt(this->distanceToLineSegmentBetweenSqd(a, b));
486 * Make this vector be orthogonal to vec. Looking down vec the
487 * new vector will point in direction indicated by side (which
488 * must be kLeft_Side or kRight_Side).
490 void setOrthog(const SkPoint& vec, Side side = kLeft_Side) {
492 SkScalar tmp = vec.fX;
493 if (kRight_Side == side) {
497 SkASSERT(kLeft_Side == side);
504 * cast-safe way to treat the point as an array of (2) SkScalars.
506 const SkScalar* asScalars() const { return &fX; }
509 typedef SkPoint SkVector;