Skia 2D Graphics Library
SkPoint.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #ifndef SkPoint_DEFINED
9 #define SkPoint_DEFINED
10
11 #include "include/core/SkMath.h"
12 #include "include/core/SkScalar.h"
13 #include "include/private/SkSafe32.h"
14
15 struct SkIPoint;
16
21
25 struct SkIPoint {
26  int32_t fX;
27  int32_t fY;
28
35  static constexpr SkIPoint Make(int32_t x, int32_t y) {
36  return {x, y};
37  }
38
43  constexpr int32_t x() const { return fX; }
44
49  constexpr int32_t y() const { return fY; }
50
55  bool isZero() const { return (fX | fY) == 0; }
56
62  void set(int32_t x, int32_t y) {
63  fX = x;
64  fY = y;
65  }
66
71  SkIPoint operator-() const {
72  return {-fX, -fY};
73  }
74
79  void operator+=(const SkIVector& v) {
82  }
83
88  void operator-=(const SkIVector& v) {
89  fX = Sk32_sat_sub(fX, v.fX);
90  fY = Sk32_sat_sub(fY, v.fY);
91  }
92
99  bool equals(int32_t x, int32_t y) const {
100  return fX == x && fY == y;
101  }
102
109  friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
110  return a.fX == b.fX && a.fY == b.fY;
111  }
112
119  friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
120  return a.fX != b.fX || a.fY != b.fY;
121  }
122
131  friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) {
132  return { Sk32_sat_sub(a.fX, b.fX), Sk32_sat_sub(a.fY, b.fY) };
133  }
134
145  friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) {
147  }
148 };
149
150 struct SkPoint;
151
156
160 struct SK_API SkPoint {
163
170  static constexpr SkPoint Make(SkScalar x, SkScalar y) {
171  return {x, y};
172  }
173
178  constexpr SkScalar x() const { return fX; }
179
184  constexpr SkScalar y() const { return fY; }
185
190  bool isZero() const { return (0 == fX) & (0 == fY); }
191
197  void set(SkScalar x, SkScalar y) {
198  fX = x;
199  fY = y;
200  }
201
211  void iset(int32_t x, int32_t y) {
212  fX = SkIntToScalar(x);
213  fY = SkIntToScalar(y);
214  }
215
224  void iset(const SkIPoint& p) {
225  fX = SkIntToScalar(p.fX);
226  fY = SkIntToScalar(p.fY);
227  }
228
233  void setAbs(const SkPoint& pt) {
234  fX = SkScalarAbs(pt.fX);
235  fY = SkScalarAbs(pt.fY);
236  }
237
244  static void Offset(SkPoint points[], int count, const SkVector& offset) {
245  Offset(points, count, offset.fX, offset.fY);
246  }
247
255  static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) {
256  for (int i = 0; i < count; ++i) {
257  points[i].offset(dx, dy);
258  }
259  }
260
266  void offset(SkScalar dx, SkScalar dy) {
267  fX += dx;
268  fY += dy;
269  }
270
279  SkScalar length() const { return SkPoint::Length(fX, fY); }
280
289  SkScalar distanceToOrigin() const { return this->length(); }
290
299  bool normalize();
300
312
322  bool setLength(SkScalar length);
323
335  bool setLength(SkScalar x, SkScalar y, SkScalar length);
336
344  void scale(SkScalar scale, SkPoint* dst) const;
345
350  void scale(SkScalar value) { this->scale(value, this); }
351
354  void negate() {
355  fX = -fX;
356  fY = -fY;
357  }
358
363  SkPoint operator-() const {
364  return {-fX, -fY};
365  }
366
371  void operator+=(const SkVector& v) {
372  fX += v.fX;
373  fY += v.fY;
374  }
375
380  void operator-=(const SkVector& v) {
381  fX -= v.fX;
382  fY -= v.fY;
383  }
384
390  SkPoint operator*(SkScalar scale) const {
391  return {fX * scale, fY * scale};
392  }
393
400  fX *= scale;
401  fY *= scale;
402  return *this;
403  }
404
409  bool isFinite() const {
410  SkScalar accum = 0;
411  accum *= fX;
412  accum *= fY;
413
414  // accum is either NaN or it is finite (zero).
415  SkASSERT(0 == accum || SkScalarIsNaN(accum));
416
417  // value==value will be true iff value is not NaN
418  // TODO: is it faster to say !accum or accum==accum?
419  return !SkScalarIsNaN(accum);
420  }
421
428  bool equals(SkScalar x, SkScalar y) const {
429  return fX == x && fY == y;
430  }
431
438  friend bool operator==(const SkPoint& a, const SkPoint& b) {
439  return a.fX == b.fX && a.fY == b.fY;
440  }
441
448  friend bool operator!=(const SkPoint& a, const SkPoint& b) {
449  return a.fX != b.fX || a.fY != b.fY;
450  }
451
461  friend SkVector operator-(const SkPoint& a, const SkPoint& b) {
462  return {a.fX - b.fX, a.fY - b.fY};
463  }
464
475  friend SkPoint operator+(const SkPoint& a, const SkVector& b) {
476  return {a.fX + b.fX, a.fY + b.fY};
477  }
478
492
507
514  static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
515  return Length(a.fX - b.fX, a.fY - b.fY);
516  }
517
524  static SkScalar DotProduct(const SkVector& a, const SkVector& b) {
525  return a.fX * b.fX + a.fY * b.fY;
526  }
527
538  static SkScalar CrossProduct(const SkVector& a, const SkVector& b) {
539  return a.fX * b.fY - a.fY * b.fX;
540  }
541
551  SkScalar cross(const SkVector& vec) const {
552  return CrossProduct(*this, vec);
553  }
554
560  SkScalar dot(const SkVector& vec) const {
561  return DotProduct(*this, vec);
562  }
563
564 };
565
566 #endif
SkIPoint::operator-=
void operator-=(const SkIVector &v)
Subtracts ivector v from SkIPoint.
Definition: SkPoint.h:88
SkIPoint::fX
int32_t fX
x-axis value
Definition: SkPoint.h:26
SkPoint::length
SkScalar length() const
Returns the Euclidean distance from origin, computed as:
Definition: SkPoint.h:279
SkScalarIsNaN
static bool SkScalarIsNaN(SkScalar x)
Definition: SkScalar.h:67
SkVector
SkPoint SkVector
SkVector provides an alternative name for SkPoint.
Definition: SkPoint.h:150
SkIPoint::fY
int32_t fY
y-axis value
Definition: SkPoint.h:27
SkPoint::set
void set(SkScalar x, SkScalar y)
Sets fX to x and fY to y.
Definition: SkPoint.h:197
SkPoint::operator-=
void operator-=(const SkVector &v)
Subtracts vector v from SkPoint.
Definition: SkPoint.h:380
SkPoint::operator+
friend SkPoint operator+(const SkPoint &a, const SkVector &b)
Returns SkPoint resulting from SkPoint a offset by vector b, computed as: (a.fX + b....
Definition: SkPoint.h:475
SkPoint::setAbs
void setAbs(const SkPoint &pt)
Sets fX to absolute value of pt.fX; and fY to absolute value of pt.fY.
Definition: SkPoint.h:233
SkPoint::operator!=
friend bool operator!=(const SkPoint &a, const SkPoint &b)
Returns true if a is not equivalent to b.
Definition: SkPoint.h:448
SkPoint::Make
static constexpr SkPoint Make(SkScalar x, SkScalar y)
Sets fX to x, fY to y.
Definition: SkPoint.h:170
SkIPoint::equals
bool equals(int32_t x, int32_t y) const
Returns true if SkIPoint is equivalent to SkIPoint constructed from (x, y).
Definition: SkPoint.h:99
SkPoint::cross
SkScalar cross(const SkVector &vec) const
Returns the cross product of vector and vec.
Definition: SkPoint.h:551
SkPoint::Distance
static SkScalar Distance(const SkPoint &a, const SkPoint &b)
Returns the Euclidean distance between a and b.
Definition: SkPoint.h:514
SkIPoint::operator+
friend SkIPoint operator+(const SkIPoint &a, const SkIVector &b)
Returns SkIPoint resulting from SkIPoint a offset by ivector b, computed as: (a.fX + b....
Definition: SkPoint.h:145
SkIPoint::y
constexpr int32_t y() const
Returns y-axis value of SkIPoint.
Definition: SkPoint.h:49
SkPoint::negate
void negate()
Changes the sign of fX and fY.
Definition: SkPoint.h:354
SkIPoint::x
constexpr int32_t x() const
Returns x-axis value of SkIPoint.
Definition: SkPoint.h:43
SkPoint::DotProduct
static SkScalar DotProduct(const SkVector &a, const SkVector &b)
Returns the dot product of vector a and vector b.
Definition: SkPoint.h:524
SkPoint::Offset
static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy)
Adds offset (dx, dy) to each SkPoint in points array of length count.
Definition: SkPoint.h:255
SkPoint::operator*
SkPoint operator*(SkScalar scale) const
Returns SkPoint multiplied by scale.
Definition: SkPoint.h:390
SkIPoint::operator-
SkIPoint operator-() const
Returns SkIPoint changing the signs of fX and fY.
Definition: SkPoint.h:71
SkPoint::Offset
static void Offset(SkPoint points[], int count, const SkVector &offset)
Adds offset to each SkPoint in points array with count entries.
Definition: SkPoint.h:244
SkIPoint::set
void set(int32_t x, int32_t y)
Sets fX to x and fY to y.
Definition: SkPoint.h:62
SkScalar.h
SkIPoint::isZero
bool isZero() const
Returns true if fX and fY are both zero.
Definition: SkPoint.h:55
SkPoint::iset
void iset(int32_t x, int32_t y)
Sets fX to x and fY to y, promoting integers to SkScalar values.
Definition: SkPoint.h:211
SkPoint
Definition: SkPoint.h:160
SkPoint::setNormalize
bool setNormalize(SkScalar x, SkScalar y)
Sets vector to (x, y) scaled so length() returns one, and so that (fX, fY) is proportional to (x,...
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:468
SkPoint::normalize
bool normalize()
Scales (fX, fY) so that length() returns one, while preserving ratio of fX to fY, if possible.
SkPoint::fY
SkScalar fY
y-axis value
Definition: SkPoint.h:162
SkPoint::Length
static SkScalar Length(SkScalar x, SkScalar y)
Returns the Euclidean distance from origin, computed as:
SkPoint::operator==
friend bool operator==(const SkPoint &a, const SkPoint &b)
Returns true if a is equivalent to b.
Definition: SkPoint.h:438
SkIPoint::operator-
friend SkIVector operator-(const SkIPoint &a, const SkIPoint &b)
Returns ivector from b to a; computed as (a.fX - b.fX, a.fY - b.fY).
Definition: SkPoint.h:131
SkPoint::scale
void scale(SkScalar value)
Scales SkPoint in place by scale.
Definition: SkPoint.h:350
SkPoint::offset
void offset(SkScalar dx, SkScalar dy)
Adds offset (dx, dy) to SkPoint.
Definition: SkPoint.h:266
SkPoint::dot
SkScalar dot(const SkVector &vec) const
Returns the dot product of vector and vector vec.
Definition: SkPoint.h:560
SkPoint::operator+=
void operator+=(const SkVector &v)
Definition: SkPoint.h:371
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkPoint::operator-
SkPoint operator-() const
Returns SkPoint changing the signs of fX and fY.
Definition: SkPoint.h:363
SkScalarAbs
#define SkScalarAbs(x)
Definition: SkScalar.h:38
SkIPoint::Make
static constexpr SkIPoint Make(int32_t x, int32_t y)
Sets fX to x, fY to y.
Definition: SkPoint.h:35
SkPoint::operator*=
SkPoint & operator*=(SkScalar scale)
Multiplies SkPoint by scale.
Definition: SkPoint.h:399
SkPoint::setLength
bool setLength(SkScalar x, SkScalar y, SkScalar length)
Sets vector to (x, y) scaled to length, if possible.
SkPoint::fX
SkScalar fX
x-axis value
Definition: SkPoint.h:161
SkPoint::isZero
bool isZero() const
Returns true if fX and fY are both zero.
Definition: SkPoint.h:190
SkIPoint::operator==
friend bool operator==(const SkIPoint &a, const SkIPoint &b)
Returns true if a is equivalent to b.
Definition: SkPoint.h:109
SkPoint::distanceToOrigin
SkScalar distanceToOrigin() const
Returns the Euclidean distance from origin, computed as:
Definition: SkPoint.h:289
SkPoint::Normalize
static SkScalar Normalize(SkVector *vec)
Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX to vec->fY,...
SkPoint::scale
void scale(SkScalar scale, SkPoint *dst) const
Sets dst to SkPoint times scale.
SkPoint::y
constexpr SkScalar y() const
Returns y-axis value of SkPoint or vector.
Definition: SkPoint.h:184
SkPoint::isFinite
bool isFinite() const
Returns true if both fX and fY are measurable values.
Definition: SkPoint.h:409
SkIntToScalar
#define SkIntToScalar(x)
Definition: SkScalar.h:56
SkPoint::operator-
friend SkVector operator-(const SkPoint &a, const SkPoint &b)
Returns vector from b to a, computed as (a.fX - b.fX, a.fY - b.fY).
Definition: SkPoint.h:461
SkIPoint
Definition: SkPoint.h:25
SkPoint::equals
bool equals(SkScalar x, SkScalar y) const
Returns true if SkPoint is equivalent to SkPoint constructed from (x, y).
Definition: SkPoint.h:428
SkPoint::setLength
bool setLength(SkScalar length)
Scales vector so that distanceToOrigin() returns length, if possible.
SkIPoint::operator!=
friend bool operator!=(const SkIPoint &a, const SkIPoint &b)
Returns true if a is not equivalent to b.
Definition: SkPoint.h:119
SkIPoint::operator+=
void operator+=(const SkIVector &v)
Offsets SkIPoint by ivector v.
Definition: SkPoint.h:79
SkIVector
SkIPoint SkIVector
SkIVector provides an alternative name for SkIPoint.
Definition: SkPoint.h:15
SkPoint::iset
void iset(const SkIPoint &p)
Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values.
Definition: SkPoint.h:224
SkPoint::CrossProduct
static SkScalar CrossProduct(const SkVector &a, const SkVector &b)
Returns the cross product of vector a and vector b.
Definition: SkPoint.h:538
SkPoint::x
constexpr SkScalar x() const
Returns x-axis value of SkPoint or vector.
Definition: SkPoint.h:178
SkMath.h