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 | |
| 8 | #ifndef SkPoint_DEFINED |
| 9 | #define SkPoint_DEFINED |
| 10 | |
tomhudson@google.com | c12e1b1 | 2011-09-27 18:03:23 +0000 | [diff] [blame] | 11 | #include "SkMath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkScalar.h" |
| 13 | |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 14 | /** \struct SkIPoint16 |
| 15 | |
| 16 | SkIPoint holds two 16 bit integer coordinates |
| 17 | */ |
| 18 | struct SkIPoint16 { |
Cary Clark | 60aaeb2 | 2017-11-03 08:06:09 -0400 | [diff] [blame] | 19 | int16_t fX; |
| 20 | int16_t fY; |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 21 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 22 | static constexpr SkIPoint16 Make(int x, int y) { |
| 23 | return {SkToS16(x), SkToS16(y)}; |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | int16_t x() const { return fX; } |
| 27 | int16_t y() const { return fY; } |
| 28 | |
| 29 | void set(int x, int y) { |
| 30 | fX = SkToS16(x); |
| 31 | fY = SkToS16(y); |
| 32 | } |
| 33 | }; |
| 34 | |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 35 | struct SkIPoint; |
| 36 | typedef SkIPoint SkIVector; |
| 37 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | /** \struct SkIPoint |
| 39 | |
| 40 | SkIPoint holds two 32 bit integer coordinates |
| 41 | */ |
| 42 | struct SkIPoint { |
Cary Clark | 60aaeb2 | 2017-11-03 08:06:09 -0400 | [diff] [blame] | 43 | int32_t fX; |
| 44 | int32_t fY; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 45 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 46 | static constexpr SkIPoint Make(int32_t x, int32_t y) { |
| 47 | return {x, y}; |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 48 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 50 | int32_t x() const { return fX; } |
| 51 | int32_t y() const { return fY; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Returns true iff fX and fY are both zero. |
| 55 | */ |
| 56 | bool isZero() const { return (fX | fY) == 0; } |
| 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | /** Set the x and y values of the point. */ |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 59 | void set(int32_t x, int32_t y) { |
| 60 | fX = x; |
| 61 | fY = y; |
| 62 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | /** Return a new point whose X and Y coordinates are the negative of the |
| 65 | original point's |
| 66 | */ |
| 67 | SkIPoint operator-() const { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 68 | return {-fX, -fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** Add v's coordinates to this point's */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 72 | void operator+=(const SkIVector& v) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | fX += v.fX; |
| 74 | fY += v.fY; |
| 75 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 76 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | /** Subtract v's coordinates from this point's */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 78 | void operator-=(const SkIVector& v) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | fX -= v.fX; |
| 80 | fY -= v.fY; |
| 81 | } |
| 82 | |
| 83 | /** Returns true if the point's coordinates equal (x,y) */ |
| 84 | bool equals(int32_t x, int32_t y) const { |
| 85 | return fX == x && fY == y; |
| 86 | } |
| 87 | |
| 88 | friend bool operator==(const SkIPoint& a, const SkIPoint& b) { |
| 89 | return a.fX == b.fX && a.fY == b.fY; |
| 90 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | friend bool operator!=(const SkIPoint& a, const SkIPoint& b) { |
| 93 | return a.fX != b.fX || a.fY != b.fY; |
| 94 | } |
| 95 | |
| 96 | /** Returns a new point whose coordinates are the difference between |
| 97 | a and b (i.e. a - b) |
| 98 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 99 | friend SkIVector operator-(const SkIPoint& a, const SkIPoint& b) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 100 | return {a.fX - b.fX, a.fY - b.fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** Returns a new point whose coordinates are the sum of a and b (a + b) |
| 104 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 105 | friend SkIPoint operator+(const SkIPoint& a, const SkIVector& b) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 106 | return {a.fX + b.fX, a.fY + b.fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |
| 108 | }; |
| 109 | |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 110 | struct SkPoint; |
| 111 | typedef SkPoint SkVector; |
| 112 | |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 113 | struct SK_API SkPoint { |
Cary Clark | 60aaeb2 | 2017-11-03 08:06:09 -0400 | [diff] [blame] | 114 | SkScalar fX; |
| 115 | SkScalar fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 117 | static constexpr SkPoint Make(SkScalar x, SkScalar y) { |
| 118 | return {x, y}; |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 119 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 120 | |
mike@reedtribe.org | b7d956d | 2011-03-20 20:19:16 +0000 | [diff] [blame] | 121 | SkScalar x() const { return fX; } |
| 122 | SkScalar y() const { return fY; } |
| 123 | |
mike@reedtribe.org | 398b1bc | 2012-05-29 01:40:15 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Returns true iff fX and fY are both zero. |
| 126 | */ |
| 127 | bool isZero() const { return (0 == fX) & (0 == fY); } |
| 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | /** Set the point's X and Y coordinates */ |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 130 | void set(SkScalar x, SkScalar y) { |
| 131 | fX = x; |
| 132 | fY = y; |
| 133 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 134 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | /** Set the point's X and Y coordinates by automatically promoting (x,y) to |
| 136 | SkScalar values. |
| 137 | */ |
| 138 | void iset(int32_t x, int32_t y) { |
| 139 | fX = SkIntToScalar(x); |
| 140 | fY = SkIntToScalar(y); |
| 141 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | /** Set the point's X and Y coordinates by automatically promoting p's |
| 144 | coordinates to SkScalar values. |
| 145 | */ |
| 146 | void iset(const SkIPoint& p) { |
| 147 | fX = SkIntToScalar(p.fX); |
| 148 | fY = SkIntToScalar(p.fY); |
| 149 | } |
| 150 | |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 151 | void setAbs(const SkPoint& pt) { |
| 152 | fX = SkScalarAbs(pt.fX); |
| 153 | fY = SkScalarAbs(pt.fY); |
| 154 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 155 | |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 156 | static void Offset(SkPoint points[], int count, const SkVector& offset) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 157 | Offset(points, count, offset.fX, offset.fY); |
| 158 | } |
| 159 | |
| 160 | static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) { |
| 161 | for (int i = 0; i < count; ++i) { |
| 162 | points[i].offset(dx, dy); |
| 163 | } |
| 164 | } |
| 165 | |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 166 | void offset(SkScalar dx, SkScalar dy) { |
| 167 | fX += dx; |
| 168 | fY += dy; |
| 169 | } |
| 170 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | /** Return the euclidian distance from (0,0) to the point |
| 172 | */ |
| 173 | SkScalar length() const { return SkPoint::Length(fX, fY); } |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 174 | SkScalar distanceToOrigin() const { return this->length(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | |
| 176 | /** Set the point (vector) to be unit-length in the same direction as it |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 177 | already points. If the point has a degenerate length (i.e. nearly 0) |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 178 | then set it to (0,0) and return false; otherwise return true. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | */ |
| 180 | bool normalize(); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 181 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | /** Set the point (vector) to be unit-length in the same direction as the |
| 183 | x,y params. If the vector (x,y) has a degenerate length (i.e. nearly 0) |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 184 | then set it to (0,0) and return false, otherwise return true. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | */ |
| 186 | bool setNormalize(SkScalar x, SkScalar y); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 187 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | /** Scale the point (vector) to have the specified length, and return that |
| 189 | length. If the original length is degenerately small (nearly zero), |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 190 | set it to (0,0) and return false, otherwise return true. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | */ |
| 192 | bool setLength(SkScalar length); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | /** Set the point (vector) to have the specified length in the same |
| 195 | direction as (x,y). If the vector (x,y) has a degenerate length |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 196 | (i.e. nearly 0) then set it to (0,0) and return false, otherwise return true. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | */ |
| 198 | bool setLength(SkScalar x, SkScalar y, SkScalar length); |
| 199 | |
| 200 | /** Scale the point's coordinates by scale, writing the answer into dst. |
| 201 | It is legal for dst == this. |
| 202 | */ |
| 203 | void scale(SkScalar scale, SkPoint* dst) const; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 204 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | /** Scale the point's coordinates by scale, writing the answer back into |
| 206 | the point. |
| 207 | */ |
reed@android.com | fc25abd | 2009-01-15 14:38:33 +0000 | [diff] [blame] | 208 | void scale(SkScalar value) { this->scale(value, this); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | /** Negate the point's coordinates |
| 211 | */ |
| 212 | void negate() { |
| 213 | fX = -fX; |
| 214 | fY = -fY; |
| 215 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 216 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | /** Returns a new point whose coordinates are the negative of the point's |
| 218 | */ |
| 219 | SkPoint operator-() const { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 220 | return {-fX, -fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** Add v's coordinates to the point's |
| 224 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 225 | void operator+=(const SkVector& v) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | fX += v.fX; |
| 227 | fY += v.fY; |
| 228 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 229 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | /** Subtract v's coordinates from the point's |
| 231 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 232 | void operator-=(const SkVector& v) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | fX -= v.fX; |
| 234 | fY -= v.fY; |
| 235 | } |
| 236 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 237 | SkPoint operator*(SkScalar scale) const { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 238 | return {fX * scale, fY * scale}; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 239 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 240 | |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 241 | SkPoint& operator*=(SkScalar scale) { |
| 242 | fX *= scale; |
| 243 | fY *= scale; |
| 244 | return *this; |
| 245 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 246 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 247 | /** |
| 248 | * Returns true if both X and Y are finite (not infinity or NaN) |
| 249 | */ |
| 250 | bool isFinite() const { |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 251 | SkScalar accum = 0; |
| 252 | accum *= fX; |
| 253 | accum *= fY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 254 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 255 | // accum is either NaN or it is finite (zero). |
ehsan.akhgari | 6f90475 | 2014-12-15 12:08:47 -0800 | [diff] [blame] | 256 | SkASSERT(0 == accum || SkScalarIsNaN(accum)); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 257 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 258 | // value==value will be true iff value is not NaN |
| 259 | // TODO: is it faster to say !accum or accum==accum? |
ehsan.akhgari | 6f90475 | 2014-12-15 12:08:47 -0800 | [diff] [blame] | 260 | return !SkScalarIsNaN(accum); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 261 | } |
| 262 | |
reed@google.com | 24d10cb | 2013-01-28 22:36:34 +0000 | [diff] [blame] | 263 | /** |
| 264 | * Returns true if the point's coordinates equal (x,y) |
| 265 | */ |
| 266 | bool equals(SkScalar x, SkScalar y) const { |
| 267 | return fX == x && fY == y; |
| 268 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | |
| 270 | friend bool operator==(const SkPoint& a, const SkPoint& b) { |
| 271 | return a.fX == b.fX && a.fY == b.fY; |
| 272 | } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 273 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 274 | friend bool operator!=(const SkPoint& a, const SkPoint& b) { |
| 275 | return a.fX != b.fX || a.fY != b.fY; |
| 276 | } |
| 277 | |
| 278 | /** Returns a new point whose coordinates are the difference between |
| 279 | a's and b's (a - b) |
| 280 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 281 | friend SkVector operator-(const SkPoint& a, const SkPoint& b) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 282 | return {a.fX - b.fX, a.fY - b.fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** Returns a new point whose coordinates are the sum of a's and b's (a + b) |
| 286 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 287 | friend SkPoint operator+(const SkPoint& a, const SkVector& b) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 288 | return {a.fX + b.fX, a.fY + b.fY}; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** Returns the euclidian distance from (0,0) to (x,y) |
| 292 | */ |
| 293 | static SkScalar Length(SkScalar x, SkScalar y); |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 294 | |
| 295 | /** Normalize pt, returning its previous length. If the prev length is too |
reed | a8b326c | 2014-12-09 11:50:32 -0800 | [diff] [blame] | 296 | small (degenerate), set pt to (0,0) and return 0. This uses the same |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 297 | tolerance as CanNormalize. |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 298 | |
| 299 | Note that this method may be significantly more expensive than |
| 300 | the non-static normalize(), because it has to return the previous length |
| 301 | of the point. If you don't need the previous length, call the |
| 302 | non-static normalize() method instead. |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 303 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 304 | static SkScalar Normalize(SkVector* vec); |
reed@android.com | ac75309 | 2010-01-28 21:34:33 +0000 | [diff] [blame] | 305 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 306 | /** Returns the euclidian distance between a and b |
| 307 | */ |
| 308 | static SkScalar Distance(const SkPoint& a, const SkPoint& b) { |
| 309 | return Length(a.fX - b.fX, a.fY - b.fY); |
| 310 | } |
| 311 | |
| 312 | /** Returns the dot product of a and b, treating them as 2D vectors |
| 313 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 314 | static SkScalar DotProduct(const SkVector& a, const SkVector& b) { |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 315 | return a.fX * b.fX + a.fY * b.fY; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** Returns the cross product of a and b, treating them as 2D vectors |
| 319 | */ |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 320 | static SkScalar CrossProduct(const SkVector& a, const SkVector& b) { |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 321 | return a.fX * b.fY - a.fY * b.fX; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | } |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 323 | |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 324 | SkScalar cross(const SkVector& vec) const { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 325 | return CrossProduct(*this, vec); |
| 326 | } |
| 327 | |
Cary Clark | ba2526b | 2017-11-09 16:03:40 -0500 | [diff] [blame^] | 328 | SkScalar dot(const SkVector& vec) const { |
reed@google.com | 7744c20 | 2011-05-06 19:26:26 +0000 | [diff] [blame] | 329 | return DotProduct(*this, vec); |
| 330 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 331 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | #endif |