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