reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 8 | /* Generated by tools/bookmaker from include/core/SkPoint.h and docs/SkPoint_Reference.bmh |
Cary Clark | 9bb34b7 | 2018-09-13 14:08:12 -0400 | [diff] [blame] | 9 | on 2018-09-13 13:59:55. Additional documentation and examples can be found at: |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #ifndef SkPoint_DEFINED |
| 19 | #define SkPoint_DEFINED |
| 20 | |
tomhudson@google.com | c12e1b1 | 2011-09-27 18:03:23 +0000 | [diff] [blame] | 21 | #include "SkMath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkScalar.h" |
Mike Reed | 655bf8f | 2018-02-13 12:48:57 -0500 | [diff] [blame] | 23 | #include "../private/SkSafe32.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 25 | struct SkIPoint; |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 26 | |
| 27 | /** SkIVector provides an alternative name for SkIPoint. SkIVector and SkIPoint |
| 28 | can be used interchangeably for all purposes. |
| 29 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 30 | typedef SkIPoint SkIVector; |
| 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | /** \struct SkIPoint |
Cary Clark | 462505f | 2018-05-30 09:20:29 -0400 | [diff] [blame] | 33 | SkIPoint holds two 32-bit integer coordinates. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | */ |
| 35 | struct SkIPoint { |
Cary Clark | 462505f | 2018-05-30 09:20:29 -0400 | [diff] [blame] | 36 | int32_t fX; //!< x-axis value |
| 37 | int32_t fY; //!< y-axis value |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 45 | static constexpr SkIPoint Make(int32_t x, int32_t y) { |
| 46 | return {x, y}; |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 47 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 49 | /** Returns x-axis value of SkIPoint. |
| 50 | |
| 51 | @return fX |
| 52 | */ |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 53 | int32_t x() const { return fX; } |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 54 | |
| 55 | /** Returns y-axis value of SkIPoint. |
| 56 | |
| 57 | @return fY |
| 58 | */ |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 59 | int32_t y() const { return fY; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 60 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 61 | /** Returns true if fX and fY are both zero. |
| 62 | |
| 63 | @return true if fX is zero and fY is zero |
| 64 | */ |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 65 | bool isZero() const { return (fX | fY) == 0; } |
| 66 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 72 | void set(int32_t x, int32_t y) { |
| 73 | fX = x; |
| 74 | fY = y; |
| 75 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 76 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 77 | /** Returns SkIPoint changing the signs of fX and fY. |
| 78 | |
| 79 | @return SkIPoint as (-fX, -fY) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | */ |
| 81 | SkIPoint operator-() const { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 82 | return {-fX, -fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 85 | /** Offsets SkIPoint by ivector v. Sets SkIPoint to (fX + v.fX, fY + v.fY). |
| 86 | |
| 87 | @param v ivector to add |
| 88 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 89 | void operator+=(const SkIVector& v) { |
Mike Reed | 655bf8f | 2018-02-13 12:48:57 -0500 | [diff] [blame] | 90 | fX = Sk32_sat_add(fX, v.fX); |
| 91 | fY = Sk32_sat_add(fY, v.fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 93 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 94 | /** Subtracts ivector v from SkIPoint. Sets SkIPoint to: (fX - v.fX, fY - v.fY). |
| 95 | |
| 96 | @param v ivector to subtract |
| 97 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 98 | void operator-=(const SkIVector& v) { |
Mike Reed | 655bf8f | 2018-02-13 12:48:57 -0500 | [diff] [blame] | 99 | fX = Sk32_sat_sub(fX, v.fX); |
| 100 | fY = Sk32_sat_sub(fY, v.fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | bool equals(int32_t x, int32_t y) const { |
| 110 | return fX == x && fY == y; |
| 111 | } |
| 112 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | friend bool operator==(const SkIPoint& a, const SkIPoint& b) { |
| 120 | return a.fX == b.fX && a.fY == b.fY; |
| 121 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 122 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | friend bool operator!=(const SkIPoint& a, const SkIPoint& b) { |
| 130 | return a.fX != b.fX || a.fY != b.fY; |
| 131 | } |
| 132 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 141 | friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) { |
Mike Reed | 655bf8f | 2018-02-13 12:48:57 -0500 | [diff] [blame] | 142 | return { Sk32_sat_sub(a.fX, b.fX), Sk32_sat_sub(a.fY, b.fY) }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Cary Clark | 9bb34b7 | 2018-09-13 14:08:12 -0400 | [diff] [blame] | 145 | /** Returns SkIPoint resulting from SkIPoint a offset by ivector b, computed as: |
| 146 | (a.fX + b.fX, a.fY + b.fY). |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 155 | friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) { |
Mike Reed | 655bf8f | 2018-02-13 12:48:57 -0500 | [diff] [blame] | 156 | return { Sk32_sat_add(a.fX, b.fX), Sk32_sat_add(a.fY, b.fY) }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | } |
| 158 | }; |
| 159 | |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 160 | struct SkPoint; |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 161 | |
| 162 | /** SkVector provides an alternative name for SkPoint. SkVector and SkPoint can |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 163 | be used interchangeably for all purposes. |
Cary Clark | 9149954 | 2018-06-08 11:49:19 -0400 | [diff] [blame] | 164 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 165 | typedef SkPoint SkVector; |
| 166 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 167 | /** \struct SkPoint |
Cary Clark | 462505f | 2018-05-30 09:20:29 -0400 | [diff] [blame] | 168 | SkPoint holds two 32-bit floating point coordinates. |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 169 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 170 | struct SK_API SkPoint { |
Cary Clark | 462505f | 2018-05-30 09:20:29 -0400 | [diff] [blame] | 171 | SkScalar fX; //!< x-axis value |
| 172 | SkScalar fY; //!< y-axis value |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 180 | static constexpr SkPoint Make(SkScalar x, SkScalar y) { |
| 181 | return {x, y}; |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 182 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 183 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 184 | /** Returns x-axis value of SkPoint or vector. |
| 185 | |
| 186 | @return fX |
| 187 | */ |
mike@reedtribe.org | b7d956d | 2011-03-20 20:19:16 +0000 | [diff] [blame] | 188 | SkScalar x() const { return fX; } |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 189 | |
| 190 | /** Returns y-axis value of SkPoint or vector. |
| 191 | |
| 192 | @return fY |
| 193 | */ |
mike@reedtribe.org | b7d956d | 2011-03-20 20:19:16 +0000 | [diff] [blame] | 194 | SkScalar y() const { return fY; } |
| 195 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 196 | /** Returns true if fX and fY are both zero. |
| 197 | |
| 198 | @return true if fX is zero and fY is zero |
| 199 | */ |
mike@reedtribe.org | 398b1bc | 2012-05-29 01:40:15 +0000 | [diff] [blame] | 200 | bool isZero() const { return (0 == fX) & (0 == fY); } |
| 201 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 207 | void set(SkScalar x, SkScalar y) { |
| 208 | fX = x; |
| 209 | fY = y; |
| 210 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 211 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | */ |
| 221 | void iset(int32_t x, int32_t y) { |
| 222 | fX = SkIntToScalar(x); |
| 223 | fY = SkIntToScalar(y); |
| 224 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 225 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | */ |
| 234 | void iset(const SkIPoint& p) { |
| 235 | fX = SkIntToScalar(p.fX); |
| 236 | fY = SkIntToScalar(p.fY); |
| 237 | } |
| 238 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 243 | void setAbs(const SkPoint& pt) { |
| 244 | fX = SkScalarAbs(pt.fX); |
| 245 | fY = SkScalarAbs(pt.fY); |
| 246 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 247 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 254 | static void Offset(SkPoint points[], int count, const SkVector& offset) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 255 | Offset(points, count, offset.fX, offset.fY); |
| 256 | } |
| 257 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 265 | 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 Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 271 | /** Adds offset (dx, dy) to SkPoint. |
| 272 | |
| 273 | @param dx added to fX |
| 274 | @param dy added to fY |
| 275 | */ |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 276 | void offset(SkScalar dx, SkScalar dy) { |
| 277 | fX += dx; |
| 278 | fY += dy; |
| 279 | } |
| 280 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 281 | /** Returns the Euclidean distance from origin, computed as: |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 282 | |
| 283 | sqrt(fX * fX + fY * fY) |
| 284 | |
| 285 | . |
| 286 | |
| 287 | @return straight-line distance to origin |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | */ |
| 289 | SkScalar length() const { return SkPoint::Length(fX, fY); } |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 290 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 291 | /** Returns the Euclidean distance from origin, computed as: |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 292 | |
| 293 | sqrt(fX * fX + fY * fY) |
| 294 | |
| 295 | . |
| 296 | |
| 297 | @return straight-line distance to origin |
| 298 | */ |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 299 | SkScalar distanceToOrigin() const { return this->length(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 306 | */ |
| 307 | bool normalize(); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 308 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | */ |
| 317 | bool setNormalize(SkScalar x, SkScalar y); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 318 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | */ |
| 326 | bool setLength(SkScalar length); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 327 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | */ |
| 337 | bool setLength(SkScalar x, SkScalar y, SkScalar length); |
| 338 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | */ |
| 344 | void scale(SkScalar scale, SkPoint* dst) const; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 345 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 346 | /** Scales SkPoint in place by scale. |
| 347 | |
| 348 | @param value factor to multiply SkPoint by |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | */ |
reed@android.com | fc25abd | 2009-01-15 14:38:33 +0000 | [diff] [blame] | 350 | void scale(SkScalar value) { this->scale(value, this); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 351 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 352 | /** Changes the sign of fX and fY. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | */ |
| 354 | void negate() { |
| 355 | fX = -fX; |
| 356 | fY = -fY; |
| 357 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 358 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 359 | /** Returns SkPoint changing the signs of fX and fY. |
| 360 | |
| 361 | @return SkPoint as (-fX, -fY) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | */ |
| 363 | SkPoint operator-() const { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 364 | return {-fX, -fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 367 | /** Adds vector v to SkPoint. Sets SkPoint to: (fX + v.fX, fY + v.fY). |
| 368 | |
| 369 | @param v vector to add |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 370 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 371 | void operator+=(const SkVector& v) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | fX += v.fX; |
| 373 | fY += v.fY; |
| 374 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 375 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 376 | /** Subtracts vector v from SkPoint. Sets SkPoint to: (fX - v.fX, fY - v.fY). |
| 377 | |
| 378 | @param v vector to subtract |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 380 | void operator-=(const SkVector& v) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | fX -= v.fX; |
| 382 | fY -= v.fY; |
| 383 | } |
| 384 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 385 | /** Returns SkPoint multiplied by scale. |
| 386 | |
| 387 | @param scale scalar to multiply by |
| 388 | @return SkPoint as (fX * scale, fY * scale) |
| 389 | */ |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 390 | SkPoint operator*(SkScalar scale) const { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 391 | return {fX * scale, fY * scale}; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 392 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 393 | |
Cary Clark | 7651c16 | 2018-07-13 08:21:59 -0400 | [diff] [blame] | 394 | /** Multiplies SkPoint by scale. Sets SkPoint to: (fX * scale, fY * scale). |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 395 | |
| 396 | @param scale scalar to multiply by |
| 397 | @return reference to SkPoint |
| 398 | */ |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 399 | SkPoint& operator*=(SkScalar scale) { |
| 400 | fX *= scale; |
| 401 | fY *= scale; |
| 402 | return *this; |
| 403 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 404 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 405 | /** Returns true if both fX and fY are measurable values. |
| 406 | |
| 407 | @return true for values other than infinities and NaN |
| 408 | */ |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 409 | bool isFinite() const { |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 410 | SkScalar accum = 0; |
| 411 | accum *= fX; |
| 412 | accum *= fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 413 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 414 | // accum is either NaN or it is finite (zero). |
ehsan.akhgari | 6f90475 | 2014-12-15 12:08:47 -0800 | [diff] [blame] | 415 | SkASSERT(0 == accum || SkScalarIsNaN(accum)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 416 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 417 | // value==value will be true iff value is not NaN |
| 418 | // TODO: is it faster to say !accum or accum==accum? |
ehsan.akhgari | 6f90475 | 2014-12-15 12:08:47 -0800 | [diff] [blame] | 419 | return !SkScalarIsNaN(accum); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
reed@google.com | 24d10cb | 2013-01-28 22:36:34 +0000 | [diff] [blame] | 428 | bool equals(SkScalar x, SkScalar y) const { |
| 429 | return fX == x && fY == y; |
| 430 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 431 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | friend bool operator==(const SkPoint& a, const SkPoint& b) { |
| 439 | return a.fX == b.fX && a.fY == b.fY; |
| 440 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 441 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 448 | friend bool operator!=(const SkPoint& a, const SkPoint& b) { |
| 449 | return a.fX != b.fX || a.fY != b.fY; |
| 450 | } |
| 451 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 460 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 461 | friend SkVector operator-(const SkPoint& a, const SkPoint& b) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 462 | return {a.fX - b.fX, a.fY - b.fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Cary Clark | 9bb34b7 | 2018-09-13 14:08:12 -0400 | [diff] [blame] | 465 | /** Returns SkPoint resulting from SkPoint a offset by vector b, computed as: |
| 466 | (a.fX + b.fX, a.fY + b.fY). |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 474 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 475 | friend SkPoint operator+(const SkPoint& a, const SkVector& b) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 476 | return {a.fX + b.fX, a.fY + b.fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 479 | /** Returns the Euclidean distance from origin, computed as: |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 488 | */ |
| 489 | static SkScalar Length(SkScalar x, SkScalar y); |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 490 | |
Cary Clark | 8245649 | 2018-10-31 10:54:50 -0400 | [diff] [blame^] | 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. |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 494 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 502 | static SkScalar Normalize(SkVector* vec); |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 503 | |
Cary Clark | ca6a245 | 2018-06-14 13:21:08 -0400 | [diff] [blame] | 504 | /** Returns the Euclidean distance between a and b. |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 505 | |
| 506 | @param a line end point |
| 507 | @param b line end point |
| 508 | @return straight-line distance from a to b |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 509 | */ |
| 510 | static SkScalar Distance(const SkPoint& a, const SkPoint& b) { |
| 511 | return Length(a.fX - b.fX, a.fY - b.fY); |
| 512 | } |
| 513 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 519 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 520 | static SkScalar DotProduct(const SkVector& a, const SkVector& b) { |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 521 | return a.fX * b.fX + a.fY * b.fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 524 | /** Returns the cross product of vector a and vector b. |
| 525 | |
Cary Clark | 7595939 | 2018-02-27 10:22:04 -0500 | [diff] [blame] | 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. |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 534 | static SkScalar CrossProduct(const SkVector& a, const SkVector& b) { |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 535 | return a.fX * b.fY - a.fY * b.fX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | } |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 537 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 538 | /** Returns the cross product of vector and vec. |
| 539 | |
Cary Clark | 7595939 | 2018-02-27 10:22:04 -0500 | [diff] [blame] | 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. |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 543 | |
| 544 | @param vec right side of cross product |
| 545 | @return area spanned by vectors signed by angle direction |
| 546 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 547 | SkScalar cross(const SkVector& vec) const { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 548 | return CrossProduct(*this, vec); |
| 549 | } |
| 550 | |
Cary Clark | 2823f9f | 2018-01-03 10:00:34 -0500 | [diff] [blame] | 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 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame] | 556 | SkScalar dot(const SkVector& vec) const { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 557 | return DotProduct(*this, vec); |
| 558 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 559 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 560 | }; |
| 561 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 562 | #endif |