SkPoint.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. #ifndef SkPoint_DEFINED
  8. #define SkPoint_DEFINED
  9. #include "include/core/SkMath.h"
  10. #include "include/core/SkScalar.h"
  11. #include "include/private/SkSafe32.h"
  12. struct SkIPoint;
  13. /** SkIVector provides an alternative name for SkIPoint. SkIVector and SkIPoint
  14. can be used interchangeably for all purposes.
  15. */
  16. typedef SkIPoint SkIVector;
  17. /** \struct SkIPoint
  18. SkIPoint holds two 32-bit integer coordinates.
  19. */
  20. struct SkIPoint {
  21. int32_t fX; //!< x-axis value
  22. int32_t fY; //!< y-axis value
  23. /** Sets fX to x, fY to y.
  24. @param x integer x-axis value of constructed SkIPoint
  25. @param y integer y-axis value of constructed SkIPoint
  26. @return SkIPoint (x, y)
  27. */
  28. static constexpr SkIPoint Make(int32_t x, int32_t y) {
  29. return {x, y};
  30. }
  31. /** Returns x-axis value of SkIPoint.
  32. @return fX
  33. */
  34. int32_t x() const { return fX; }
  35. /** Returns y-axis value of SkIPoint.
  36. @return fY
  37. */
  38. int32_t y() const { return fY; }
  39. /** Returns true if fX and fY are both zero.
  40. @return true if fX is zero and fY is zero
  41. */
  42. bool isZero() const { return (fX | fY) == 0; }
  43. /** Sets fX to x and fY to y.
  44. @param x new value for fX
  45. @param y new value for fY
  46. */
  47. void set(int32_t x, int32_t y) {
  48. fX = x;
  49. fY = y;
  50. }
  51. /** Returns SkIPoint changing the signs of fX and fY.
  52. @return SkIPoint as (-fX, -fY)
  53. */
  54. SkIPoint operator-() const {
  55. return {-fX, -fY};
  56. }
  57. /** Offsets SkIPoint by ivector v. Sets SkIPoint to (fX + v.fX, fY + v.fY).
  58. @param v ivector to add
  59. */
  60. void operator+=(const SkIVector& v) {
  61. fX = Sk32_sat_add(fX, v.fX);
  62. fY = Sk32_sat_add(fY, v.fY);
  63. }
  64. /** Subtracts ivector v from SkIPoint. Sets SkIPoint to: (fX - v.fX, fY - v.fY).
  65. @param v ivector to subtract
  66. */
  67. void operator-=(const SkIVector& v) {
  68. fX = Sk32_sat_sub(fX, v.fX);
  69. fY = Sk32_sat_sub(fY, v.fY);
  70. }
  71. /** Returns true if SkIPoint is equivalent to SkIPoint constructed from (x, y).
  72. @param x value compared with fX
  73. @param y value compared with fY
  74. @return true if SkIPoint equals (x, y)
  75. */
  76. bool equals(int32_t x, int32_t y) const {
  77. return fX == x && fY == y;
  78. }
  79. /** Returns true if a is equivalent to b.
  80. @param a SkIPoint to compare
  81. @param b SkIPoint to compare
  82. @return true if a.fX == b.fX and a.fY == b.fY
  83. */
  84. friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
  85. return a.fX == b.fX && a.fY == b.fY;
  86. }
  87. /** Returns true if a is not equivalent to b.
  88. @param a SkIPoint to compare
  89. @param b SkIPoint to compare
  90. @return true if a.fX != b.fX or a.fY != b.fY
  91. */
  92. friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
  93. return a.fX != b.fX || a.fY != b.fY;
  94. }
  95. /** Returns ivector from b to a; computed as (a.fX - b.fX, a.fY - b.fY).
  96. Can also be used to subtract ivector from ivector, returning ivector.
  97. @param a SkIPoint or ivector to subtract from
  98. @param b ivector to subtract
  99. @return ivector from b to a
  100. */
  101. friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) {
  102. return { Sk32_sat_sub(a.fX, b.fX), Sk32_sat_sub(a.fY, b.fY) };
  103. }
  104. /** Returns SkIPoint resulting from SkIPoint a offset by ivector b, computed as:
  105. (a.fX + b.fX, a.fY + b.fY).
  106. Can also be used to offset SkIPoint b by ivector a, returning SkIPoint.
  107. Can also be used to add ivector to ivector, returning ivector.
  108. @param a SkIPoint or ivector to add to
  109. @param b SkIPoint or ivector to add
  110. @return SkIPoint equal to a offset by b
  111. */
  112. friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) {
  113. return { Sk32_sat_add(a.fX, b.fX), Sk32_sat_add(a.fY, b.fY) };
  114. }
  115. };
  116. struct SkPoint;
  117. /** SkVector provides an alternative name for SkPoint. SkVector and SkPoint can
  118. be used interchangeably for all purposes.
  119. */
  120. typedef SkPoint SkVector;
  121. /** \struct SkPoint
  122. SkPoint holds two 32-bit floating point coordinates.
  123. */
  124. struct SK_API SkPoint {
  125. SkScalar fX; //!< x-axis value
  126. SkScalar fY; //!< y-axis value
  127. /** Sets fX to x, fY to y. Used both to set SkPoint and vector.
  128. @param x SkScalar x-axis value of constructed SkPoint or vector
  129. @param y SkScalar y-axis value of constructed SkPoint or vector
  130. @return SkPoint (x, y)
  131. */
  132. static constexpr SkPoint Make(SkScalar x, SkScalar y) {
  133. return {x, y};
  134. }
  135. /** Returns x-axis value of SkPoint or vector.
  136. @return fX
  137. */
  138. SkScalar x() const { return fX; }
  139. /** Returns y-axis value of SkPoint or vector.
  140. @return fY
  141. */
  142. SkScalar y() const { return fY; }
  143. /** Returns true if fX and fY are both zero.
  144. @return true if fX is zero and fY is zero
  145. */
  146. bool isZero() const { return (0 == fX) & (0 == fY); }
  147. /** Sets fX to x and fY to y.
  148. @param x new value for fX
  149. @param y new value for fY
  150. */
  151. void set(SkScalar x, SkScalar y) {
  152. fX = x;
  153. fY = y;
  154. }
  155. /** Sets fX to x and fY to y, promoting integers to SkScalar values.
  156. Assigning a large integer value directly to fX or fY may cause a compiler
  157. error, triggered by narrowing conversion of int to SkScalar. This safely
  158. casts x and y to avoid the error.
  159. @param x new value for fX
  160. @param y new value for fY
  161. */
  162. void iset(int32_t x, int32_t y) {
  163. fX = SkIntToScalar(x);
  164. fY = SkIntToScalar(y);
  165. }
  166. /** Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values.
  167. Assigning an SkIPoint containing a large integer value directly to fX or fY may
  168. cause a compiler error, triggered by narrowing conversion of int to SkScalar.
  169. This safely casts p.fX and p.fY to avoid the error.
  170. @param p SkIPoint members promoted to SkScalar
  171. */
  172. void iset(const SkIPoint& p) {
  173. fX = SkIntToScalar(p.fX);
  174. fY = SkIntToScalar(p.fY);
  175. }
  176. /** Sets fX to absolute value of pt.fX; and fY to absolute value of pt.fY.
  177. @param pt members providing magnitude for fX and fY
  178. */
  179. void setAbs(const SkPoint& pt) {
  180. fX = SkScalarAbs(pt.fX);
  181. fY = SkScalarAbs(pt.fY);
  182. }
  183. /** Adds offset to each SkPoint in points array with count entries.
  184. @param points SkPoint array
  185. @param count entries in array
  186. @param offset vector added to points
  187. */
  188. static void Offset(SkPoint points[], int count, const SkVector& offset) {
  189. Offset(points, count, offset.fX, offset.fY);
  190. }
  191. /** Adds offset (dx, dy) to each SkPoint in points array of length count.
  192. @param points SkPoint array
  193. @param count entries in array
  194. @param dx added to fX in points
  195. @param dy added to fY in points
  196. */
  197. static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) {
  198. for (int i = 0; i < count; ++i) {
  199. points[i].offset(dx, dy);
  200. }
  201. }
  202. /** Adds offset (dx, dy) to SkPoint.
  203. @param dx added to fX
  204. @param dy added to fY
  205. */
  206. void offset(SkScalar dx, SkScalar dy) {
  207. fX += dx;
  208. fY += dy;
  209. }
  210. /** Returns the Euclidean distance from origin, computed as:
  211. sqrt(fX * fX + fY * fY)
  212. .
  213. @return straight-line distance to origin
  214. */
  215. SkScalar length() const { return SkPoint::Length(fX, fY); }
  216. /** Returns the Euclidean distance from origin, computed as:
  217. sqrt(fX * fX + fY * fY)
  218. .
  219. @return straight-line distance to origin
  220. */
  221. SkScalar distanceToOrigin() const { return this->length(); }
  222. /** Scales (fX, fY) so that length() returns one, while preserving ratio of fX to fY,
  223. if possible. If prior length is nearly zero, sets vector to (0, 0) and returns
  224. false; otherwise returns true.
  225. @return true if former length is not zero or nearly zero
  226. */
  227. bool normalize();
  228. /** Sets vector to (x, y) scaled so length() returns one, and so that
  229. (fX, fY) is proportional to (x, y). If (x, y) length is nearly zero,
  230. sets vector to (0, 0) and returns false; otherwise returns true.
  231. @param x proportional value for fX
  232. @param y proportional value for fY
  233. @return true if (x, y) length is not zero or nearly zero
  234. */
  235. bool setNormalize(SkScalar x, SkScalar y);
  236. /** Scales vector so that distanceToOrigin() returns length, if possible. If former
  237. length is nearly zero, sets vector to (0, 0) and return false; otherwise returns
  238. true.
  239. @param length straight-line distance to origin
  240. @return true if former length is not zero or nearly zero
  241. */
  242. bool setLength(SkScalar length);
  243. /** Sets vector to (x, y) scaled to length, if possible. If former
  244. length is nearly zero, sets vector to (0, 0) and return false; otherwise returns
  245. true.
  246. @param x proportional value for fX
  247. @param y proportional value for fY
  248. @param length straight-line distance to origin
  249. @return true if (x, y) length is not zero or nearly zero
  250. */
  251. bool setLength(SkScalar x, SkScalar y, SkScalar length);
  252. /** Sets dst to SkPoint times scale. dst may be SkPoint to modify SkPoint in place.
  253. @param scale factor to multiply SkPoint by
  254. @param dst storage for scaled SkPoint
  255. */
  256. void scale(SkScalar scale, SkPoint* dst) const;
  257. /** Scales SkPoint in place by scale.
  258. @param value factor to multiply SkPoint by
  259. */
  260. void scale(SkScalar value) { this->scale(value, this); }
  261. /** Changes the sign of fX and fY.
  262. */
  263. void negate() {
  264. fX = -fX;
  265. fY = -fY;
  266. }
  267. /** Returns SkPoint changing the signs of fX and fY.
  268. @return SkPoint as (-fX, -fY)
  269. */
  270. SkPoint operator-() const {
  271. return {-fX, -fY};
  272. }
  273. /** Adds vector v to SkPoint. Sets SkPoint to: (fX + v.fX, fY + v.fY).
  274. @param v vector to add
  275. */
  276. void operator+=(const SkVector& v) {
  277. fX += v.fX;
  278. fY += v.fY;
  279. }
  280. /** Subtracts vector v from SkPoint. Sets SkPoint to: (fX - v.fX, fY - v.fY).
  281. @param v vector to subtract
  282. */
  283. void operator-=(const SkVector& v) {
  284. fX -= v.fX;
  285. fY -= v.fY;
  286. }
  287. /** Returns SkPoint multiplied by scale.
  288. @param scale scalar to multiply by
  289. @return SkPoint as (fX * scale, fY * scale)
  290. */
  291. SkPoint operator*(SkScalar scale) const {
  292. return {fX * scale, fY * scale};
  293. }
  294. /** Multiplies SkPoint by scale. Sets SkPoint to: (fX * scale, fY * scale).
  295. @param scale scalar to multiply by
  296. @return reference to SkPoint
  297. */
  298. SkPoint& operator*=(SkScalar scale) {
  299. fX *= scale;
  300. fY *= scale;
  301. return *this;
  302. }
  303. /** Returns true if both fX and fY are measurable values.
  304. @return true for values other than infinities and NaN
  305. */
  306. bool isFinite() const {
  307. SkScalar accum = 0;
  308. accum *= fX;
  309. accum *= fY;
  310. // accum is either NaN or it is finite (zero).
  311. SkASSERT(0 == accum || SkScalarIsNaN(accum));
  312. // value==value will be true iff value is not NaN
  313. // TODO: is it faster to say !accum or accum==accum?
  314. return !SkScalarIsNaN(accum);
  315. }
  316. /** Returns true if SkPoint is equivalent to SkPoint constructed from (x, y).
  317. @param x value compared with fX
  318. @param y value compared with fY
  319. @return true if SkPoint equals (x, y)
  320. */
  321. bool equals(SkScalar x, SkScalar y) const {
  322. return fX == x && fY == y;
  323. }
  324. /** Returns true if a is equivalent to b.
  325. @param a SkPoint to compare
  326. @param b SkPoint to compare
  327. @return true if a.fX == b.fX and a.fY == b.fY
  328. */
  329. friend bool operator==(const SkPoint& a, const SkPoint& b) {
  330. return a.fX == b.fX && a.fY == b.fY;
  331. }
  332. /** Returns true if a is not equivalent to b.
  333. @param a SkPoint to compare
  334. @param b SkPoint to compare
  335. @return true if a.fX != b.fX or a.fY != b.fY
  336. */
  337. friend bool operator!=(const SkPoint& a, const SkPoint& b) {
  338. return a.fX != b.fX || a.fY != b.fY;
  339. }
  340. /** Returns vector from b to a, computed as (a.fX - b.fX, a.fY - b.fY).
  341. Can also be used to subtract vector from SkPoint, returning SkPoint.
  342. Can also be used to subtract vector from vector, returning vector.
  343. @param a SkPoint to subtract from
  344. @param b SkPoint to subtract
  345. @return vector from b to a
  346. */
  347. friend SkVector operator-(const SkPoint& a, const SkPoint& b) {
  348. return {a.fX - b.fX, a.fY - b.fY};
  349. }
  350. /** Returns SkPoint resulting from SkPoint a offset by vector b, computed as:
  351. (a.fX + b.fX, a.fY + b.fY).
  352. Can also be used to offset SkPoint b by vector a, returning SkPoint.
  353. Can also be used to add vector to vector, returning vector.
  354. @param a SkPoint or vector to add to
  355. @param b SkPoint or vector to add
  356. @return SkPoint equal to a offset by b
  357. */
  358. friend SkPoint operator+(const SkPoint& a, const SkVector& b) {
  359. return {a.fX + b.fX, a.fY + b.fY};
  360. }
  361. /** Returns the Euclidean distance from origin, computed as:
  362. sqrt(x * x + y * y)
  363. .
  364. @param x component of length
  365. @param y component of length
  366. @return straight-line distance to origin
  367. */
  368. static SkScalar Length(SkScalar x, SkScalar y);
  369. /** Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX
  370. to vec->fY, if possible. If original length is nearly zero, sets vec to (0, 0) and returns
  371. zero; otherwise, returns length of vec before vec is scaled.
  372. Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar.
  373. Note that normalize() is faster if prior length is not required.
  374. @param vec normalized to unit length
  375. @return original vec length
  376. */
  377. static SkScalar Normalize(SkVector* vec);
  378. /** Returns the Euclidean distance between a and b.
  379. @param a line end point
  380. @param b line end point
  381. @return straight-line distance from a to b
  382. */
  383. static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
  384. return Length(a.fX - b.fX, a.fY - b.fY);
  385. }
  386. /** Returns the dot product of vector a and vector b.
  387. @param a left side of dot product
  388. @param b right side of dot product
  389. @return product of input magnitudes and cosine of the angle between them
  390. */
  391. static SkScalar DotProduct(const SkVector& a, const SkVector& b) {
  392. return a.fX * b.fX + a.fY * b.fY;
  393. }
  394. /** Returns the cross product of vector a and vector b.
  395. a and b form three-dimensional vectors with z-axis value equal to zero. The
  396. cross product is a three-dimensional vector with x-axis and y-axis values equal
  397. to zero. The cross product z-axis component is returned.
  398. @param a left side of cross product
  399. @param b right side of cross product
  400. @return area spanned by vectors signed by angle direction
  401. */
  402. static SkScalar CrossProduct(const SkVector& a, const SkVector& b) {
  403. return a.fX * b.fY - a.fY * b.fX;
  404. }
  405. /** Returns the cross product of vector and vec.
  406. Vector and vec form three-dimensional vectors with z-axis value equal to zero.
  407. The cross product is a three-dimensional vector with x-axis and y-axis values
  408. equal to zero. The cross product z-axis component is returned.
  409. @param vec right side of cross product
  410. @return area spanned by vectors signed by angle direction
  411. */
  412. SkScalar cross(const SkVector& vec) const {
  413. return CrossProduct(*this, vec);
  414. }
  415. /** Returns the dot product of vector and vector vec.
  416. @param vec right side of dot product
  417. @return product of input magnitudes and cosine of the angle between them
  418. */
  419. SkScalar dot(const SkVector& vec) const {
  420. return DotProduct(*this, vec);
  421. }
  422. };
  423. #endif