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