• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* Generated by tools/bookmaker from include/core/SkPoint.h and docs/SkPoint_Reference.bmh
9    on 2018-09-13 13:59:55. Additional documentation and examples can be found at:
10    https://skia.org/user/api/SkPoint_Reference
11 
12    You may edit either file directly. Structural changes to public interfaces require
13    editing both files. After editing docs/SkPoint_Reference.bmh, run:
14        bookmaker -b docs -i include/core/SkPoint.h -p
15    to create an updated version of this file.
16  */
17 
18 #ifndef SkPoint_DEFINED
19 #define SkPoint_DEFINED
20 
21 #include "SkMath.h"
22 #include "SkScalar.h"
23 #include "../private/SkSafe32.h"
24 
25 struct SkIPoint;
26 
27 /** SkIVector provides an alternative name for SkIPoint. SkIVector and SkIPoint
28     can be used interchangeably for all purposes.
29 */
30 typedef SkIPoint SkIVector;
31 
32 /** \struct SkIPoint
33     SkIPoint holds two 32-bit integer coordinates.
34 */
35 struct SkIPoint {
36     int32_t fX; //!< x-axis value
37     int32_t fY; //!< y-axis value
38 
39     /** Sets fX to x, fY to y.
40 
41         @param x  integer x-axis value of constructed SkIPoint
42         @param y  integer y-axis value of constructed SkIPoint
43         @return   SkIPoint (x, y)
44     */
MakeSkIPoint45     static constexpr SkIPoint Make(int32_t x, int32_t y) {
46         return {x, y};
47     }
48 
49     /** Returns x-axis value of SkIPoint.
50 
51         @return  fX
52     */
xSkIPoint53     int32_t x() const { return fX; }
54 
55     /** Returns y-axis value of SkIPoint.
56 
57         @return  fY
58     */
ySkIPoint59     int32_t y() const { return fY; }
60 
61     /** Returns true if fX and fY are both zero.
62 
63         @return  true if fX is zero and fY is zero
64     */
isZeroSkIPoint65     bool isZero() const { return (fX | fY) == 0; }
66 
67     /** Sets fX to x and fY to y.
68 
69         @param x  new value for fX
70         @param y  new value for fY
71     */
setSkIPoint72     void set(int32_t x, int32_t y) {
73         fX = x;
74         fY = y;
75     }
76 
77     /** Returns SkIPoint changing the signs of fX and fY.
78 
79         @return  SkIPoint as (-fX, -fY)
80     */
81     SkIPoint operator-() const {
82         return {-fX, -fY};
83     }
84 
85     /** Offsets SkIPoint by ivector v. Sets SkIPoint to (fX + v.fX, fY + v.fY).
86 
87         @param v  ivector to add
88     */
89     void operator+=(const SkIVector& v) {
90         fX = Sk32_sat_add(fX, v.fX);
91         fY = Sk32_sat_add(fY, v.fY);
92     }
93 
94     /** Subtracts ivector v from SkIPoint. Sets SkIPoint to: (fX - v.fX, fY - v.fY).
95 
96         @param v  ivector to subtract
97     */
98     void operator-=(const SkIVector& v) {
99         fX = Sk32_sat_sub(fX, v.fX);
100         fY = Sk32_sat_sub(fY, v.fY);
101     }
102 
103     /** Returns true if SkIPoint is equivalent to SkIPoint constructed from (x, y).
104 
105         @param x  value compared with fX
106         @param y  value compared with fY
107         @return   true if SkIPoint equals (x, y)
108     */
equalsSkIPoint109     bool equals(int32_t x, int32_t y) const {
110         return fX == x && fY == y;
111     }
112 
113     /** Returns true if a is equivalent to b.
114 
115         @param a  SkIPoint to compare
116         @param b  SkIPoint to compare
117         @return   true if a.fX == b.fX and a.fY == b.fY
118     */
119     friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
120         return a.fX == b.fX && a.fY == b.fY;
121     }
122 
123     /** Returns true if a is not equivalent to b.
124 
125         @param a  SkIPoint to compare
126         @param b  SkIPoint to compare
127         @return   true if a.fX != b.fX or a.fY != b.fY
128     */
129     friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
130         return a.fX != b.fX || a.fY != b.fY;
131     }
132 
133     /** Returns ivector from b to a; computed as (a.fX - b.fX, a.fY - b.fY).
134 
135         Can also be used to subtract ivector from ivector, returning ivector.
136 
137         @param a  SkIPoint or ivector to subtract from
138         @param b  ivector to subtract
139         @return   ivector from b to a
140     */
141     friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) {
142         return { Sk32_sat_sub(a.fX, b.fX), Sk32_sat_sub(a.fY, b.fY) };
143     }
144 
145     /** Returns SkIPoint resulting from SkIPoint a offset by ivector b, computed as:
146         (a.fX + b.fX, a.fY + b.fY).
147 
148         Can also be used to offset SkIPoint b by ivector a, returning SkIPoint.
149         Can also be used to add ivector to ivector, returning ivector.
150 
151         @param a  SkIPoint or ivector to add to
152         @param b  SkIPoint or ivector to add
153         @return   SkIPoint equal to a offset by b
154     */
155     friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) {
156         return { Sk32_sat_add(a.fX, b.fX), Sk32_sat_add(a.fY, b.fY) };
157     }
158 };
159 
160 struct SkPoint;
161 
162 /** SkVector provides an alternative name for SkPoint. SkVector and SkPoint can
163     be used interchangeably for all purposes.
164 */
165 typedef SkPoint SkVector;
166 
167 /** \struct SkPoint
168     SkPoint holds two 32-bit floating point coordinates.
169 */
170 struct SK_API SkPoint {
171     SkScalar fX; //!< x-axis value
172     SkScalar fY; //!< y-axis value
173 
174     /** Sets fX to x, fY to y. Used both to set SkPoint and vector.
175 
176         @param x  SkScalar x-axis value of constructed SkPoint or vector
177         @param y  SkScalar y-axis value of constructed SkPoint or vector
178         @return   SkPoint (x, y)
179     */
MakeSkPoint180     static constexpr SkPoint Make(SkScalar x, SkScalar y) {
181         return {x, y};
182     }
183 
184     /** Returns x-axis value of SkPoint or vector.
185 
186         @return  fX
187     */
xSkPoint188     SkScalar x() const { return fX; }
189 
190     /** Returns y-axis value of SkPoint or vector.
191 
192         @return  fY
193     */
ySkPoint194     SkScalar y() const { return fY; }
195 
196     /** Returns true if fX and fY are both zero.
197 
198         @return  true if fX is zero and fY is zero
199     */
isZeroSkPoint200     bool isZero() const { return (0 == fX) & (0 == fY); }
201 
202     /** Sets fX to x and fY to y.
203 
204         @param x  new value for fX
205         @param y  new value for fY
206     */
setSkPoint207     void set(SkScalar x, SkScalar y) {
208         fX = x;
209         fY = y;
210     }
211 
212     /** Sets fX to x and fY to y, promoting integers to SkScalar values.
213 
214         Assigning a large integer value directly to fX or fY may cause a compiler
215         error, triggered by narrowing conversion of int to SkScalar. This safely
216         casts x and y to avoid the error.
217 
218         @param x  new value for fX
219         @param y  new value for fY
220     */
isetSkPoint221     void iset(int32_t x, int32_t y) {
222         fX = SkIntToScalar(x);
223         fY = SkIntToScalar(y);
224     }
225 
226     /** Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values.
227 
228         Assigning an SkIPoint containing a large integer value directly to fX or fY may
229         cause a compiler error, triggered by narrowing conversion of int to SkScalar.
230         This safely casts p.fX and p.fY to avoid the error.
231 
232         @param p  SkIPoint members promoted to SkScalar
233     */
isetSkPoint234     void iset(const SkIPoint& p) {
235         fX = SkIntToScalar(p.fX);
236         fY = SkIntToScalar(p.fY);
237     }
238 
239     /** Sets fX to absolute value of pt.fX; and fY to absolute value of pt.fY.
240 
241         @param pt  members providing magnitude for fX and fY
242     */
setAbsSkPoint243     void setAbs(const SkPoint& pt) {
244         fX = SkScalarAbs(pt.fX);
245         fY = SkScalarAbs(pt.fY);
246     }
247 
248     /** Adds offset to each SkPoint in points array with count entries.
249 
250         @param points  SkPoint array
251         @param count   entries in array
252         @param offset  vector added to points
253     */
OffsetSkPoint254     static void Offset(SkPoint points[], int count, const SkVector& offset) {
255         Offset(points, count, offset.fX, offset.fY);
256     }
257 
258     /** Adds offset (dx, dy) to each SkPoint in points array of length count.
259 
260         @param points  SkPoint array
261         @param count   entries in array
262         @param dx      added to fX in points
263         @param dy      added to fY in points
264     */
OffsetSkPoint265     static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) {
266         for (int i = 0; i < count; ++i) {
267             points[i].offset(dx, dy);
268         }
269     }
270 
271     /** Adds offset (dx, dy) to SkPoint.
272 
273         @param dx  added to fX
274         @param dy  added to fY
275     */
offsetSkPoint276     void offset(SkScalar dx, SkScalar dy) {
277         fX += dx;
278         fY += dy;
279     }
280 
281     /** Returns the Euclidean distance from origin, computed as:
282 
283             sqrt(fX * fX + fY * fY)
284 
285         .
286 
287         @return  straight-line distance to origin
288     */
lengthSkPoint289     SkScalar length() const { return SkPoint::Length(fX, fY); }
290 
291     /** Returns the Euclidean distance from origin, computed as:
292 
293             sqrt(fX * fX + fY * fY)
294 
295         .
296 
297         @return  straight-line distance to origin
298     */
distanceToOriginSkPoint299     SkScalar distanceToOrigin() const { return this->length(); }
300 
301     /** Scales (fX, fY) so that length() returns one, while preserving ratio of fX to fY,
302         if possible. If prior length is nearly zero, sets vector to (0, 0) and returns
303         false; otherwise returns true.
304 
305         @return  true if former length is not zero or nearly zero
306     */
307     bool normalize();
308 
309     /** Sets vector to (x, y) scaled so length() returns one, and so that
310         (fX, fY) is proportional to (x, y).  If (x, y) length is nearly zero,
311         sets vector to (0, 0) and returns false; otherwise returns true.
312 
313         @param x  proportional value for fX
314         @param y  proportional value for fY
315         @return   true if (x, y) length is not zero or nearly zero
316     */
317     bool setNormalize(SkScalar x, SkScalar y);
318 
319     /** Scales vector so that distanceToOrigin() returns length, if possible. If former
320         length is nearly zero, sets vector to (0, 0) and return false; otherwise returns
321         true.
322 
323         @param length  straight-line distance to origin
324         @return        true if former length is not zero or nearly zero
325     */
326     bool setLength(SkScalar length);
327 
328     /** Sets vector to (x, y) scaled to length, if possible. If former
329         length is nearly zero, sets vector to (0, 0) and return false; otherwise returns
330         true.
331 
332         @param x       proportional value for fX
333         @param y       proportional value for fY
334         @param length  straight-line distance to origin
335         @return        true if (x, y) length is not zero or nearly zero
336     */
337     bool setLength(SkScalar x, SkScalar y, SkScalar length);
338 
339     /** Sets dst to SkPoint times scale. dst may be SkPoint to modify SkPoint in place.
340 
341         @param scale  factor to multiply SkPoint by
342         @param dst    storage for scaled SkPoint
343     */
344     void scale(SkScalar scale, SkPoint* dst) const;
345 
346     /** Scales SkPoint in place by scale.
347 
348         @param value  factor to multiply SkPoint by
349     */
scaleSkPoint350     void scale(SkScalar value) { this->scale(value, this); }
351 
352     /** Changes the sign of fX and fY.
353     */
negateSkPoint354     void negate() {
355         fX = -fX;
356         fY = -fY;
357     }
358 
359     /** Returns SkPoint changing the signs of fX and fY.
360 
361         @return  SkPoint as (-fX, -fY)
362     */
363     SkPoint operator-() const {
364         return {-fX, -fY};
365     }
366 
367     /** Adds vector v to SkPoint. Sets SkPoint to: (fX + v.fX, fY + v.fY).
368 
369         @param v  vector to add
370     */
371     void operator+=(const SkVector& v) {
372         fX += v.fX;
373         fY += v.fY;
374     }
375 
376     /** Subtracts vector v from SkPoint. Sets SkPoint to: (fX - v.fX, fY - v.fY).
377 
378         @param v  vector to subtract
379     */
380     void operator-=(const SkVector& v) {
381         fX -= v.fX;
382         fY -= v.fY;
383     }
384 
385     /** Returns SkPoint multiplied by scale.
386 
387         @param scale  scalar to multiply by
388         @return       SkPoint as (fX * scale, fY * scale)
389     */
390     SkPoint operator*(SkScalar scale) const {
391         return {fX * scale, fY * scale};
392     }
393 
394     /** Multiplies SkPoint by scale. Sets SkPoint to: (fX * scale, fY * scale).
395 
396         @param scale  scalar to multiply by
397         @return       reference to SkPoint
398     */
399     SkPoint& operator*=(SkScalar scale) {
400         fX *= scale;
401         fY *= scale;
402         return *this;
403     }
404 
405     /** Returns true if both fX and fY are measurable values.
406 
407         @return  true for values other than infinities and NaN
408     */
isFiniteSkPoint409     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 
422     /** Returns true if SkPoint is equivalent to SkPoint constructed from (x, y).
423 
424         @param x  value compared with fX
425         @param y  value compared with fY
426         @return   true if SkPoint equals (x, y)
427     */
equalsSkPoint428     bool equals(SkScalar x, SkScalar y) const {
429         return fX == x && fY == y;
430     }
431 
432     /** Returns true if a is equivalent to b.
433 
434         @param a  SkPoint to compare
435         @param b  SkPoint to compare
436         @return   true if a.fX == b.fX and a.fY == b.fY
437     */
438     friend bool operator==(const SkPoint& a, const SkPoint& b) {
439         return a.fX == b.fX && a.fY == b.fY;
440     }
441 
442     /** Returns true if a is not equivalent to b.
443 
444         @param a  SkPoint to compare
445         @param b  SkPoint to compare
446         @return   true if a.fX != b.fX or a.fY != b.fY
447     */
448     friend bool operator!=(const SkPoint& a, const SkPoint& b) {
449         return a.fX != b.fX || a.fY != b.fY;
450     }
451 
452     /** Returns vector from b to a, computed as (a.fX - b.fX, a.fY - b.fY).
453 
454         Can also be used to subtract vector from SkPoint, returning SkPoint.
455         Can also be used to subtract vector from vector, returning vector.
456 
457         @param a  SkPoint to subtract from
458         @param b  SkPoint to subtract
459         @return   vector from b to a
460     */
461     friend SkVector operator-(const SkPoint& a, const SkPoint& b) {
462         return {a.fX - b.fX, a.fY - b.fY};
463     }
464 
465     /** Returns SkPoint resulting from SkPoint a offset by vector b, computed as:
466         (a.fX + b.fX, a.fY + b.fY).
467 
468         Can also be used to offset SkPoint b by vector a, returning SkPoint.
469         Can also be used to add vector to vector, returning vector.
470 
471         @param a  SkPoint or vector to add to
472         @param b  SkPoint or vector to add
473         @return   SkPoint equal to a offset by b
474     */
475     friend SkPoint operator+(const SkPoint& a, const SkVector& b) {
476         return {a.fX + b.fX, a.fY + b.fY};
477     }
478 
479     /** Returns the Euclidean distance from origin, computed as:
480 
481             sqrt(x * x + y * y)
482 
483         .
484 
485         @param x  component of length
486         @param y  component of length
487         @return   straight-line distance to origin
488     */
489     static SkScalar Length(SkScalar x, SkScalar y);
490 
491     /** Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX
492         to vec->fY, if possible. If original length is nearly zero, sets vec to (0, 0) and returns
493         zero; otherwise, returns length of vec before vec is scaled.
494 
495         Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar.
496 
497         Note that normalize() is faster if prior length is not required.
498 
499         @param vec  normalized to unit length
500         @return     original vec length
501     */
502     static SkScalar Normalize(SkVector* vec);
503 
504     /** Returns the Euclidean distance between a and b.
505 
506         @param a  line end point
507         @param b  line end point
508         @return   straight-line distance from a to b
509     */
DistanceSkPoint510     static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
511         return Length(a.fX - b.fX, a.fY - b.fY);
512     }
513 
514     /** Returns the dot product of vector a and vector b.
515 
516         @param a  left side of dot product
517         @param b  right side of dot product
518         @return   product of input magnitudes and cosine of the angle between them
519     */
DotProductSkPoint520     static SkScalar DotProduct(const SkVector& a, const SkVector& b) {
521         return a.fX * b.fX + a.fY * b.fY;
522     }
523 
524     /** Returns the cross product of vector a and vector b.
525 
526         a and b form three-dimensional vectors with z-axis value equal to zero. The
527         cross product is a three-dimensional vector with x-axis and y-axis values equal
528         to zero. The cross product z-axis component is returned.
529 
530         @param a  left side of cross product
531         @param b  right side of cross product
532         @return   area spanned by vectors signed by angle direction
533     */
CrossProductSkPoint534     static SkScalar CrossProduct(const SkVector& a, const SkVector& b) {
535         return a.fX * b.fY - a.fY * b.fX;
536     }
537 
538     /** Returns the cross product of vector and vec.
539 
540         Vector and vec form three-dimensional vectors with z-axis value equal to zero.
541         The cross product is a three-dimensional vector with x-axis and y-axis values
542         equal to zero. The cross product z-axis component is returned.
543 
544         @param vec  right side of cross product
545         @return     area spanned by vectors signed by angle direction
546     */
crossSkPoint547     SkScalar cross(const SkVector& vec) const {
548         return CrossProduct(*this, vec);
549     }
550 
551     /** Returns the dot product of vector and vector vec.
552 
553         @param vec  right side of dot product
554         @return     product of input magnitudes and cosine of the angle between them
555     */
dotSkPoint556     SkScalar dot(const SkVector& vec) const {
557         return DotProduct(*this, vec);
558     }
559 
560 };
561 
562 #endif
563