Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1 | #Topic Point |
| 2 | #Alias Points |
| 3 | #Alias Point_Reference |
| 4 | |
| 5 | #Struct SkPoint |
| 6 | |
| 7 | #Topic Overview |
| 8 | |
| 9 | #Subtopic Subtopics |
| 10 | #ToDo manually add subtopics ## |
| 11 | #Table |
| 12 | #Legend |
| 13 | # topics # description ## |
| 14 | #Legend ## |
| 15 | #Table ## |
| 16 | ## |
| 17 | |
| 18 | #Subtopic Operators |
| 19 | #Table |
| 20 | #Legend |
| 21 | # description # function ## |
| 22 | #Legend ## |
| 23 | # SkPoint operator*(SkScalar scale)_const # Returns Point multiplied by scale. ## |
| 24 | # SkPoint operator-()_const # Reverses sign of Point. ## |
| 25 | # SkPoint& operator*=(SkScalar scale) # Multiplies Point by scale factor. ## |
| 26 | # SkPoint operator+(const SkPoint& a, const SkVector& b) # Returns Point offset by Vector. ## |
| 27 | # SkVector operator-(const SkPoint& a, const SkPoint& b) # Returns Vector between Points. ## |
| 28 | # bool operator!=(const SkPoint& a, const SkPoint& b) # Returns true if Point are unequal. ## |
| 29 | # bool operator==(const SkPoint& a, const SkPoint& b) # Returns true if Point are equal. ## |
| 30 | # void operator+=(const SkVector& v) # Adds Vector to Point. ## |
| 31 | # void operator-=(const SkVector& v) # Subtracts Vector from Point. ## |
| 32 | #Table ## |
| 33 | #Subtopic ## |
| 34 | |
| 35 | #Subtopic Member_Functions |
| 36 | #Table |
| 37 | #Legend |
| 38 | # description # function ## |
| 39 | #Legend ## |
| 40 | # CrossProduct # Returns cross product. ## |
| 41 | # Distance # Returns straight-line distance between points. ## |
| 42 | # DotProduct # Returns dot product. ## |
| 43 | # Length # Returns straight-line distance to origin. ## |
| 44 | # Make # Constructs from SkScalar inputs. ## |
| 45 | # Normalize # Sets length to one, and returns prior length. ## |
| 46 | # Offset # Translates Point array. ## |
| 47 | # cross() # Returns cross product. ## |
| 48 | # distanceToOrigin # Returns straight-line distance to origin. ## |
| 49 | # dot() # Returns dot product. ## |
| 50 | # equals() # Returns true if Points are equal. ## |
| 51 | # isFinite # Returns true if no member is infinite or NaN. ## |
| 52 | # isZero # Returns true if both members equal zero. ## |
| 53 | # iset() # Sets to integer input. ## |
| 54 | # length() # Returns straight-line distance to origin. ## |
| 55 | # negate() # Reverses the sign of both members. ## |
| 56 | # normalize() # Sets length to one, preserving direction. ## |
| 57 | # offset() # Translates Point. ## |
| 58 | # scale() # Multiplies Point by scale factor. ## |
| 59 | # set() # Sets to SkScalar input. ## |
| 60 | # setAbs # Sets sign of both members to positive. ## |
| 61 | # setLength # Sets straight-line distance to origin. ## |
| 62 | # setNormalize # Sets length to one, in direction of (x, y). ## |
| 63 | # x() # Returns fX. ## |
| 64 | # y() # Returns fY. ## |
| 65 | #Table ## |
| 66 | #Subtopic ## |
| 67 | |
| 68 | #Topic ## |
| 69 | |
| 70 | #Member SkScalar fX |
| 71 | x-axis value used by both Point and Vector. May contain any value, including |
| 72 | infinities and NaN. |
| 73 | ## |
| 74 | |
| 75 | #Member SkScalar fY |
| 76 | y-axis value used by both Point and Vector. May contain any value, including |
| 77 | infinities and NaN. |
| 78 | ## |
| 79 | |
| 80 | # ------------------------------------------------------------------------------ |
| 81 | |
| 82 | #Method static constexpr SkPoint Make(SkScalar x, SkScalar y) |
| 83 | |
| 84 | Sets fX to x, fY to y. Used both to set Point and Vector. |
| 85 | |
| 86 | #Param x SkScalar x-axis value of constructed Point or Vector ## |
| 87 | #Param y SkScalar y-axis value of constructed Point or Vector ## |
| 88 | |
| 89 | #Return Point (x, y) ## |
| 90 | |
| 91 | #Example |
| 92 | SkPoint pt1 = {45, 66}; |
| 93 | SkPoint pt2 = SkPoint::Make(45, 66); |
| 94 | SkVector v1 = {45, 66}; |
| 95 | SkVector v2 = SkPoint::Make(45, 66); |
| 96 | SkDebugf("all %s" "equal\n", pt1 == pt2 && pt2 == v1 && v1 == v2 ? "" : "not "); |
| 97 | #StdOut |
| 98 | all equal |
| 99 | ## |
| 100 | ## |
| 101 | |
| 102 | #SeeAlso set() iset() SkIPoint::Make |
| 103 | |
| 104 | #Method ## |
| 105 | |
| 106 | # ------------------------------------------------------------------------------ |
| 107 | |
| 108 | #Method SkScalar x() const |
| 109 | |
| 110 | Returns x-axis value of Point or Vector. |
| 111 | |
| 112 | #Return fX ## |
| 113 | |
| 114 | #Example |
| 115 | SkPoint pt1 = {45, 66}; |
| 116 | SkDebugf("pt1.fX %c= pt1.x()\n", pt1.fX == pt1.x() ? '=' : '!'); |
| 117 | #StdOut |
| 118 | pt1.fX == pt1.x() |
| 119 | ## |
| 120 | ## |
| 121 | |
| 122 | #SeeAlso y() SkIPoint::x() |
| 123 | |
| 124 | #Method ## |
| 125 | |
| 126 | # ------------------------------------------------------------------------------ |
| 127 | |
| 128 | #Method SkScalar y() const |
| 129 | |
| 130 | Returns y-axis value of Point or Vector. |
| 131 | |
| 132 | #Return fY ## |
| 133 | |
| 134 | #Example |
| 135 | SkPoint pt1 = {45, 66}; |
| 136 | SkDebugf("pt1.fY %c= pt1.y()\n", pt1.fY == pt1.y() ? '=' : '!'); |
| 137 | #StdOut |
| 138 | pt1.fY == pt1.y() |
| 139 | ## |
| 140 | ## |
| 141 | |
| 142 | #SeeAlso x() SkIPoint::y() |
| 143 | |
| 144 | #Method ## |
| 145 | |
| 146 | # ------------------------------------------------------------------------------ |
| 147 | |
| 148 | #Method bool isZero() const |
| 149 | |
| 150 | Returns true if fX and fY are both zero. |
| 151 | |
| 152 | #Return true if fX is zero and fY is zero ## |
| 153 | |
| 154 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 155 | SkPoint pt = { 0.f, -0.f}; |
| 156 | SkDebugf("pt.fX=%c%g pt.fY=%c%g\n", std::signbit(pt.fX) ? '-' : '+', fabsf(pt.fX), |
| 157 | std::signbit(pt.fY) ? '-' : '+', fabsf(pt.fY)); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 158 | SkDebugf("pt.isZero() == %s\n", pt.isZero() ? "true" : "false"); |
| 159 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 160 | pt.fX=+0 pt.fY=-0 |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 161 | pt.isZero() == true |
| 162 | ## |
| 163 | ## |
| 164 | |
| 165 | #SeeAlso isFinite SkIPoint::isZero |
| 166 | |
| 167 | #Method ## |
| 168 | |
| 169 | # ------------------------------------------------------------------------------ |
| 170 | |
| 171 | #Method void set(SkScalar x, SkScalar y) |
| 172 | |
| 173 | Sets fX to x and fY to y. |
| 174 | |
| 175 | #Param x new value for fX ## |
| 176 | #Param y new value for fY ## |
| 177 | |
| 178 | #Example |
| 179 | SkPoint pt1, pt2 = { SK_ScalarPI, SK_ScalarSqrt2 }; |
| 180 | pt1.set(SK_ScalarPI, SK_ScalarSqrt2); |
| 181 | SkDebugf("pt1 %c= pt2\n", pt1 == pt2 ? '=' : '!'); |
| 182 | #StdOut |
| 183 | pt1 == pt2 |
| 184 | ## |
| 185 | ## |
| 186 | |
| 187 | #SeeAlso iset() Make |
| 188 | |
| 189 | #Method ## |
| 190 | |
| 191 | # ------------------------------------------------------------------------------ |
| 192 | |
| 193 | #Method void iset(int32_t x, int32_t y) |
| 194 | |
| 195 | Sets fX to x and fY to y, promoting integers to SkScalar values. |
| 196 | |
| 197 | Assigning a large integer value directly to fX or fY may cause a compiler |
| 198 | error, triggered by narrowing conversion of int to SkScalar. This safely |
| 199 | casts x and y to avoid the error. |
| 200 | |
| 201 | #Param x new value for fX ## |
| 202 | #Param y new value for fY ## |
| 203 | |
| 204 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 205 | SkPoint pt1, pt2 = { SK_MinS16, SK_MaxS16 }; |
| 206 | pt1.iset(SK_MinS16, SK_MaxS16); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 207 | SkDebugf("pt1 %c= pt2\n", pt1 == pt2 ? '=' : '!'); |
| 208 | ## |
| 209 | |
| 210 | #SeeAlso set Make SkIPoint::set |
| 211 | |
| 212 | #Method ## |
| 213 | |
| 214 | # ------------------------------------------------------------------------------ |
| 215 | |
| 216 | #Method void iset(const SkIPoint& p) |
| 217 | |
| 218 | Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values. |
| 219 | |
| 220 | Assigning an IPoint containing a large integer value directly to fX or fY may |
| 221 | cause a compiler error, triggered by narrowing conversion of int to SkScalar. |
| 222 | This safely casts p.fX and p.fY to avoid the error. |
| 223 | |
| 224 | #Param p IPoint members promoted to SkScalar ## |
| 225 | |
| 226 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 227 | SkIPoint iPt = { SK_MinS32, SK_MaxS32 }; |
| 228 | SkPoint fPt; |
| 229 | fPt.iset(iPt); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 230 | SkDebugf("iPt: %d, %d\n", iPt.fX, iPt.fY); |
| 231 | SkDebugf("fPt: %g, %g\n", fPt.fX, fPt.fY); |
| 232 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 233 | iPt: -2147483647, 2147483647 |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 234 | fPt: -2.14748e+09, 2.14748e+09 |
| 235 | ## |
| 236 | ## |
| 237 | |
| 238 | #SeeAlso set Make SkIPoint::set |
| 239 | |
| 240 | #Method ## |
| 241 | |
| 242 | # ------------------------------------------------------------------------------ |
| 243 | |
| 244 | #Method void setAbs(const SkPoint& pt) |
| 245 | |
| 246 | Sets fX to absolute value of pt.fX; and fY to absolute value of pt.fY. |
| 247 | |
| 248 | #Param pt members providing magnitude for fX and fY ## |
| 249 | |
| 250 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 251 | SkPoint test[] = { {0.f, -0.f}, {-1, -2}, |
| 252 | { SK_ScalarInfinity, SK_ScalarNegativeInfinity }, |
| 253 | { SK_ScalarNaN, -SK_ScalarNaN } }; |
| 254 | for (const SkPoint& pt : test) { |
| 255 | SkPoint absPt; |
| 256 | absPt.setAbs(pt); |
| 257 | SkDebugf("pt: %g, %g abs: %g, %g\n", pt.fX, pt.fY, absPt.fX, absPt.fY); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 258 | } |
| 259 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 260 | pt: 0, -0 abs: 0, 0 |
| 261 | pt: -1, -2 abs: 1, 2 |
| 262 | pt: inf, -inf abs: inf, inf |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 263 | pt: nan, -nan abs: nan, nan |
| 264 | ## |
| 265 | ## |
| 266 | |
| 267 | #SeeAlso set Make negate |
| 268 | |
| 269 | #Method ## |
| 270 | |
| 271 | # ------------------------------------------------------------------------------ |
| 272 | |
| 273 | #Method static void Offset(SkPoint points[], int count, const SkVector& offset) |
| 274 | |
| 275 | Adds offset to each Point in points array with count entries. |
| 276 | |
| 277 | #Param points Point array ## |
| 278 | #Param count entries in array ## |
| 279 | #Param offset Vector added to points ## |
| 280 | |
| 281 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 282 | SkPaint paint; |
| 283 | paint.setAntiAlias(true); |
| 284 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 285 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 286 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 287 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 288 | canvas->scale(30, 15); |
| 289 | paint.setStyle(SkPaint::kStroke_Style); |
| 290 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 291 | SkPoint::Offset(points, SK_ARRAY_COUNT(points), { 1, 9 } ); |
| 292 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 293 | ## |
| 294 | |
| 295 | #SeeAlso offset operator+=(const SkVector& v) |
| 296 | |
| 297 | #Method ## |
| 298 | |
| 299 | # ------------------------------------------------------------------------------ |
| 300 | |
| 301 | #Method static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy) |
| 302 | |
| 303 | Adds offset (dx, dy) to each Point in points array of length count. |
| 304 | |
| 305 | #Param points Point array ## |
| 306 | #Param count entries in array ## |
| 307 | #Param dx added to fX in points ## |
| 308 | #Param dy added to fY in points ## |
| 309 | |
| 310 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 311 | SkPaint paint; |
| 312 | paint.setAntiAlias(true); |
| 313 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 314 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 315 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 316 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 317 | canvas->scale(30, 15); |
| 318 | paint.setStyle(SkPaint::kStroke_Style); |
| 319 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 320 | SkPoint::Offset(points, SK_ARRAY_COUNT(points), 1, 9); |
| 321 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 322 | ## |
| 323 | |
| 324 | #SeeAlso offset operator+=(const SkVector& v) |
| 325 | |
| 326 | #Method ## |
| 327 | |
| 328 | # ------------------------------------------------------------------------------ |
| 329 | |
| 330 | #Method void offset(SkScalar dx, SkScalar dy) |
| 331 | |
| 332 | Adds offset (dx, dy) to Point. |
| 333 | |
| 334 | #Param dx added to fX ## |
| 335 | #Param dy added to fY ## |
| 336 | |
| 337 | #Example |
| 338 | #Height 128 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 339 | SkPaint paint; |
| 340 | paint.setAntiAlias(true); |
| 341 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 342 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 343 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 344 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 345 | canvas->scale(30, 15); |
| 346 | paint.setStyle(SkPaint::kStroke_Style); |
| 347 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 348 | points[1].offset(1, 1); |
| 349 | paint.setColor(SK_ColorRED); |
| 350 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 351 | ## |
| 352 | |
| 353 | #SeeAlso Offset operator+=(const SkVector& v) |
| 354 | |
| 355 | #Method ## |
| 356 | |
| 357 | # ------------------------------------------------------------------------------ |
| 358 | |
| 359 | #Method SkScalar length() const |
| 360 | |
| 361 | Returns the Euclidean_Distance from origin, computed as: |
| 362 | #Code |
| 363 | #Literal |
| 364 | sqrt(fX * fX + fY * fY) |
| 365 | ## |
| 366 | . |
| 367 | |
| 368 | #Return straight-line distance to origin ## |
| 369 | |
| 370 | #Example |
| 371 | #Height 192 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 372 | SkPaint paint; |
| 373 | paint.setAntiAlias(true); |
| 374 | const SkPoint points[] = { { 90, 30 }, { 120, 150 }, { 150, 30 }, { 210, 90 } }; |
| 375 | const SkPoint origin = {30, 140}; |
| 376 | for (auto point : points) { |
| 377 | canvas->drawLine(origin, point, paint); |
| 378 | SkAutoCanvasRestore acr(canvas, true); |
| 379 | SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX); |
| 380 | canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY); |
| 381 | SkString length("length = "); |
| 382 | length.appendScalar(point.length()); |
| 383 | canvas->drawString(length, origin.fX + 25, origin.fY - 4, paint); |
| 384 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 385 | ## |
| 386 | |
| 387 | #SeeAlso distanceToOrigin Length setLength Distance |
| 388 | |
| 389 | #Method ## |
| 390 | |
| 391 | # ------------------------------------------------------------------------------ |
| 392 | |
| 393 | #Method SkScalar distanceToOrigin() const |
| 394 | |
| 395 | Returns the Euclidean_Distance from origin, computed as: |
| 396 | #Code |
| 397 | #Literal |
| 398 | sqrt(fX * fX + fY * fY) |
| 399 | ## |
| 400 | . |
| 401 | |
| 402 | #Return straight-line distance to origin ## |
| 403 | |
| 404 | #Example |
| 405 | #Height 192 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 406 | SkPaint paint; |
| 407 | paint.setAntiAlias(true); |
| 408 | const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } }; |
| 409 | const SkPoint origin = {0, 0}; |
| 410 | canvas->translate(30, 140); |
| 411 | for (auto point : points) { |
| 412 | canvas->drawLine(origin, point, paint); |
| 413 | SkAutoCanvasRestore acr(canvas, true); |
| 414 | SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX); |
| 415 | canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY); |
| 416 | SkString distance("distance = "); |
| 417 | distance.appendScalar(point.distanceToOrigin()); |
| 418 | canvas->drawString(distance, origin.fX + 25, origin.fY - 4, paint); |
| 419 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 420 | ## |
| 421 | |
| 422 | #SeeAlso length Length setLength Distance |
| 423 | |
| 424 | #Method ## |
| 425 | |
| 426 | # ------------------------------------------------------------------------------ |
| 427 | |
| 428 | #Method bool normalize() |
| 429 | |
| 430 | Scales (fX, fY) so that length() returns one, while preserving ratio of fX to fY, |
| 431 | if possible. If prior length is nearly zero, sets Vector to (0, 0) and returns |
| 432 | false; otherwise returns true. |
| 433 | |
| 434 | #Return true if former length is not zero or nearly zero ## |
| 435 | |
| 436 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 437 | SkPaint paint; |
| 438 | paint.setAntiAlias(true); |
| 439 | const SkPoint lines[][2] = { {{ 30, 110 }, { 190, 30 }}, |
| 440 | {{ 120, 140 }, { 30, 220 }}}; |
| 441 | for (auto line : lines) { |
| 442 | canvas->drawLine(line[0], line[1], paint); |
| 443 | SkVector vector = line[1] - line[0]; |
| 444 | if (vector.normalize()) { |
| 445 | SkVector rotate90 = { -vector.fY, vector.fX }; |
| 446 | rotate90 *= 10.f; |
| 447 | canvas->drawLine(line[0] - rotate90, line[0] + rotate90, paint); |
| 448 | canvas->drawLine(line[1] - rotate90, line[1] + rotate90, paint); |
| 449 | } |
| 450 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 451 | ## |
| 452 | |
| 453 | #SeeAlso Normalize setLength length Length |
| 454 | |
| 455 | #Method ## |
| 456 | |
| 457 | # ------------------------------------------------------------------------------ |
| 458 | |
| 459 | #Method bool setNormalize(SkScalar x, SkScalar y) |
| 460 | |
| 461 | Sets Vector to (x, y) scaled so length() returns one, and so that |
| 462 | (fX, fY) is proportional to (x, y). If (x, y) length is nearly zero, |
| 463 | sets Vector to (0, 0) and returns false; otherwise returns true. |
| 464 | |
| 465 | #Param x proportional value for fX ## |
| 466 | #Param y proportional value for fY ## |
| 467 | |
| 468 | #Return true if (x, y) length is not zero or nearly zero ## |
| 469 | |
| 470 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 471 | SkPaint paint; |
| 472 | paint.setAntiAlias(true); |
| 473 | const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } }; |
| 474 | const SkPoint origin = {0, 0}; |
| 475 | canvas->translate(30, 140); |
| 476 | for (auto point : points) { |
| 477 | paint.setStrokeWidth(1); |
| 478 | paint.setColor(SK_ColorBLACK); |
| 479 | canvas->drawLine(origin, point, paint); |
| 480 | SkVector normal; |
| 481 | normal.setNormalize(point.fX, point.fY); |
| 482 | normal *= 100; |
| 483 | paint.setStrokeWidth(10); |
| 484 | paint.setColor(0x3f4512bf); |
| 485 | canvas->drawLine(origin, normal, paint); |
| 486 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 487 | ## |
| 488 | |
| 489 | #SeeAlso normalize setLength |
| 490 | |
| 491 | #Method ## |
| 492 | |
| 493 | # ------------------------------------------------------------------------------ |
| 494 | |
| 495 | #Method bool setLength(SkScalar length) |
| 496 | |
| 497 | Scales Vector so that distanceToOrigin returns length, if possible. If former |
| 498 | length is nearly zero, sets Vector to (0, 0) and return false; otherwise returns |
| 499 | true. |
| 500 | |
| 501 | #Param length straight-line distance to origin ## |
| 502 | |
| 503 | #Return true if former length is not zero or nearly zero ## |
| 504 | |
| 505 | #Example |
| 506 | #Height 160 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 507 | SkPaint paint; |
| 508 | paint.setAntiAlias(true); |
| 509 | const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } }; |
| 510 | const SkPoint origin = {0, 0}; |
| 511 | canvas->translate(30, 140); |
| 512 | for (auto point : points) { |
| 513 | paint.setStrokeWidth(1); |
| 514 | paint.setColor(SK_ColorBLACK); |
| 515 | canvas->drawLine(origin, point, paint); |
| 516 | SkVector normal = point; |
| 517 | normal.setLength(100); |
| 518 | paint.setStrokeWidth(10); |
| 519 | paint.setColor(0x3f45bf12); |
| 520 | canvas->drawLine(origin, normal, paint); |
| 521 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 522 | ## |
| 523 | |
| 524 | #SeeAlso length Length setNormalize setAbs |
| 525 | |
| 526 | #Method ## |
| 527 | |
| 528 | # ------------------------------------------------------------------------------ |
| 529 | |
| 530 | #Method bool setLength(SkScalar x, SkScalar y, SkScalar length) |
| 531 | |
| 532 | Sets Vector to (x, y) scaled to length, if possible. If former |
| 533 | length is nearly zero, sets Vector to (0, 0) and return false; otherwise returns |
| 534 | true. |
| 535 | |
| 536 | #Param x proportional value for fX ## |
| 537 | #Param y proportional value for fY ## |
| 538 | #Param length straight-line distance to origin ## |
| 539 | |
| 540 | #Return true if (x, y) length is not zero or nearly zero ## |
| 541 | |
| 542 | #Example |
| 543 | #Height 160 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 544 | SkPaint paint; |
| 545 | paint.setAntiAlias(true); |
| 546 | const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } }; |
| 547 | const SkPoint origin = {0, 0}; |
| 548 | canvas->translate(30, 140); |
| 549 | for (auto point : points) { |
| 550 | paint.setStrokeWidth(1); |
| 551 | paint.setColor(SK_ColorBLACK); |
| 552 | canvas->drawLine(origin, point, paint); |
| 553 | SkVector normal; |
| 554 | normal.setLength(point.fX, point.fY, 100); |
| 555 | paint.setStrokeWidth(10); |
| 556 | paint.setColor(0x3fbf4512); |
| 557 | canvas->drawLine(origin, normal, paint); |
| 558 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 559 | ## |
| 560 | |
| 561 | #SeeAlso length Length setNormalize setAbs |
| 562 | |
| 563 | #Method ## |
| 564 | |
| 565 | # ------------------------------------------------------------------------------ |
| 566 | |
| 567 | #Method void scale(SkScalar scale, SkPoint* dst) const |
| 568 | |
| 569 | Sets dst to Point times scale. dst may be Point to modify Point in place. |
| 570 | |
| 571 | #Param scale factor to multiply Point by ## |
| 572 | #Param dst storage for scaled Point ## |
| 573 | |
| 574 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 575 | SkPaint paint; |
| 576 | paint.setAntiAlias(true); |
| 577 | SkPoint point = {40, -15}, scaled; |
| 578 | SkPoint origin = {30, 110}; |
| 579 | for (auto scale : {1, 2, 3, 5}) { |
| 580 | paint.setStrokeWidth(scale * 5); |
| 581 | paint.setARGB(0x7f, 0x9f, 0xbf, 0x33 * scale); |
| 582 | point.scale(scale, &scaled); |
| 583 | canvas->drawLine(origin, origin + scaled, paint); |
| 584 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 585 | ## |
| 586 | |
| 587 | #SeeAlso operator*(SkScalar scale)_const operator*=(SkScalar scale) setLength |
| 588 | |
| 589 | #Method ## |
| 590 | |
| 591 | # ------------------------------------------------------------------------------ |
| 592 | |
| 593 | #Method void scale(SkScalar value) |
| 594 | |
| 595 | Scales Point in place by scale. |
| 596 | |
| 597 | #Param value factor to multiply Point by ## |
| 598 | |
| 599 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 600 | SkPaint paint; |
| 601 | paint.setAntiAlias(true); |
| 602 | SkPoint point = {40, -15}; |
| 603 | SkPoint origin = {30, 110}; |
| 604 | for (auto scale : {1, 2, 3, 5}) { |
| 605 | paint.setStrokeWidth(scale * 5); |
| 606 | paint.setARGB(0x7f, 0x9f, 0xbf, 0x33 * scale); |
| 607 | point.scale(scale); |
| 608 | canvas->drawLine(origin, origin + point, paint); |
| 609 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 610 | ## |
| 611 | |
| 612 | #SeeAlso operator*(SkScalar scale)_const operator*=(SkScalar scale) setLength |
| 613 | |
| 614 | #Method ## |
| 615 | |
| 616 | # ------------------------------------------------------------------------------ |
| 617 | |
| 618 | #Method void negate() |
| 619 | |
| 620 | Changes the sign of fX and fY. |
| 621 | |
| 622 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 623 | SkPoint test[] = { {0.f, -0.f}, {-1, -2}, |
| 624 | { SK_ScalarInfinity, SK_ScalarNegativeInfinity }, |
| 625 | { SK_ScalarNaN, -SK_ScalarNaN } }; |
| 626 | for (const SkPoint& pt : test) { |
| 627 | SkPoint negPt = pt; |
| 628 | negPt.negate(); |
| 629 | SkDebugf("pt: %g, %g negate: %g, %g\n", pt.fX, pt.fY, negPt.fX, negPt.fY); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 630 | } |
| 631 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 632 | pt: 0, -0 negate: -0, 0 |
| 633 | pt: -1, -2 negate: 1, 2 |
| 634 | pt: inf, -inf negate: -inf, inf |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 635 | pt: nan, -nan negate: -nan, nan |
| 636 | ## |
| 637 | ## |
| 638 | |
| 639 | #SeeAlso operator-()_const setAbs |
| 640 | |
| 641 | #Method ## |
| 642 | |
| 643 | # ------------------------------------------------------------------------------ |
| 644 | |
| 645 | #Method SkPoint operator-()_const |
| 646 | |
| 647 | Returns Point changing the signs of fX and fY. |
| 648 | |
| 649 | #Return Point as (-fX, -fY) ## |
| 650 | |
| 651 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 652 | SkPoint test[] = { {0.f, -0.f}, {-1, -2}, |
| 653 | { SK_ScalarInfinity, SK_ScalarNegativeInfinity }, |
| 654 | { SK_ScalarNaN, -SK_ScalarNaN } }; |
| 655 | for (const SkPoint& pt : test) { |
| 656 | SkPoint negPt = -pt; |
| 657 | SkDebugf("pt: %g, %g negate: %g, %g\n", pt.fX, pt.fY, negPt.fX, negPt.fY); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 658 | } |
| 659 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 660 | pt: 0, -0 negate: -0, 0 |
| 661 | pt: -1, -2 negate: 1, 2 |
| 662 | pt: inf, -inf negate: -inf, inf |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 663 | pt: nan, -nan negate: -nan, nan |
| 664 | ## |
| 665 | ## |
| 666 | |
| 667 | #SeeAlso negate operator-(const SkPoint& a, const SkPoint& b) operator-=(const SkVector& v) SkIPoint::operator-()_const |
| 668 | |
| 669 | #Method ## |
| 670 | |
| 671 | # ------------------------------------------------------------------------------ |
| 672 | |
| 673 | #Method void operator+=(const SkVector& v) |
| 674 | |
| 675 | Adds Vector v to Point. Sets Point to: |
| 676 | #Formula |
| 677 | (fX + v.fX, fY + v.fY) |
| 678 | ## |
| 679 | . |
| 680 | |
| 681 | #Param v Vector to add ## |
| 682 | |
| 683 | #Example |
| 684 | #Height 128 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 685 | SkPaint paint; |
| 686 | paint.setAntiAlias(true); |
| 687 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 688 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 689 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 690 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 691 | canvas->scale(30, 15); |
| 692 | paint.setStyle(SkPaint::kStroke_Style); |
| 693 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 694 | points[1] += {1, 1}; |
| 695 | points[2] += {-1, -1}; |
| 696 | paint.setColor(SK_ColorRED); |
| 697 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 698 | ## |
| 699 | |
| 700 | #SeeAlso offset() operator+(const SkPoint& a, const SkVector& b) SkIPoint::operator+=(const SkIVector& v) |
| 701 | |
| 702 | #Method ## |
| 703 | |
| 704 | # ------------------------------------------------------------------------------ |
| 705 | |
| 706 | #Method void operator-=(const SkVector& v) |
| 707 | |
| 708 | Subtracts Vector v from Point. Sets Point to: |
| 709 | #Formula |
| 710 | (fX - v.fX, fY - v.fY) |
| 711 | ## |
| 712 | . |
| 713 | |
| 714 | #Param v Vector to subtract ## |
| 715 | |
| 716 | #Example |
| 717 | #Height 128 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 718 | SkPaint paint; |
| 719 | paint.setAntiAlias(true); |
| 720 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 721 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 722 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 723 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 724 | canvas->scale(30, 15); |
| 725 | paint.setStyle(SkPaint::kStroke_Style); |
| 726 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 727 | points[1] -= {1, 1}; |
| 728 | points[2] -= {-1, -1}; |
| 729 | paint.setColor(SK_ColorRED); |
| 730 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 731 | ## |
| 732 | |
| 733 | #SeeAlso offset() operator-(const SkPoint& a, const SkPoint& b) SkIPoint::operator-=(const SkIVector& v) |
| 734 | |
| 735 | #Method ## |
| 736 | |
| 737 | # ------------------------------------------------------------------------------ |
| 738 | |
| 739 | #Method SkPoint operator*(SkScalar scale)_const |
| 740 | |
| 741 | Returns Point multiplied by scale. |
| 742 | |
| 743 | #Param scale Scalar to multiply by ## |
| 744 | |
| 745 | #Return Point as (fX * scale, fY * scale) ## |
| 746 | |
| 747 | #Example |
| 748 | #Height 128 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 749 | SkPaint paint; |
| 750 | paint.setAntiAlias(true); |
| 751 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 752 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 753 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 754 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 755 | canvas->scale(15, 10); |
| 756 | paint.setStyle(SkPaint::kStroke_Style); |
| 757 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 758 | for (auto& point : points) { |
| 759 | point = point * 1.5f; |
| 760 | } |
| 761 | paint.setColor(SK_ColorRED); |
| 762 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 763 | ## |
| 764 | |
| 765 | #SeeAlso operator*=(SkScalar scale) scale() setLength setNormalize |
| 766 | |
| 767 | #Method ## |
| 768 | |
| 769 | # ------------------------------------------------------------------------------ |
| 770 | |
| 771 | #Method SkPoint& operator*=(SkScalar scale) |
| 772 | |
| 773 | Multiplies Point by scale. Sets Point to: |
| 774 | #Formula |
| 775 | (fX * scale, fY * scale) |
| 776 | ## |
| 777 | |
| 778 | #Param scale Scalar to multiply by ## |
| 779 | |
| 780 | #Return reference to Point ## |
| 781 | |
| 782 | #Example |
| 783 | #Height 128 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 784 | SkPaint paint; |
| 785 | paint.setAntiAlias(true); |
| 786 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 787 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 788 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 789 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 790 | canvas->scale(15, 10); |
| 791 | paint.setStyle(SkPaint::kStroke_Style); |
| 792 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 793 | for (auto& point : points) { |
| 794 | point *= 2; |
| 795 | } |
| 796 | paint.setColor(SK_ColorRED); |
| 797 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 798 | ## |
| 799 | |
| 800 | #SeeAlso operator*(SkScalar scale)_const scale() setLength setNormalize |
| 801 | |
| 802 | #Method ## |
| 803 | |
| 804 | # ------------------------------------------------------------------------------ |
| 805 | |
| 806 | #Method bool isFinite() const |
| 807 | |
| 808 | Returns true if both fX and fY are measurable values. |
| 809 | |
| 810 | #Return true for values other than infinities and NaN ## |
| 811 | |
| 812 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 813 | SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} }; |
| 814 | for (const SkPoint& pt : test) { |
| 815 | SkDebugf("pt: %g, %g finite: %s\n", pt.fX, pt.fY, pt.isFinite() ? "true" : "false"); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 816 | } |
| 817 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 818 | pt: 0, -0 finite: true |
| 819 | pt: -1, -2 finite: true |
| 820 | pt: inf, 1 finite: false |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 821 | pt: nan, -1 finite: false |
| 822 | ## |
| 823 | ## |
| 824 | |
| 825 | #SeeAlso SkRect::isFinite SkPath::isFinite |
| 826 | |
| 827 | #Method ## |
| 828 | |
| 829 | # ------------------------------------------------------------------------------ |
| 830 | |
| 831 | #Method bool equals(SkScalar x, SkScalar y) const |
| 832 | |
| 833 | Returns true if Point is equivalent to Point constructed from (x, y). |
| 834 | |
| 835 | #Param x value compared with fX ## |
| 836 | #Param y value compared with fY ## |
| 837 | |
| 838 | #Return true if Point equals (x, y) ## |
| 839 | |
| 840 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 841 | SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} }; |
| 842 | for (const SkPoint& pt : test) { |
| 843 | SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt.equals(pt.fX, pt.fY) ? '=' : '!'); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 844 | } |
| 845 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 846 | pt: 0, -0 == pt |
| 847 | pt: -1, -2 == pt |
| 848 | pt: inf, 1 == pt |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 849 | pt: nan, -1 != pt |
| 850 | ## |
| 851 | ## |
| 852 | |
| 853 | #SeeAlso operator==(const SkPoint& a, const SkPoint& b) |
| 854 | |
| 855 | #Method ## |
| 856 | |
| 857 | # ------------------------------------------------------------------------------ |
| 858 | |
| 859 | #Method bool operator==(const SkPoint& a, const SkPoint& b) |
| 860 | |
| 861 | Returns true if a is equivalent to b. |
| 862 | |
| 863 | #Param a Point to compare ## |
| 864 | #Param b Point to compare ## |
| 865 | |
| 866 | #Return true if a.fX == b.fX and a.fY == b.fY ## |
| 867 | |
| 868 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 869 | SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} }; |
| 870 | for (const SkPoint& pt : test) { |
| 871 | SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt == pt ? '=' : '!'); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 872 | } |
| 873 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 874 | pt: 0, -0 == pt |
| 875 | pt: -1, -2 == pt |
| 876 | pt: inf, 1 == pt |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 877 | pt: nan, -1 != pt |
| 878 | ## |
| 879 | ## |
| 880 | |
| 881 | #SeeAlso equals() operator!=(const SkPoint& a, const SkPoint& b) |
| 882 | |
| 883 | #Method ## |
| 884 | |
| 885 | # ------------------------------------------------------------------------------ |
| 886 | |
| 887 | #Method bool operator!=(const SkPoint& a, const SkPoint& b) |
| 888 | |
| 889 | Returns true if a is not equivalent to b. |
| 890 | |
| 891 | #Param a Point to compare ## |
| 892 | #Param b Point to compare ## |
| 893 | |
| 894 | #Return true if a.fX != b.fX or a.fY != b.fY ## |
| 895 | |
| 896 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 897 | SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} }; |
| 898 | for (const SkPoint& pt : test) { |
| 899 | SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt != pt ? '!' : '='); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 900 | } |
| 901 | #StdOut |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 902 | pt: 0, -0 == pt |
| 903 | pt: -1, -2 == pt |
| 904 | pt: inf, 1 == pt |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 905 | pt: nan, -1 != pt |
| 906 | ## |
| 907 | ## |
| 908 | |
| 909 | #SeeAlso operator==(const SkPoint& a, const SkPoint& b) equals() |
| 910 | |
| 911 | #Method ## |
| 912 | |
| 913 | # ------------------------------------------------------------------------------ |
| 914 | |
| 915 | #Method SkVector operator-(const SkPoint& a, const SkPoint& b) |
| 916 | |
| 917 | Returns Vector from b to a, computed as |
| 918 | #Formula |
| 919 | (a.fX - b.fX, a.fY - b.fY) |
| 920 | ## |
| 921 | . |
| 922 | |
| 923 | Can also be used to subtract Vector from Point, returning Point. |
| 924 | Can also be used to subtract Vector from Vector, returning Vector. |
| 925 | |
| 926 | #Param a Point to subtract from ## |
| 927 | #Param b Point to subtract ## |
| 928 | |
| 929 | #Return Vector from b to a ## |
| 930 | |
| 931 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 932 | SkPaint paint; |
| 933 | paint.setAntiAlias(true); |
| 934 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 935 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 936 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 937 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 938 | canvas->scale(30, 15); |
| 939 | paint.setStyle(SkPaint::kStroke_Style); |
| 940 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 941 | points[1] += points[0] - points[2]; |
| 942 | points[2] -= points[3] - points[5]; |
| 943 | paint.setColor(SK_ColorRED); |
| 944 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 945 | ## |
| 946 | |
| 947 | #SeeAlso operator-=(const SkVector& v) offset() |
| 948 | |
| 949 | #Method ## |
| 950 | |
| 951 | # ------------------------------------------------------------------------------ |
| 952 | |
| 953 | #Method SkPoint operator+(const SkPoint& a, const SkVector& b) |
| 954 | |
| 955 | Returns Point resulting from Point a offset by Vector b, computed as: |
| 956 | #Formula |
| 957 | (a.fX + b.fX, a.fY + b.fY) |
| 958 | ## |
| 959 | . |
| 960 | |
| 961 | Can also be used to offset Point b by Vector a, returning Point. |
| 962 | Can also be used to add Vector to Vector, returning Vector. |
| 963 | |
| 964 | #Param a Point or Vector to add to ## |
| 965 | #Param b Point or Vector to add ## |
| 966 | |
| 967 | #Return Point equal to a offset by b ## |
| 968 | |
| 969 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 970 | SkPaint paint; |
| 971 | paint.setAntiAlias(true); |
| 972 | SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 973 | { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 974 | { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 975 | { 2, 4 }, { 1, 3 }, { 3, 1 } }; |
| 976 | canvas->scale(30, 15); |
| 977 | paint.setStyle(SkPaint::kStroke_Style); |
| 978 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
| 979 | SkVector mod = {1, 1}; |
| 980 | for (auto& point : points) { |
| 981 | point = point + mod; |
| 982 | mod.fX *= 1.1f; |
| 983 | mod.fY += .2f; |
| 984 | } |
| 985 | paint.setColor(SK_ColorRED); |
| 986 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 987 | ## |
| 988 | |
| 989 | #SeeAlso operator+=(const SkVector& v) offset() |
| 990 | |
| 991 | #Method ## |
| 992 | |
| 993 | # ------------------------------------------------------------------------------ |
| 994 | |
| 995 | #Method static SkScalar Length(SkScalar x, SkScalar y) |
| 996 | |
| 997 | Returns the Euclidean_Distance from origin, computed as: |
| 998 | #Code |
| 999 | #Literal |
| 1000 | sqrt(x * x + y * y) |
| 1001 | ## |
| 1002 | . |
| 1003 | |
| 1004 | #Param x component of length ## |
| 1005 | #Param y component of length ## |
| 1006 | |
| 1007 | #Return straight-line distance to origin ## |
| 1008 | |
| 1009 | #Example |
| 1010 | #Height 192 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 1011 | SkPaint paint; |
| 1012 | paint.setAntiAlias(true); |
| 1013 | const SkPoint points[] = { { 90, 30 }, { 120, 150 }, { 150, 30 }, { 210, 90 } }; |
| 1014 | const SkPoint origin = {30, 140}; |
| 1015 | for (auto point : points) { |
| 1016 | canvas->drawLine(origin, point, paint); |
| 1017 | SkAutoCanvasRestore acr(canvas, true); |
| 1018 | SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX); |
| 1019 | canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY); |
| 1020 | SkString length("length = "); |
| 1021 | length.appendScalar(SkPoint::Length(point.fX, point.fY)); |
| 1022 | canvas->drawString(length, origin.fX + 25, origin.fY - 4, paint); |
| 1023 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1024 | ## |
| 1025 | |
| 1026 | #SeeAlso length() Distance setLength |
| 1027 | |
| 1028 | #Method ## |
| 1029 | |
| 1030 | # ------------------------------------------------------------------------------ |
| 1031 | |
| 1032 | #Method static SkScalar Normalize(SkVector* vec) |
| 1033 | |
| 1034 | Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX to vec->fY, |
| 1035 | if possible. If original length is nearly zero, sets vec to (0, 0) and returns zero; |
| 1036 | otherwise, returns length of vec before vec is scaled. |
| 1037 | |
| 1038 | Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar. |
| 1039 | |
| 1040 | Note that normalize() is faster if prior length is not required. |
| 1041 | |
| 1042 | #Param vec normalized to unit length ## |
| 1043 | |
| 1044 | #Return original vec length ## |
| 1045 | |
| 1046 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 1047 | SkPaint paint; |
| 1048 | paint.setAntiAlias(true); |
| 1049 | const SkPoint lines[][2] = { {{ 30, 110 }, { 190, 30 }}, |
| 1050 | {{ 30, 220 }, { 120, 140 }}}; |
| 1051 | for (auto line : lines) { |
| 1052 | canvas->drawLine(line[0], line[1], paint); |
| 1053 | SkVector vector = line[1] - line[0]; |
| 1054 | SkScalar priorLength = SkPoint::Normalize(&vector); |
| 1055 | SkVector rotate90 = { -vector.fY, vector.fX }; |
| 1056 | rotate90 *= 10.f; |
| 1057 | canvas->drawLine(line[0] - rotate90, line[0] + rotate90, paint); |
| 1058 | canvas->drawLine(line[1] - rotate90, line[1] + rotate90, paint); |
| 1059 | SkString length("length = "); |
| 1060 | length.appendScalar(priorLength); |
| 1061 | canvas->drawString(length, line[0].fX + 25, line[0].fY - 4, paint); |
| 1062 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1063 | ## |
| 1064 | |
| 1065 | #SeeAlso normalize() setLength Length |
| 1066 | |
| 1067 | #Method ## |
| 1068 | |
| 1069 | # ------------------------------------------------------------------------------ |
| 1070 | |
| 1071 | #Method static SkScalar Distance(const SkPoint& a, const SkPoint& b) |
| 1072 | |
| 1073 | Returns the Euclidean_Distance between a and b. |
| 1074 | |
| 1075 | #Param a line end point ## |
| 1076 | #Param b line end point ## |
| 1077 | |
| 1078 | #Return straight-line distance from a to b ## |
| 1079 | |
| 1080 | #Example |
| 1081 | #Height 192 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 1082 | SkPaint paint; |
| 1083 | paint.setAntiAlias(true); |
| 1084 | const SkPoint lines[][2] = {{{-10, -10}, {90, 30}}, {{0, 0}, {150, 30}}, {{10, 25}, {120, 150}}}; |
| 1085 | const SkPoint origin = {30, 160}; |
| 1086 | for (auto line : lines) { |
| 1087 | SkPoint a = origin + line[0]; |
| 1088 | const SkPoint& b = line[1]; |
| 1089 | canvas->drawLine(a, b, paint); |
| 1090 | SkAutoCanvasRestore acr(canvas, true); |
| 1091 | SkScalar angle = SkScalarATan2((b.fY - a.fY), b.fX - a.fX); |
| 1092 | canvas->rotate(angle * 180 / SK_ScalarPI, a.fX, a.fY); |
| 1093 | SkString distance("distance = "); |
| 1094 | distance.appendScalar(SkPoint::Distance(a, b)); |
| 1095 | canvas->drawString(distance, a.fX + 25, a.fY - 4, paint); |
| 1096 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1097 | ## |
| 1098 | |
| 1099 | #SeeAlso length() setLength |
| 1100 | |
| 1101 | #Method ## |
| 1102 | |
| 1103 | # ------------------------------------------------------------------------------ |
| 1104 | |
| 1105 | #Method static SkScalar DotProduct(const SkVector& a, const SkVector& b) |
| 1106 | |
| 1107 | Returns the dot product of Vector a and Vector b. |
| 1108 | |
| 1109 | #Param a left side of dot product ## |
| 1110 | #Param b right side of dot product ## |
| 1111 | |
| 1112 | #Return product of input magnitudes and cosine of the angle between them ## |
| 1113 | |
| 1114 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 1115 | SkPaint paint; |
| 1116 | paint.setAntiAlias(true); |
| 1117 | SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}}, |
| 1118 | {{-20, -24}, {-24, -20}}}; |
| 1119 | SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}}; |
| 1120 | paint.setStrokeWidth(2); |
| 1121 | for (size_t i = 0; i < 4; ++i) { |
| 1122 | canvas->drawLine(center[i], center[i] + vectors[i][0], paint); |
| 1123 | canvas->drawLine(center[i], center[i] + vectors[i][1], paint); |
| 1124 | SkString str; |
| 1125 | str.printf("dot = %g", SkPoint::DotProduct(vectors[i][0], vectors[i][1])); |
| 1126 | canvas->drawString(str, center[i].fX, center[i].fY, paint); |
| 1127 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1128 | ## |
| 1129 | |
| 1130 | #SeeAlso dot CrossProduct |
| 1131 | |
| 1132 | #Method ## |
| 1133 | |
| 1134 | # ------------------------------------------------------------------------------ |
| 1135 | |
| 1136 | #Method static SkScalar CrossProduct(const SkVector& a, const SkVector& b) |
| 1137 | |
| 1138 | Returns the cross product of Vector a and Vector b. |
| 1139 | |
| 1140 | a and b form three-dimensional vectors with z equal to zero. The cross product |
| 1141 | is a three-dimensional vector with x and y equal to zero. The cross product z |
| 1142 | term equals the returned value. |
| 1143 | |
| 1144 | #Param a left side of cross product ## |
| 1145 | #Param b right side of cross product ## |
| 1146 | |
| 1147 | #Return area spanned by Vectors signed by angle direction ## |
| 1148 | |
| 1149 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 1150 | SkPaint paint; |
| 1151 | paint.setAntiAlias(true); |
| 1152 | SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}}, |
| 1153 | {{-20, -24}, {-24, -20}}}; |
| 1154 | SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}}; |
| 1155 | paint.setStrokeWidth(2); |
| 1156 | for (size_t i = 0; i < 4; ++i) { |
| 1157 | paint.setColor(SK_ColorRED); |
| 1158 | canvas->drawLine(center[i], center[i] + vectors[i][0], paint); |
| 1159 | paint.setColor(SK_ColorBLUE); |
| 1160 | canvas->drawLine(center[i], center[i] + vectors[i][1], paint); |
| 1161 | SkString str; |
| 1162 | SkScalar cross = SkPoint::CrossProduct(vectors[i][1], vectors[i][0]); |
| 1163 | str.printf("cross = %g", cross); |
| 1164 | paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE); |
| 1165 | canvas->drawString(str, center[i].fX, center[i].fY, paint); |
| 1166 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1167 | ## |
| 1168 | |
| 1169 | #SeeAlso cross DotProduct |
| 1170 | |
| 1171 | #Method ## |
| 1172 | |
| 1173 | # ------------------------------------------------------------------------------ |
| 1174 | |
| 1175 | #Method SkScalar cross(const SkVector& vec) const |
| 1176 | |
| 1177 | Returns the cross product of Vector and vec. |
| 1178 | |
| 1179 | Vector and vec form three-dimensional vectors with z equal to zero. The |
| 1180 | cross product is a three-dimensional vector with x and y equal to zero. |
| 1181 | The cross product z term equals the returned value. |
| 1182 | |
| 1183 | #Param vec right side of cross product ## |
| 1184 | |
| 1185 | #Return area spanned by Vectors signed by angle direction ## |
| 1186 | |
| 1187 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 1188 | SkPaint paint; |
| 1189 | paint.setAntiAlias(true); |
| 1190 | SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}}, |
| 1191 | {{-20, -24}, {-24, -20}}}; |
| 1192 | SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}}; |
| 1193 | paint.setStrokeWidth(2); |
| 1194 | for (size_t i = 0; i < 4; ++i) { |
| 1195 | paint.setColor(SK_ColorRED); |
| 1196 | canvas->drawLine(center[i], center[i] + vectors[i][0], paint); |
| 1197 | paint.setColor(SK_ColorBLUE); |
| 1198 | canvas->drawLine(center[i], center[i] + vectors[i][1], paint); |
| 1199 | SkString str; |
| 1200 | SkScalar cross = vectors[i][0].cross(vectors[i][1]); |
| 1201 | str.printf("cross = %g", cross); |
| 1202 | paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE); |
| 1203 | canvas->drawString(str, center[i].fX, center[i].fY, paint); |
| 1204 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1205 | ## |
| 1206 | |
| 1207 | #SeeAlso CrossProduct dot |
| 1208 | |
| 1209 | #Method ## |
| 1210 | |
| 1211 | # ------------------------------------------------------------------------------ |
| 1212 | |
| 1213 | #Method SkScalar dot(const SkVector& vec) const |
| 1214 | |
| 1215 | Returns the dot product of Vector and Vector vec. |
| 1216 | |
| 1217 | #Param vec right side of dot product ## |
| 1218 | |
| 1219 | #Return product of input magnitudes and cosine of the angle between them ## |
| 1220 | |
| 1221 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 1222 | SkPaint paint; |
| 1223 | paint.setAntiAlias(true); |
| 1224 | SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}}, |
| 1225 | {{-20, -24}, {-24, -20}}}; |
| 1226 | SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}}; |
| 1227 | paint.setStrokeWidth(2); |
| 1228 | for (size_t i = 0; i < 4; ++i) { |
| 1229 | canvas->drawLine(center[i], center[i] + vectors[i][0], paint); |
| 1230 | canvas->drawLine(center[i], center[i] + vectors[i][1], paint); |
| 1231 | SkString str; |
| 1232 | str.printf("dot = %g", vectors[i][0].dot(vectors[i][1])); |
| 1233 | canvas->drawString(str, center[i].fX, center[i].fY, paint); |
| 1234 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1235 | ## |
| 1236 | |
| 1237 | #SeeAlso DotProduct cross |
| 1238 | |
| 1239 | #Method ## |
| 1240 | |
| 1241 | #Struct SkPoint ## |
| 1242 | |
| 1243 | #Topic Point ## |
| 1244 | |
| 1245 | # ------------------------------------------------------------------------------ |
| 1246 | |
| 1247 | #Topic Vector |
| 1248 | #Alias Vectors |
| 1249 | #Typedef SkPoint SkVector |
| 1250 | #Typedef ## |
| 1251 | ## |