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