blob: a6a8e730c26bf1bc9ac2aced5255f748bdf08bb7 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkPoint_DEFINED
9#define SkPoint_DEFINED
10
tomhudson@google.comc12e1b12011-09-27 18:03:23 +000011#include "SkMath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkScalar.h"
13
14/** \struct SkIPoint
15
16 SkIPoint holds two 32 bit integer coordinates
17*/
18struct SkIPoint {
19 int32_t fX, fY;
reed@google.com6f8f2922011-03-04 22:27:10 +000020
reed@android.comac753092010-01-28 21:34:33 +000021 static SkIPoint Make(int32_t x, int32_t y) {
22 SkIPoint pt;
23 pt.set(x, y);
24 return pt;
25 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
reed@google.com6f8f2922011-03-04 22:27:10 +000027 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; }
31
32 /**
33 * Returns true iff fX and fY are both zero.
34 */
35 bool isZero() const { return (fX | fY) == 0; }
36
37 /**
38 * Set both fX and fY to zero. Same as set(0, 0)
39 */
40 void setZero() { fX = fY = 0; }
41
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 /** Set the x and y values of the point. */
43 void set(int32_t x, int32_t y) { fX = x; fY = y; }
44
45 /** Rotate the point clockwise, writing the new point into dst
46 It is legal for dst == this
47 */
48 void rotateCW(SkIPoint* dst) const;
49
50 /** Rotate the point clockwise, writing the new point back into the point
51 */
52
53 void rotateCW() { this->rotateCW(this); }
54
55 /** Rotate the point counter-clockwise, writing the new point into dst.
56 It is legal for dst == this
57 */
58 void rotateCCW(SkIPoint* dst) const;
59
60 /** Rotate the point counter-clockwise, writing the new point back into
61 the point
62 */
63 void rotateCCW() { this->rotateCCW(this); }
reed@google.com6f8f2922011-03-04 22:27:10 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 /** Negate the X and Y coordinates of the point.
66 */
67 void negate() { fX = -fX; fY = -fY; }
reed@google.com6f8f2922011-03-04 22:27:10 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 /** Return a new point whose X and Y coordinates are the negative of the
70 original point's
71 */
72 SkIPoint operator-() const {
73 SkIPoint neg;
74 neg.fX = -fX;
75 neg.fY = -fY;
76 return neg;
77 }
78
79 /** Add v's coordinates to this point's */
80 void operator+=(const SkIPoint& v) {
81 fX += v.fX;
82 fY += v.fY;
83 }
reed@google.com6f8f2922011-03-04 22:27:10 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 /** Subtract v's coordinates from this point's */
86 void operator-=(const SkIPoint& v) {
87 fX -= v.fX;
88 fY -= v.fY;
89 }
90
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;
94 }
95
96 friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
97 return a.fX == b.fX && a.fY == b.fY;
98 }
reed@google.com6f8f2922011-03-04 22:27:10 +000099
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
101 return a.fX != b.fX || a.fY != b.fY;
102 }
103
104 /** Returns a new point whose coordinates are the difference between
105 a and b (i.e. a - b)
106 */
107 friend SkIPoint operator-(const SkIPoint& a, const SkIPoint& b) {
108 SkIPoint v;
109 v.set(a.fX - b.fX, a.fY - b.fY);
110 return v;
111 }
112
113 /** Returns a new point whose coordinates are the sum of a and b (a + b)
114 */
115 friend SkIPoint operator+(const SkIPoint& a, const SkIPoint& b) {
116 SkIPoint v;
117 v.set(a.fX + b.fX, a.fY + b.fY);
118 return v;
119 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 /** Returns the dot product of a and b, treating them as 2D vectors
122 */
123 static int32_t DotProduct(const SkIPoint& a, const SkIPoint& b) {
124 return a.fX * b.fX + a.fY * b.fY;
125 }
126
127 /** Returns the cross product of a and b, treating them as 2D vectors
128 */
129 static int32_t CrossProduct(const SkIPoint& a, const SkIPoint& b) {
130 return a.fX * b.fY - a.fY * b.fX;
131 }
132};
133
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000134struct SK_API SkPoint {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 SkScalar fX, fY;
136
reed@android.comac753092010-01-28 21:34:33 +0000137 static SkPoint Make(SkScalar x, SkScalar y) {
138 SkPoint pt;
139 pt.set(x, y);
140 return pt;
141 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000142
mike@reedtribe.orgb7d956d2011-03-20 20:19:16 +0000143 SkScalar x() const { return fX; }
144 SkScalar y() const { return fY; }
145
mike@reedtribe.org398b1bc2012-05-29 01:40:15 +0000146 /**
147 * Returns true iff fX and fY are both zero.
148 */
149 bool isZero() const { return (0 == fX) & (0 == fY); }
150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 /** Set the point's X and Y coordinates */
152 void set(SkScalar x, SkScalar y) { fX = x; fY = y; }
reed@google.com6f8f2922011-03-04 22:27:10 +0000153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 /** Set the point's X and Y coordinates by automatically promoting (x,y) to
155 SkScalar values.
156 */
157 void iset(int32_t x, int32_t y) {
158 fX = SkIntToScalar(x);
159 fY = SkIntToScalar(y);
160 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000161
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 /** Set the point's X and Y coordinates by automatically promoting p's
163 coordinates to SkScalar values.
164 */
165 void iset(const SkIPoint& p) {
166 fX = SkIntToScalar(p.fX);
167 fY = SkIntToScalar(p.fY);
168 }
169
reed@google.com7744c202011-05-06 19:26:26 +0000170 void setAbs(const SkPoint& pt) {
171 fX = SkScalarAbs(pt.fX);
172 fY = SkScalarAbs(pt.fY);
173 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000174
reed@google.com7744c202011-05-06 19:26:26 +0000175 // counter-clockwise fan
176 void setIRectFan(int l, int t, int r, int b) {
177 SkPoint* v = this;
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));
182 }
183 void setIRectFan(int l, int t, int r, int b, size_t stride);
184
185 // counter-clockwise fan
186 void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
187 SkPoint* v = this;
188 v[0].set(l, t);
189 v[1].set(l, b);
190 v[2].set(r, b);
191 v[3].set(r, t);
192 }
193 void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b, size_t stride);
194
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000195 static void Offset(SkPoint points[], int count, const SkPoint& offset) {
196 Offset(points, count, offset.fX, offset.fY);
197 }
198
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);
202 }
203 }
204
reed@google.com7744c202011-05-06 19:26:26 +0000205 void offset(SkScalar dx, SkScalar dy) {
206 fX += dx;
207 fY += dy;
208 }
209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 /** Return the euclidian distance from (0,0) to the point
211 */
212 SkScalar length() const { return SkPoint::Length(fX, fY); }
reed@google.com7744c202011-05-06 19:26:26 +0000213 SkScalar distanceToOrigin() const { return this->length(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214
reed@google.com55b5f4b2011-09-07 12:23:41 +0000215 /**
216 * Return true if the computed length of the vector is >= the internal
217 * tolerance (used to avoid dividing by tiny values).
218 */
reed@google.come3987da2012-05-07 13:51:05 +0000219 static bool CanNormalize(SkScalar dx, SkScalar dy)
220#ifdef SK_SCALAR_IS_FLOAT
221 // Simple enough (and performance critical sometimes) so we inline it.
222 { return (dx*dx + dy*dy) > (SK_ScalarNearlyZero * SK_ScalarNearlyZero); }
223#else
224 ;
225#endif
reed@google.com55b5f4b2011-09-07 12:23:41 +0000226
227 bool canNormalize() const {
228 return CanNormalize(fX, fY);
229 }
230
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 /** Set the point (vector) to be unit-length in the same direction as it
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000232 already points. If the point has a degenerate length (i.e. nearly 0)
233 then return false and do nothing; otherwise return true.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 */
235 bool normalize();
reed@google.com6f8f2922011-03-04 22:27:10 +0000236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 /** Set the point (vector) to be unit-length in the same direction as the
238 x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0)
239 then return false and do nothing, otherwise return true.
240 */
241 bool setNormalize(SkScalar x, SkScalar y);
reed@google.com6f8f2922011-03-04 22:27:10 +0000242
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 /** Scale the point (vector) to have the specified length, and return that
244 length. If the original length is degenerately small (nearly zero),
245 do nothing and return false, otherwise return true.
246 */
247 bool setLength(SkScalar length);
reed@google.com6f8f2922011-03-04 22:27:10 +0000248
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 /** Set the point (vector) to have the specified length in the same
250 direction as (x,y). If the vector (x,y) has a degenerate length
251 (i.e. nearly 0) then return false and do nothing, otherwise return true.
252 */
253 bool setLength(SkScalar x, SkScalar y, SkScalar length);
254
255 /** Scale the point's coordinates by scale, writing the answer into dst.
256 It is legal for dst == this.
257 */
258 void scale(SkScalar scale, SkPoint* dst) const;
reed@google.com6f8f2922011-03-04 22:27:10 +0000259
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 /** Scale the point's coordinates by scale, writing the answer back into
261 the point.
262 */
reed@android.comfc25abd2009-01-15 14:38:33 +0000263 void scale(SkScalar value) { this->scale(value, this); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264
265 /** Rotate the point clockwise by 90 degrees, writing the answer into dst.
266 It is legal for dst == this.
267 */
268 void rotateCW(SkPoint* dst) const;
reed@google.com6f8f2922011-03-04 22:27:10 +0000269
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 /** Rotate the point clockwise by 90 degrees, writing the answer back into
271 the point.
272 */
273 void rotateCW() { this->rotateCW(this); }
reed@google.com6f8f2922011-03-04 22:27:10 +0000274
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 /** Rotate the point counter-clockwise by 90 degrees, writing the answer
276 into dst. It is legal for dst == this.
277 */
278 void rotateCCW(SkPoint* dst) const;
reed@google.com6f8f2922011-03-04 22:27:10 +0000279
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 /** Rotate the point counter-clockwise by 90 degrees, writing the answer
281 back into the point.
282 */
283 void rotateCCW() { this->rotateCCW(this); }
reed@google.com6f8f2922011-03-04 22:27:10 +0000284
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 /** Negate the point's coordinates
286 */
287 void negate() {
288 fX = -fX;
289 fY = -fY;
290 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000291
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 /** Returns a new point whose coordinates are the negative of the point's
293 */
294 SkPoint operator-() const {
295 SkPoint neg;
296 neg.fX = -fX;
297 neg.fY = -fY;
298 return neg;
299 }
300
301 /** Add v's coordinates to the point's
302 */
303 void operator+=(const SkPoint& v) {
304 fX += v.fX;
305 fY += v.fY;
306 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 /** Subtract v's coordinates from the point's
309 */
310 void operator-=(const SkPoint& v) {
311 fX -= v.fX;
312 fY -= v.fY;
313 }
314
reed@google.com0bb18bb2012-07-26 15:20:36 +0000315 /**
316 * Returns true if both X and Y are finite (not infinity or NaN)
317 */
318 bool isFinite() const {
319#ifdef SK_SCALAR_IS_FLOAT
320 SkScalar accum = 0;
321 accum *= fX;
322 accum *= fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000323
reed@google.com0bb18bb2012-07-26 15:20:36 +0000324 // accum is either NaN or it is finite (zero).
325 SkASSERT(0 == accum || !(accum == accum));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000326
reed@google.com0bb18bb2012-07-26 15:20:36 +0000327 // value==value will be true iff value is not NaN
328 // TODO: is it faster to say !accum or accum==accum?
329 return accum == accum;
330#else
331 // use bit-or for speed, since we don't care about short-circuting the
332 // tests, and we expect the common case will be that we need to check all.
333 int isNaN = (SK_FixedNaN == fX) | (SK_FixedNaN == fX));
334 return !isNaN;
335#endif
336 }
337
reed@google.com24d10cb2013-01-28 22:36:34 +0000338 /**
339 * Returns true if the point's coordinates equal (x,y)
340 */
341 bool equals(SkScalar x, SkScalar y) const {
342 return fX == x && fY == y;
343 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344
345 friend bool operator==(const SkPoint& a, const SkPoint& b) {
346 return a.fX == b.fX && a.fY == b.fY;
347 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000348
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 friend bool operator!=(const SkPoint& a, const SkPoint& b) {
350 return a.fX != b.fX || a.fY != b.fY;
351 }
352
epoger@google.com94fa43c2012-04-11 17:51:01 +0000353 /** Return true if this point and the given point are far enough apart
354 such that a vector between them would be non-degenerate.
355
356 WARNING: Unlike the deprecated version of equalsWithinTolerance(),
357 this method does not use componentwise comparison. Instead, it
358 uses a comparison designed to match judgments elsewhere regarding
359 degeneracy ("points A and B are so close that the vector between them
360 is essentially zero").
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000361 */
epoger@google.com94fa43c2012-04-11 17:51:01 +0000362 bool equalsWithinTolerance(const SkPoint& p) const {
363 return !CanNormalize(fX - p.fX, fY - p.fY);
364 }
365
366 /** DEPRECATED: Return true if this and the given point are componentwise
367 within tolerance "tol".
368
369 WARNING: There is no guarantee that the result will reflect judgments
370 elsewhere regarding degeneracy ("points A and B are so close that the
371 vector between them is essentially zero").
372 */
373 bool equalsWithinTolerance(const SkPoint& p, SkScalar tol) const {
374 return SkScalarNearlyZero(fX - p.fX, tol)
375 && SkScalarNearlyZero(fY - p.fY, tol);
schenney@chromium.org4da06ab2011-12-20 15:14:18 +0000376 }
377
reed@android.com8a1c16f2008-12-17 15:59:43 +0000378 /** Returns a new point whose coordinates are the difference between
379 a's and b's (a - b)
380 */
381 friend SkPoint operator-(const SkPoint& a, const SkPoint& b) {
382 SkPoint v;
383 v.set(a.fX - b.fX, a.fY - b.fY);
384 return v;
385 }
386
387 /** Returns a new point whose coordinates are the sum of a's and b's (a + b)
388 */
389 friend SkPoint operator+(const SkPoint& a, const SkPoint& b) {
390 SkPoint v;
391 v.set(a.fX + b.fX, a.fY + b.fY);
392 return v;
393 }
394
395 /** Returns the euclidian distance from (0,0) to (x,y)
396 */
397 static SkScalar Length(SkScalar x, SkScalar y);
reed@android.comac753092010-01-28 21:34:33 +0000398
399 /** Normalize pt, returning its previous length. If the prev length is too
reed@google.com55b5f4b2011-09-07 12:23:41 +0000400 small (degenerate), return 0 and leave pt unchanged. This uses the same
401 tolerance as CanNormalize.
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000402
403 Note that this method may be significantly more expensive than
404 the non-static normalize(), because it has to return the previous length
405 of the point. If you don't need the previous length, call the
406 non-static normalize() method instead.
reed@android.comac753092010-01-28 21:34:33 +0000407 */
408 static SkScalar Normalize(SkPoint* pt);
409
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 /** Returns the euclidian distance between a and b
411 */
412 static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
413 return Length(a.fX - b.fX, a.fY - b.fY);
414 }
415
416 /** Returns the dot product of a and b, treating them as 2D vectors
417 */
418 static SkScalar DotProduct(const SkPoint& a, const SkPoint& b) {
419 return SkScalarMul(a.fX, b.fX) + SkScalarMul(a.fY, b.fY);
420 }
421
422 /** Returns the cross product of a and b, treating them as 2D vectors
423 */
424 static SkScalar CrossProduct(const SkPoint& a, const SkPoint& b) {
425 return SkScalarMul(a.fX, b.fY) - SkScalarMul(a.fY, b.fX);
426 }
reed@google.com7744c202011-05-06 19:26:26 +0000427
428 SkScalar cross(const SkPoint& vec) const {
429 return CrossProduct(*this, vec);
430 }
431
432 SkScalar dot(const SkPoint& vec) const {
433 return DotProduct(*this, vec);
434 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000435
reed@google.com7744c202011-05-06 19:26:26 +0000436 SkScalar lengthSqd() const {
437 return DotProduct(*this, *this);
438 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000439
reed@google.com7744c202011-05-06 19:26:26 +0000440 SkScalar distanceToSqd(const SkPoint& pt) const {
441 SkScalar dx = fX - pt.fX;
442 SkScalar dy = fY - pt.fY;
443 return SkScalarMul(dx, dx) + SkScalarMul(dy, dy);
444 }
bsalomon@google.com647a8042011-08-23 14:39:01 +0000445
446 /**
447 * The side of a point relative to a line. If the line is from a to b then
448 * the values are consistent with the sign of (b-a) cross (pt-a)
449 */
450 enum Side {
451 kLeft_Side = -1,
452 kOn_Side = 0,
453 kRight_Side = 1
454 };
455
456 /**
457 * Returns the squared distance to the infinite line between two pts. Also
458 * optionally returns the side of the line that the pt falls on (looking
459 * along line from a to b)
460 */
461 SkScalar distanceToLineBetweenSqd(const SkPoint& a,
462 const SkPoint& b,
463 Side* side = NULL) const;
464
465 /**
466 * Returns the distance to the infinite line between two pts. Also
467 * optionally returns the side of the line that the pt falls on (looking
468 * along the line from a to b)
469 */
470 SkScalar distanceToLineBetween(const SkPoint& a,
471 const SkPoint& b,
472 Side* side = NULL) const {
473 return SkScalarSqrt(this->distanceToLineBetweenSqd(a, b, side));
474 }
475
476 /**
477 * Returns the squared distance to the line segment between pts a and b
478 */
479 SkScalar distanceToLineSegmentBetweenSqd(const SkPoint& a,
reed@google.com7744c202011-05-06 19:26:26 +0000480 const SkPoint& b) const;
bsalomon@google.com647a8042011-08-23 14:39:01 +0000481
482 /**
483 * Returns the distance to the line segment between pts a and b.
484 */
485 SkScalar distanceToLineSegmentBetween(const SkPoint& a,
reed@google.com7744c202011-05-06 19:26:26 +0000486 const SkPoint& b) const {
487 return SkScalarSqrt(this->distanceToLineSegmentBetweenSqd(a, b));
488 }
bsalomon@google.com647a8042011-08-23 14:39:01 +0000489
490 /**
491 * Make this vector be orthogonal to vec. Looking down vec the
492 * new vector will point in direction indicated by side (which
493 * must be kLeft_Side or kRight_Side).
494 */
495 void setOrthog(const SkPoint& vec, Side side = kLeft_Side) {
496 // vec could be this
497 SkScalar tmp = vec.fX;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000498 if (kRight_Side == side) {
bsalomon@google.com647a8042011-08-23 14:39:01 +0000499 fX = -vec.fY;
500 fY = tmp;
501 } else {
bsalomon@google.com278dc692012-02-15 16:52:51 +0000502 SkASSERT(kLeft_Side == side);
bsalomon@google.com647a8042011-08-23 14:39:01 +0000503 fX = vec.fY;
504 fY = -tmp;
505 }
506 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507};
508
509typedef SkPoint SkVector;
510
511#endif