blob: 1c4768f566d8e253339a91a5fe7a6085227dcbcc [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
Cary Clark91499542018-06-08 11:49:19 -04008/* Generated by tools/bookmaker from include/core/SkPoint.h and docs/SkPoint_Reference.bmh
Cary Clark9bb34b72018-09-13 14:08:12 -04009 on 2018-09-13 13:59:55. Additional documentation and examples can be found at:
Cary Clark91499542018-06-08 11:49:19 -040010 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
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#ifndef SkPoint_DEFINED
19#define SkPoint_DEFINED
20
tomhudson@google.comc12e1b12011-09-27 18:03:23 +000021#include "SkMath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkScalar.h"
Mike Reed655bf8f2018-02-13 12:48:57 -050023#include "../private/SkSafe32.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
Cary Clarkba2526b2017-11-09 16:03:40 -050025struct SkIPoint;
Cary Clark91499542018-06-08 11:49:19 -040026
27/** SkIVector provides an alternative name for SkIPoint. SkIVector and SkIPoint
28 can be used interchangeably for all purposes.
29*/
Cary Clarkba2526b2017-11-09 16:03:40 -050030typedef SkIPoint SkIVector;
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032/** \struct SkIPoint
Cary Clark462505f2018-05-30 09:20:29 -040033 SkIPoint holds two 32-bit integer coordinates.
reed@android.com8a1c16f2008-12-17 15:59:43 +000034*/
35struct SkIPoint {
Cary Clark462505f2018-05-30 09:20:29 -040036 int32_t fX; //!< x-axis value
37 int32_t fY; //!< y-axis value
Cary Clark2823f9f2018-01-03 10:00:34 -050038
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 */
Cary Clarkdf429f32017-11-08 11:44:31 -050045 static constexpr SkIPoint Make(int32_t x, int32_t y) {
46 return {x, y};
reed@android.comac753092010-01-28 21:34:33 +000047 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
Cary Clark2823f9f2018-01-03 10:00:34 -050049 /** Returns x-axis value of SkIPoint.
50
51 @return fX
52 */
reed@google.com6f8f2922011-03-04 22:27:10 +000053 int32_t x() const { return fX; }
Cary Clark2823f9f2018-01-03 10:00:34 -050054
55 /** Returns y-axis value of SkIPoint.
56
57 @return fY
58 */
reed@google.com6f8f2922011-03-04 22:27:10 +000059 int32_t y() const { return fY; }
reed@google.com6f8f2922011-03-04 22:27:10 +000060
Cary Clark2823f9f2018-01-03 10:00:34 -050061 /** Returns true if fX and fY are both zero.
62
63 @return true if fX is zero and fY is zero
64 */
reed@google.com6f8f2922011-03-04 22:27:10 +000065 bool isZero() const { return (fX | fY) == 0; }
66
Cary Clark2823f9f2018-01-03 10:00:34 -050067 /** Sets fX to x and fY to y.
68
69 @param x new value for fX
70 @param y new value for fY
71 */
Cary Clarkdf429f32017-11-08 11:44:31 -050072 void set(int32_t x, int32_t y) {
73 fX = x;
74 fY = y;
75 }
reed@google.com6f8f2922011-03-04 22:27:10 +000076
Cary Clark2823f9f2018-01-03 10:00:34 -050077 /** Returns SkIPoint changing the signs of fX and fY.
78
79 @return SkIPoint as (-fX, -fY)
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 */
81 SkIPoint operator-() const {
Cary Clarkdf429f32017-11-08 11:44:31 -050082 return {-fX, -fY};
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 }
84
Cary Clark2823f9f2018-01-03 10:00:34 -050085 /** Offsets SkIPoint by ivector v. Sets SkIPoint to (fX + v.fX, fY + v.fY).
86
87 @param v ivector to add
88 */
Cary Clarkba2526b2017-11-09 16:03:40 -050089 void operator+=(const SkIVector& v) {
Mike Reed655bf8f2018-02-13 12:48:57 -050090 fX = Sk32_sat_add(fX, v.fX);
91 fY = Sk32_sat_add(fY, v.fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 }
reed@google.com6f8f2922011-03-04 22:27:10 +000093
Cary Clark2823f9f2018-01-03 10:00:34 -050094 /** Subtracts ivector v from SkIPoint. Sets SkIPoint to: (fX - v.fX, fY - v.fY).
95
96 @param v ivector to subtract
97 */
Cary Clarkba2526b2017-11-09 16:03:40 -050098 void operator-=(const SkIVector& v) {
Mike Reed655bf8f2018-02-13 12:48:57 -050099 fX = Sk32_sat_sub(fX, v.fX);
100 fY = Sk32_sat_sub(fY, v.fY);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 }
102
Cary Clark2823f9f2018-01-03 10:00:34 -0500103 /** 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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 bool equals(int32_t x, int32_t y) const {
110 return fX == x && fY == y;
111 }
112
Cary Clark2823f9f2018-01-03 10:00:34 -0500113 /** 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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 friend bool operator==(const SkIPoint& a, const SkIPoint& b) {
120 return a.fX == b.fX && a.fY == b.fY;
121 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000122
Cary Clark2823f9f2018-01-03 10:00:34 -0500123 /** 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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 friend bool operator!=(const SkIPoint& a, const SkIPoint& b) {
130 return a.fX != b.fX || a.fY != b.fY;
131 }
132
Cary Clark2823f9f2018-01-03 10:00:34 -0500133 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500141 friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) {
Mike Reed655bf8f2018-02-13 12:48:57 -0500142 return { Sk32_sat_sub(a.fX, b.fX), Sk32_sat_sub(a.fY, b.fY) };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 }
144
Cary Clark9bb34b72018-09-13 14:08:12 -0400145 /** Returns SkIPoint resulting from SkIPoint a offset by ivector b, computed as:
146 (a.fX + b.fX, a.fY + b.fY).
Cary Clark2823f9f2018-01-03 10:00:34 -0500147
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500155 friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) {
Mike Reed655bf8f2018-02-13 12:48:57 -0500156 return { Sk32_sat_add(a.fX, b.fX), Sk32_sat_add(a.fY, b.fY) };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 }
158};
159
Cary Clarkba2526b2017-11-09 16:03:40 -0500160struct SkPoint;
Cary Clark91499542018-06-08 11:49:19 -0400161
162/** SkVector provides an alternative name for SkPoint. SkVector and SkPoint can
Cary Clarkca6a2452018-06-14 13:21:08 -0400163 be used interchangeably for all purposes.
Cary Clark91499542018-06-08 11:49:19 -0400164*/
Cary Clarkba2526b2017-11-09 16:03:40 -0500165typedef SkPoint SkVector;
166
Cary Clark2823f9f2018-01-03 10:00:34 -0500167/** \struct SkPoint
Cary Clark462505f2018-05-30 09:20:29 -0400168 SkPoint holds two 32-bit floating point coordinates.
Cary Clark2823f9f2018-01-03 10:00:34 -0500169*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000170struct SK_API SkPoint {
Cary Clark462505f2018-05-30 09:20:29 -0400171 SkScalar fX; //!< x-axis value
172 SkScalar fY; //!< y-axis value
Cary Clark2823f9f2018-01-03 10:00:34 -0500173
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 */
Cary Clarkdf429f32017-11-08 11:44:31 -0500180 static constexpr SkPoint Make(SkScalar x, SkScalar y) {
181 return {x, y};
reed@android.comac753092010-01-28 21:34:33 +0000182 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000183
Cary Clark2823f9f2018-01-03 10:00:34 -0500184 /** Returns x-axis value of SkPoint or vector.
185
186 @return fX
187 */
mike@reedtribe.orgb7d956d2011-03-20 20:19:16 +0000188 SkScalar x() const { return fX; }
Cary Clark2823f9f2018-01-03 10:00:34 -0500189
190 /** Returns y-axis value of SkPoint or vector.
191
192 @return fY
193 */
mike@reedtribe.orgb7d956d2011-03-20 20:19:16 +0000194 SkScalar y() const { return fY; }
195
Cary Clark2823f9f2018-01-03 10:00:34 -0500196 /** Returns true if fX and fY are both zero.
197
198 @return true if fX is zero and fY is zero
199 */
mike@reedtribe.org398b1bc2012-05-29 01:40:15 +0000200 bool isZero() const { return (0 == fX) & (0 == fY); }
201
Cary Clark2823f9f2018-01-03 10:00:34 -0500202 /** Sets fX to x and fY to y.
203
204 @param x new value for fX
205 @param y new value for fY
206 */
Cary Clarkdf429f32017-11-08 11:44:31 -0500207 void set(SkScalar x, SkScalar y) {
208 fX = x;
209 fY = y;
210 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000211
Cary Clark2823f9f2018-01-03 10:00:34 -0500212 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 */
221 void iset(int32_t x, int32_t y) {
222 fX = SkIntToScalar(x);
223 fY = SkIntToScalar(y);
224 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000225
Cary Clark2823f9f2018-01-03 10:00:34 -0500226 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 */
234 void iset(const SkIPoint& p) {
235 fX = SkIntToScalar(p.fX);
236 fY = SkIntToScalar(p.fY);
237 }
238
Cary Clark2823f9f2018-01-03 10:00:34 -0500239 /** 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 */
reed@google.com7744c202011-05-06 19:26:26 +0000243 void setAbs(const SkPoint& pt) {
244 fX = SkScalarAbs(pt.fX);
245 fY = SkScalarAbs(pt.fY);
246 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000247
Cary Clark2823f9f2018-01-03 10:00:34 -0500248 /** 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 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500254 static void Offset(SkPoint points[], int count, const SkVector& offset) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000255 Offset(points, count, offset.fX, offset.fY);
256 }
257
Cary Clark2823f9f2018-01-03 10:00:34 -0500258 /** 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 */
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000265 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
Cary Clark2823f9f2018-01-03 10:00:34 -0500271 /** Adds offset (dx, dy) to SkPoint.
272
273 @param dx added to fX
274 @param dy added to fY
275 */
reed@google.com7744c202011-05-06 19:26:26 +0000276 void offset(SkScalar dx, SkScalar dy) {
277 fX += dx;
278 fY += dy;
279 }
280
Cary Clarkca6a2452018-06-14 13:21:08 -0400281 /** Returns the Euclidean distance from origin, computed as:
Cary Clark2823f9f2018-01-03 10:00:34 -0500282
283 sqrt(fX * fX + fY * fY)
284
285 .
286
287 @return straight-line distance to origin
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 */
289 SkScalar length() const { return SkPoint::Length(fX, fY); }
Cary Clark2823f9f2018-01-03 10:00:34 -0500290
Cary Clarkca6a2452018-06-14 13:21:08 -0400291 /** Returns the Euclidean distance from origin, computed as:
Cary Clark2823f9f2018-01-03 10:00:34 -0500292
293 sqrt(fX * fX + fY * fY)
294
295 .
296
297 @return straight-line distance to origin
298 */
reed@google.com7744c202011-05-06 19:26:26 +0000299 SkScalar distanceToOrigin() const { return this->length(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300
Cary Clark2823f9f2018-01-03 10:00:34 -0500301 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 */
307 bool normalize();
reed@google.com6f8f2922011-03-04 22:27:10 +0000308
Cary Clark2823f9f2018-01-03 10:00:34 -0500309 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 */
317 bool setNormalize(SkScalar x, SkScalar y);
reed@google.com6f8f2922011-03-04 22:27:10 +0000318
Cary Clark2823f9f2018-01-03 10:00:34 -0500319 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 */
326 bool setLength(SkScalar length);
reed@google.com6f8f2922011-03-04 22:27:10 +0000327
Cary Clark2823f9f2018-01-03 10:00:34 -0500328 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336 */
337 bool setLength(SkScalar x, SkScalar y, SkScalar length);
338
Cary Clark2823f9f2018-01-03 10:00:34 -0500339 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 */
344 void scale(SkScalar scale, SkPoint* dst) const;
reed@google.com6f8f2922011-03-04 22:27:10 +0000345
Cary Clark2823f9f2018-01-03 10:00:34 -0500346 /** Scales SkPoint in place by scale.
347
348 @param value factor to multiply SkPoint by
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 */
reed@android.comfc25abd2009-01-15 14:38:33 +0000350 void scale(SkScalar value) { this->scale(value, this); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351
Cary Clark2823f9f2018-01-03 10:00:34 -0500352 /** Changes the sign of fX and fY.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 */
354 void negate() {
355 fX = -fX;
356 fY = -fY;
357 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000358
Cary Clark2823f9f2018-01-03 10:00:34 -0500359 /** Returns SkPoint changing the signs of fX and fY.
360
361 @return SkPoint as (-fX, -fY)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 */
363 SkPoint operator-() const {
Cary Clarkdf429f32017-11-08 11:44:31 -0500364 return {-fX, -fY};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 }
366
Cary Clark2823f9f2018-01-03 10:00:34 -0500367 /** Adds vector v to SkPoint. Sets SkPoint to: (fX + v.fX, fY + v.fY).
368
369 @param v vector to add
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500371 void operator+=(const SkVector& v) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 fX += v.fX;
373 fY += v.fY;
374 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000375
Cary Clark2823f9f2018-01-03 10:00:34 -0500376 /** Subtracts vector v from SkPoint. Sets SkPoint to: (fX - v.fX, fY - v.fY).
377
378 @param v vector to subtract
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500380 void operator-=(const SkVector& v) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 fX -= v.fX;
382 fY -= v.fY;
383 }
384
Cary Clark2823f9f2018-01-03 10:00:34 -0500385 /** Returns SkPoint multiplied by scale.
386
387 @param scale scalar to multiply by
388 @return SkPoint as (fX * scale, fY * scale)
389 */
reed80ea19c2015-05-12 10:37:34 -0700390 SkPoint operator*(SkScalar scale) const {
Cary Clarkdf429f32017-11-08 11:44:31 -0500391 return {fX * scale, fY * scale};
reed80ea19c2015-05-12 10:37:34 -0700392 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400393
Cary Clark7651c162018-07-13 08:21:59 -0400394 /** Multiplies SkPoint by scale. Sets SkPoint to: (fX * scale, fY * scale).
Cary Clark2823f9f2018-01-03 10:00:34 -0500395
396 @param scale scalar to multiply by
397 @return reference to SkPoint
398 */
reed80ea19c2015-05-12 10:37:34 -0700399 SkPoint& operator*=(SkScalar scale) {
400 fX *= scale;
401 fY *= scale;
402 return *this;
403 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400404
Cary Clark2823f9f2018-01-03 10:00:34 -0500405 /** Returns true if both fX and fY are measurable values.
406
407 @return true for values other than infinities and NaN
408 */
reed@google.com0bb18bb2012-07-26 15:20:36 +0000409 bool isFinite() const {
reed@google.com0bb18bb2012-07-26 15:20:36 +0000410 SkScalar accum = 0;
411 accum *= fX;
412 accum *= fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000413
reed@google.com0bb18bb2012-07-26 15:20:36 +0000414 // accum is either NaN or it is finite (zero).
ehsan.akhgari6f904752014-12-15 12:08:47 -0800415 SkASSERT(0 == accum || SkScalarIsNaN(accum));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000416
reed@google.com0bb18bb2012-07-26 15:20:36 +0000417 // value==value will be true iff value is not NaN
418 // TODO: is it faster to say !accum or accum==accum?
ehsan.akhgari6f904752014-12-15 12:08:47 -0800419 return !SkScalarIsNaN(accum);
reed@google.com0bb18bb2012-07-26 15:20:36 +0000420 }
421
Cary Clark2823f9f2018-01-03 10:00:34 -0500422 /** 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 */
reed@google.com24d10cb2013-01-28 22:36:34 +0000428 bool equals(SkScalar x, SkScalar y) const {
429 return fX == x && fY == y;
430 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431
Cary Clark2823f9f2018-01-03 10:00:34 -0500432 /** 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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 friend bool operator==(const SkPoint& a, const SkPoint& b) {
439 return a.fX == b.fX && a.fY == b.fY;
440 }
reed@google.com6f8f2922011-03-04 22:27:10 +0000441
Cary Clark2823f9f2018-01-03 10:00:34 -0500442 /** 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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 friend bool operator!=(const SkPoint& a, const SkPoint& b) {
449 return a.fX != b.fX || a.fY != b.fY;
450 }
451
Cary Clark2823f9f2018-01-03 10:00:34 -0500452 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500461 friend SkVector operator-(const SkPoint& a, const SkPoint& b) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500462 return {a.fX - b.fX, a.fY - b.fY};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463 }
464
Cary Clark9bb34b72018-09-13 14:08:12 -0400465 /** Returns SkPoint resulting from SkPoint a offset by vector b, computed as:
466 (a.fX + b.fX, a.fY + b.fY).
Cary Clark2823f9f2018-01-03 10:00:34 -0500467
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500475 friend SkPoint operator+(const SkPoint& a, const SkVector& b) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500476 return {a.fX + b.fX, a.fY + b.fY};
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477 }
478
Cary Clarkca6a2452018-06-14 13:21:08 -0400479 /** Returns the Euclidean distance from origin, computed as:
Cary Clark2823f9f2018-01-03 10:00:34 -0500480
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488 */
489 static SkScalar Length(SkScalar x, SkScalar y);
reed@android.comac753092010-01-28 21:34:33 +0000490
Cary Clark82456492018-10-31 10:54:50 -0400491 /** 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.
epoger@google.com1fd56dc2011-06-15 18:04:58 +0000494
Cary Clark2823f9f2018-01-03 10:00:34 -0500495 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 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500502 static SkScalar Normalize(SkVector* vec);
reed@android.comac753092010-01-28 21:34:33 +0000503
Cary Clarkca6a2452018-06-14 13:21:08 -0400504 /** Returns the Euclidean distance between a and b.
Cary Clark2823f9f2018-01-03 10:00:34 -0500505
506 @param a line end point
507 @param b line end point
508 @return straight-line distance from a to b
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 */
510 static SkScalar Distance(const SkPoint& a, const SkPoint& b) {
511 return Length(a.fX - b.fX, a.fY - b.fY);
512 }
513
Cary Clark2823f9f2018-01-03 10:00:34 -0500514 /** 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500520 static SkScalar DotProduct(const SkVector& a, const SkVector& b) {
reed@google.com1a5e51f2014-01-27 13:41:02 +0000521 return a.fX * b.fX + a.fY * b.fY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000522 }
523
Cary Clark2823f9f2018-01-03 10:00:34 -0500524 /** Returns the cross product of vector a and vector b.
525
Cary Clark75959392018-02-27 10:22:04 -0500526 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.
Cary Clark2823f9f2018-01-03 10:00:34 -0500529
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500534 static SkScalar CrossProduct(const SkVector& a, const SkVector& b) {
reed@google.com1a5e51f2014-01-27 13:41:02 +0000535 return a.fX * b.fY - a.fY * b.fX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 }
reed@google.com7744c202011-05-06 19:26:26 +0000537
Cary Clark2823f9f2018-01-03 10:00:34 -0500538 /** Returns the cross product of vector and vec.
539
Cary Clark75959392018-02-27 10:22:04 -0500540 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.
Cary Clark2823f9f2018-01-03 10:00:34 -0500543
544 @param vec right side of cross product
545 @return area spanned by vectors signed by angle direction
546 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500547 SkScalar cross(const SkVector& vec) const {
reed@google.com7744c202011-05-06 19:26:26 +0000548 return CrossProduct(*this, vec);
549 }
550
Cary Clark2823f9f2018-01-03 10:00:34 -0500551 /** 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 */
Cary Clarkba2526b2017-11-09 16:03:40 -0500556 SkScalar dot(const SkVector& vec) const {
reed@google.com7744c202011-05-06 19:26:26 +0000557 return DotProduct(*this, vec);
558 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000559
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560};
561
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562#endif