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 
20 typedef SkIPoint SkIVector;
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) {
80  fX = Sk32_sat_add(fX, v.fX);
81  fY = Sk32_sat_add(fY, v.fY);
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) {
146  return { Sk32_sat_add(a.fX, b.fX), Sk32_sat_add(a.fY, b.fY) };
147  }
148 };
149 
150 struct SkPoint;
151 
155 typedef SkPoint SkVector;
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 
311  bool setNormalize(SkScalar x, SkScalar y);
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 
491  static SkScalar Length(SkScalar x, SkScalar y);
492 
506  static SkScalar Normalize(SkVector* vec);
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
constexpr int32_t y() const
Returns y-axis value of SkIPoint.
Definition: SkPoint.h:49
static SkScalar Length(SkScalar x, SkScalar y)
Returns the Euclidean distance from origin, computed as:
void iset(const SkIPoint &p)
Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values.
Definition: SkPoint.h:224
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 SkVector
SkVector provides an alternative name for SkPoint.
Definition: SkPoint.h:150
SkIPoint SkIVector
SkIVector provides an alternative name for SkIPoint.
Definition: SkPoint.h:15
void negate()
Changes the sign of fX and fY.
Definition: SkPoint.h:354
SkPoint holds two 32-bit floating point coordinates.
Definition: SkPoint.h:160
static SkScalar Distance(const SkPoint &a, const SkPoint &b)
Returns the Euclidean distance between a and b.
Definition: SkPoint.h:514
SkIPoint operator-() const
Returns SkIPoint changing the signs of fX and fY.
Definition: SkPoint.h:71
#define SK_API
Definition: SkTypes.h:187
friend bool operator!=(const SkPoint &a, const SkPoint &b)
Returns true if a is not equivalent to b.
Definition: SkPoint.h:448
bool equals(SkScalar x, SkScalar y) const
Returns true if SkPoint is equivalent to SkPoint constructed from (x, y).
Definition: SkPoint.h:428
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
SkPoint operator*(SkScalar scale) const
Returns SkPoint multiplied by scale.
Definition: SkPoint.h:390
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
bool isFinite() const
Returns true if both fX and fY are measurable values.
Definition: SkPoint.h:409
SkScalar distanceToOrigin() const
Returns the Euclidean distance from origin, computed as:
Definition: SkPoint.h:289
void operator+=(const SkIVector &v)
Offsets SkIPoint by ivector v.
Definition: SkPoint.h:79
constexpr SkScalar y() const
Returns y-axis value of SkPoint or vector.
Definition: SkPoint.h:184
#define SkScalarAbs(x)
Definition: SkScalar.h:38
bool isZero() const
Returns true if fX and fY are both zero.
Definition: SkPoint.h:55
SkScalar cross(const SkVector &vec) const
Returns the cross product of vector and vec.
Definition: SkPoint.h:551
SkPoint operator-() const
Returns SkPoint changing the signs of fX and fY.
Definition: SkPoint.h:363
void operator-=(const SkVector &v)
Subtracts vector v from SkPoint.
Definition: SkPoint.h:380
SkScalar length() const
Returns the Euclidean distance from origin, computed as:
Definition: SkPoint.h:279
void operator-=(const SkIVector &v)
Subtracts ivector v from SkIPoint.
Definition: SkPoint.h:88
friend bool operator==(const SkPoint &a, const SkPoint &b)
Returns true if a is equivalent to b.
Definition: SkPoint.h:438
constexpr int32_t x() const
Returns x-axis value of SkIPoint.
Definition: SkPoint.h:43
#define SkASSERT(cond)
Definition: SkTypes.h:471
int32_t fX
x-axis value
Definition: SkPoint.h:26
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
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
friend bool operator!=(const SkIPoint &a, const SkIPoint &b)
Returns true if a is not equivalent to b.
Definition: SkPoint.h:119
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
static constexpr SkIPoint Make(int32_t x, int32_t y)
Sets fX to x, fY to y.
Definition: SkPoint.h:35
void operator+=(const SkVector &v)
Adds vector v to SkPoint.
Definition: SkPoint.h:371
static constexpr SkPoint Make(SkScalar x, SkScalar y)
Sets fX to x, fY to y.
Definition: SkPoint.h:170
SkScalar dot(const SkVector &vec) const
Returns the dot product of vector and vector vec.
Definition: SkPoint.h:560
void offset(SkScalar dx, SkScalar dy)
Adds offset (dx, dy) to SkPoint.
Definition: SkPoint.h:266
SkScalar fX
x-axis value
Definition: SkPoint.h:161
float SkScalar
Definition: SkScalar.h:16
friend bool operator==(const SkIPoint &a, const SkIPoint &b)
Returns true if a is equivalent to b.
Definition: SkPoint.h:109
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
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
static bool SkScalarIsNaN(SkScalar x)
Definition: SkScalar.h:67
constexpr SkScalar x() const
Returns x-axis value of SkPoint or vector.
Definition: SkPoint.h:178
bool isZero() const
Returns true if fX and fY are both zero.
Definition: SkPoint.h:190
static SkScalar CrossProduct(const SkVector &a, const SkVector &b)
Returns the cross product of vector a and vector b.
Definition: SkPoint.h:538
int32_t fY
y-axis value
Definition: SkPoint.h:27
SkScalar fY
y-axis value
Definition: SkPoint.h:162
SkIPoint holds two 32-bit integer coordinates.
Definition: SkPoint.h:25
static SkScalar DotProduct(const SkVector &a, const SkVector &b)
Returns the dot product of vector a and vector b.
Definition: SkPoint.h:524
#define SkIntToScalar(x)
Definition: SkScalar.h:56
SkPoint & operator*=(SkScalar scale)
Multiplies SkPoint by scale.
Definition: SkPoint.h:399
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
void scale(SkScalar value)
Scales SkPoint in place by scale.
Definition: SkPoint.h:350