SkPoint.h 17 KB

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