8 #ifndef SkPoint_DEFINED
9 #define SkPoint_DEFINED
27 int16_t x()
const {
return fX; }
28 int16_t y()
const {
return fY; }
30 void set(
int x,
int y) {
43 static SkIPoint Make(int32_t x, int32_t y) {
49 int32_t x()
const {
return fX; }
50 int32_t y()
const {
return fY; }
51 void setX(int32_t x) { fX = x; }
52 void setY(int32_t y) { fY = y; }
57 bool isZero()
const {
return (fX | fY) == 0; }
65 void set(int32_t x, int32_t y) { fX = x; fY = y; }
89 void negate() { fX = -fX; fY = -fY; }
114 bool equals(int32_t x, int32_t y)
const {
115 return fX == x && fY == y;
119 return a.fX == b.fX && a.fY == b.fY;
123 return a.fX != b.fX || a.fY != b.fY;
131 v.
set(a.fX - b.fX, a.fY - b.fY);
139 v.
set(a.fX + b.fX, a.fY + b.fY);
146 return a.fX * b.fX + a.fY * b.fY;
152 return a.fX * b.fY - a.fY * b.fX;
159 static SkPoint Make(SkScalar x, SkScalar y) {
165 SkScalar x()
const {
return fX; }
166 SkScalar y()
const {
return fY; }
171 bool isZero()
const {
return (0 == fX) & (0 == fY); }
174 void set(SkScalar x, SkScalar y) { fX = x; fY = y; }
179 void iset(int32_t x, int32_t y) {
180 fX = SkIntToScalar(x);
181 fY = SkIntToScalar(y);
188 fX = SkIntToScalar(p.fX);
189 fY = SkIntToScalar(p.fY);
192 void setAbs(
const SkPoint& pt) {
193 fX = SkScalarAbs(pt.fX);
194 fY = SkScalarAbs(pt.fY);
198 void setIRectFan(
int l,
int t,
int r,
int b) {
200 v[0].
set(SkIntToScalar(l), SkIntToScalar(t));
201 v[1].
set(SkIntToScalar(l), SkIntToScalar(b));
202 v[2].
set(SkIntToScalar(r), SkIntToScalar(b));
203 v[3].
set(SkIntToScalar(r), SkIntToScalar(t));
205 void setIRectFan(
int l,
int t,
int r,
int b,
size_t stride);
208 void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b) {
216 void setRectFan(SkScalar l, SkScalar t, SkScalar r, SkScalar b,
size_t stride) {
217 SkASSERT(stride >=
sizeof(
SkPoint));
219 ((
SkPoint*)((intptr_t)
this + 0 * stride))->set(l, t);
220 ((
SkPoint*)((intptr_t)
this + 1 * stride))->set(l, b);
221 ((
SkPoint*)((intptr_t)
this + 2 * stride))->set(r, b);
222 ((
SkPoint*)((intptr_t)
this + 3 * stride))->set(r, t);
226 static void Offset(
SkPoint points[],
int count,
const SkPoint& offset) {
227 Offset(points, count, offset.fX, offset.fY);
230 static void Offset(
SkPoint points[],
int count, SkScalar dx, SkScalar dy) {
231 for (
int i = 0; i < count; ++i) {
232 points[i].offset(dx, dy);
236 void offset(SkScalar dx, SkScalar dy) {
244 SkScalar distanceToOrigin()
const {
return this->length(); }
252 return (dx*dx + dy*dy) > (SK_ScalarNearlyZero * SK_ScalarNearlyZero);
255 bool canNormalize()
const {
256 return CanNormalize(fX, fY);
269 bool setNormalize(SkScalar x, SkScalar y);
275 bool setLength(SkScalar length);
281 bool setLength(SkScalar x, SkScalar y, SkScalar length);
285 bool setLengthFast(SkScalar length);
289 bool setLengthFast(SkScalar x, SkScalar y, SkScalar length);
294 void scale(SkScalar scale,
SkPoint* dst)
const;
304 void rotateCW(
SkPoint* dst)
const;
314 void rotateCCW(
SkPoint* dst)
const;
351 SkPoint operator*(SkScalar scale)
const {
352 return Make(fX * scale, fY * scale);
355 SkPoint& operator*=(SkScalar scale) {
370 SkASSERT(0 == accum || SkScalarIsNaN(accum));
374 return !SkScalarIsNaN(accum);
380 bool equals(SkScalar x, SkScalar y)
const {
381 return fX == x && fY == y;
385 return a.fX == b.fX && a.fY == b.fY;
389 return a.fX != b.fX || a.fY != b.fY;
402 return !CanNormalize(fX - p.fX, fY - p.fY);
410 return SkScalarNearlyZero(fX - p.fX, tol)
411 && SkScalarNearlyZero(fY - p.fY, tol);
419 v.
set(a.fX - b.fX, a.fY - b.fY);
427 v.
set(a.fX + b.fX, a.fY + b.fY);
433 static SkScalar Length(SkScalar x, SkScalar y);
444 static SkScalar Normalize(
SkPoint* pt);
449 return Length(a.fX - b.fX, a.fY - b.fY);
455 return a.fX * b.fX + a.fY * b.fY;
461 return a.fX * b.fY - a.fY * b.fX;
464 SkScalar cross(
const SkPoint& vec)
const {
465 return CrossProduct(*
this, vec);
468 SkScalar dot(
const SkPoint& vec)
const {
469 return DotProduct(*
this, vec);
472 SkScalar lengthSqd()
const {
473 return DotProduct(*
this, *
this);
476 SkScalar distanceToSqd(
const SkPoint& pt)
const {
477 SkScalar dx = fX - pt.fX;
478 SkScalar dy = fY - pt.fY;
479 return dx * dx + dy * dy;
497 SkScalar distanceToLineBetweenSqd(
const SkPoint& a,
499 Side* side = NULL)
const;
508 Side* side = NULL)
const {
509 return SkScalarSqrt(this->distanceToLineBetweenSqd(a, b, side));
515 SkScalar distanceToLineSegmentBetweenSqd(
const SkPoint& a,
523 return SkScalarSqrt(this->distanceToLineSegmentBetweenSqd(a, b));
533 SkScalar tmp = vec.fX;
534 if (kRight_Side == side) {
538 SkASSERT(kLeft_Side == side);
552 static inline bool SkPointsAreFinite(
const SkPoint array[],
int count) {
553 return SkScalarsAreFinite(&array[0].fX, count << 1);
void operator-=(const SkPoint &v)
Subtract v's coordinates from the point's.
Definition: SkPoint.h:346
SkScalar distanceToLineBetween(const SkPoint &a, const SkPoint &b, Side *side=NULL) const
Returns the distance to the infinite line between two pts.
Definition: SkPoint.h:506
static SkScalar Length(SkScalar x, SkScalar y)
Returns the euclidian distance from (0,0) to (x,y)
void iset(const SkIPoint &p)
Set the point's X and Y coordinates by automatically promoting p's coordinates to SkScalar values...
Definition: SkPoint.h:187
void set(SkScalar x, SkScalar y)
Set the point's X and Y coordinates.
Definition: SkPoint.h:174
void negate()
Negate the point's coordinates.
Definition: SkPoint.h:323
SkIPoint operator-() const
Return a new point whose X and Y coordinates are the negative of the original point's.
Definition: SkPoint.h:94
bool equalsWithinTolerance(const SkPoint &p, SkScalar tol) const
WARNING: There is no guarantee that the result will reflect judgments elsewhere regarding degeneracy ...
Definition: SkPoint.h:409
Definition: SkPoint.h:156
static SkScalar Distance(const SkPoint &a, const SkPoint &b)
Returns the euclidian distance between a and b.
Definition: SkPoint.h:448
const SkScalar * asScalars() const
cast-safe way to treat the point as an array of (2) SkScalars.
Definition: SkPoint.h:547
void operator+=(const SkIPoint &v)
Add v's coordinates to this point's.
Definition: SkPoint.h:102
friend SkIPoint operator+(const SkIPoint &a, const SkIPoint &b)
Returns a new point whose coordinates are the sum of a and b (a + b)
Definition: SkPoint.h:137
bool isFinite() const
Returns true if both X and Y are finite (not infinity or NaN)
Definition: SkPoint.h:364
SkScalar length() const
Return the euclidian distance from (0,0) to the point.
Definition: SkPoint.h:243
Side
The side of a point relative to a line.
Definition: SkPoint.h:486
void setZero()
Set both fX and fY to zero.
Definition: SkPoint.h:62
void rotateCCW()
Rotate the point counter-clockwise by 90 degrees, writing the answer back into the point...
Definition: SkPoint.h:319
static bool CanNormalize(SkScalar dx, SkScalar dy)
Return true if the computed length of the vector is >= the internal tolerance (used to avoid dividing...
Definition: SkPoint.h:250
void rotateCW()
Rotate the point clockwise, writing the new point back into the point.
Definition: SkPoint.h:75
static SkScalar DotProduct(const SkPoint &a, const SkPoint &b)
Returns the dot product of a and b, treating them as 2D vectors.
Definition: SkPoint.h:454
bool isZero() const
Returns true iff fX and fY are both zero.
Definition: SkPoint.h:57
SkIPoint holds two 16 bit integer coordinates.
Definition: SkPoint.h:18
bool equals(int32_t x, int32_t y) const
Returns true if the point's coordinates equal (x,y)
Definition: SkPoint.h:114
void setOrthog(const SkPoint &vec, Side side=kLeft_Side)
Make this vector be orthogonal to vec.
Definition: SkPoint.h:531
bool equals(SkScalar x, SkScalar y) const
Returns true if the point's coordinates equal (x,y)
Definition: SkPoint.h:380
static SkScalar CrossProduct(const SkPoint &a, const SkPoint &b)
Returns the cross product of a and b, treating them as 2D vectors.
Definition: SkPoint.h:460
SkScalar distanceToLineSegmentBetween(const SkPoint &a, const SkPoint &b) const
Returns the distance to the line segment between pts a and b.
Definition: SkPoint.h:521
static int32_t DotProduct(const SkIPoint &a, const SkIPoint &b)
Returns the dot product of a and b, treating them as 2D vectors.
Definition: SkPoint.h:145
void negate()
Negate the X and Y coordinates of the point.
Definition: SkPoint.h:89
SkPoint operator-() const
Returns a new point whose coordinates are the negative of the point's.
Definition: SkPoint.h:330
void iset(int32_t x, int32_t y)
Set the point's X and Y coordinates by automatically promoting (x,y) to SkScalar values.
Definition: SkPoint.h:179
friend SkPoint operator-(const SkPoint &a, const SkPoint &b)
Returns a new point whose coordinates are the difference between a's and b's (a - b) ...
Definition: SkPoint.h:417
friend SkPoint operator+(const SkPoint &a, const SkPoint &b)
Returns a new point whose coordinates are the sum of a's and b's (a + b)
Definition: SkPoint.h:425
void operator+=(const SkPoint &v)
Add v's coordinates to the point's.
Definition: SkPoint.h:339
void set(int32_t x, int32_t y)
Set the x and y values of the point.
Definition: SkPoint.h:65
friend SkIPoint operator-(const SkIPoint &a, const SkIPoint &b)
Returns a new point whose coordinates are the difference between a and b (i.e.
Definition: SkPoint.h:129
void rotateCCW()
Rotate the point counter-clockwise, writing the new point back into the point.
Definition: SkPoint.h:85
bool equalsWithinTolerance(const SkPoint &p) const
Return true if this point and the given point are far enough apart such that a vector between them wo...
Definition: SkPoint.h:401
void operator-=(const SkIPoint &v)
Subtract v's coordinates from this point's.
Definition: SkPoint.h:108
static int32_t CrossProduct(const SkIPoint &a, const SkIPoint &b)
Returns the cross product of a and b, treating them as 2D vectors.
Definition: SkPoint.h:151
SkIPoint holds two 32 bit integer coordinates.
Definition: SkPoint.h:40
bool isZero() const
Returns true iff fX and fY are both zero.
Definition: SkPoint.h:171
void scale(SkScalar value)
Scale the point's coordinates by scale, writing the answer back into the point.
Definition: SkPoint.h:299
void rotateCW()
Rotate the point clockwise by 90 degrees, writing the answer back into the point. ...
Definition: SkPoint.h:309