blob: ff7710e781eb6a474908d5528e4acc280110c959 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Point
2#Alias Points
3#Alias Point_Reference
4
Cary Clark08895c42018-02-01 09:37:32 -05005#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05006 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05007 #Populate
8 ##
Cary Clarka560c472017-11-27 10:44:06 -05009##
10
Cary Clark08895c42018-02-01 09:37:32 -050011#Struct SkPoint
12
Cary Clark4855f782018-02-06 09:41:53 -050013SkPoint holds two 32 bit floating point coordinates.
14
15#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050016#Populate
17##
Cary Clark5081eed2018-01-22 07:55:48 -050018
Cary Clark4855f782018-02-06 09:41:53 -050019#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050020#Populate
21##
Cary Clarka560c472017-11-27 10:44:06 -050022
Cary Clark4855f782018-02-06 09:41:53 -050023#Subtopic Member
Cary Clark08895c42018-02-01 09:37:32 -050024#Populate
Cary Clarka560c472017-11-27 10:44:06 -050025
26#Member SkScalar fX
Cary Clark08895c42018-02-01 09:37:32 -050027#Line # x-axis value ##
Cary Clarka560c472017-11-27 10:44:06 -050028x-axis value used by both Point and Vector. May contain any value, including
29infinities and NaN.
30##
31
32#Member SkScalar fY
Cary Clark08895c42018-02-01 09:37:32 -050033#Line # y-axis value ##
Cary Clarka560c472017-11-27 10:44:06 -050034y-axis value used by both Point and Vector. May contain any value, including
35infinities and NaN.
36##
37
Cary Clark4855f782018-02-06 09:41:53 -050038#Subtopic Member ##
Cary Clark08895c42018-02-01 09:37:32 -050039
Cary Clarka560c472017-11-27 10:44:06 -050040# ------------------------------------------------------------------------------
41
Cary Clark4855f782018-02-06 09:41:53 -050042#Subtopic Constructor
43#Populate
44##
Cary Clarka560c472017-11-27 10:44:06 -050045
Cary Clark4855f782018-02-06 09:41:53 -050046#Method static constexpr SkPoint Make(SkScalar x, SkScalar y)
47#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -050048#Line # constructs from SkScalar inputs ##
Cary Clarka560c472017-11-27 10:44:06 -050049Sets 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
57SkPoint pt1 = {45, 66};
58SkPoint pt2 = SkPoint::Make(45, 66);
59SkVector v1 = {45, 66};
60SkVector v2 = SkPoint::Make(45, 66);
61SkDebugf("all %s" "equal\n", pt1 == pt2 && pt2 == v1 && v1 == v2 ? "" : "not ");
62#StdOut
63all equal
64##
65##
66
67#SeeAlso set() iset() SkIPoint::Make
68
69#Method ##
70
71# ------------------------------------------------------------------------------
72
Cary Clark4855f782018-02-06 09:41:53 -050073#Subtopic Property
74#Line # member values ##
75#Populate
76##
Cary Clarka560c472017-11-27 10:44:06 -050077
Cary Clark4855f782018-02-06 09:41:53 -050078#Method SkScalar x() const
79#In Property
Cary Clark08895c42018-02-01 09:37:32 -050080#Line # returns fX ##
Cary Clarka560c472017-11-27 10:44:06 -050081Returns x-axis value of Point or Vector.
82
83#Return fX ##
84
85#Example
86SkPoint pt1 = {45, 66};
87SkDebugf("pt1.fX %c= pt1.x()\n", pt1.fX == pt1.x() ? '=' : '!');
88#StdOut
89pt1.fX == pt1.x()
90##
91##
92
93#SeeAlso y() SkIPoint::x()
94
95#Method ##
96
97# ------------------------------------------------------------------------------
98
99#Method SkScalar y() const
Cary Clark4855f782018-02-06 09:41:53 -0500100#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500101#Line # returns fY ##
Cary Clarka560c472017-11-27 10:44:06 -0500102Returns y-axis value of Point or Vector.
103
104#Return fY ##
105
106#Example
107SkPoint pt1 = {45, 66};
108SkDebugf("pt1.fY %c= pt1.y()\n", pt1.fY == pt1.y() ? '=' : '!');
109#StdOut
110pt1.fY == pt1.y()
111##
112##
113
114#SeeAlso x() SkIPoint::y()
115
116#Method ##
117
118# ------------------------------------------------------------------------------
119
120#Method bool isZero() const
Cary Clark4855f782018-02-06 09:41:53 -0500121#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500122#Line # returns true if both members equal zero ##
Cary Clarka560c472017-11-27 10:44:06 -0500123Returns true if fX and fY are both zero.
124
125#Return true if fX is zero and fY is zero ##
126
127#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500128SkPoint pt = { 0.f, -0.f};
129SkDebugf("pt.fX=%c%g pt.fY=%c%g\n", std::signbit(pt.fX) ? '-' : '+', fabsf(pt.fX),
130 std::signbit(pt.fY) ? '-' : '+', fabsf(pt.fY));
Cary Clarka560c472017-11-27 10:44:06 -0500131SkDebugf("pt.isZero() == %s\n", pt.isZero() ? "true" : "false");
132#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500133pt.fX=+0 pt.fY=-0
Cary Clarka560c472017-11-27 10:44:06 -0500134pt.isZero() == true
135##
136##
137
138#SeeAlso isFinite SkIPoint::isZero
139
140#Method ##
141
142# ------------------------------------------------------------------------------
143
Cary Clark4855f782018-02-06 09:41:53 -0500144#Subtopic Set
145#Populate
146#Line # replaces all values ##
147##
Cary Clarka560c472017-11-27 10:44:06 -0500148
Cary Clark4855f782018-02-06 09:41:53 -0500149#Method void set(SkScalar x, SkScalar y)
150#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500151#Line # sets to SkScalar input ##
Cary Clarka560c472017-11-27 10:44:06 -0500152Sets 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
158SkPoint pt1, pt2 = { SK_ScalarPI, SK_ScalarSqrt2 };
159pt1.set(SK_ScalarPI, SK_ScalarSqrt2);
160SkDebugf("pt1 %c= pt2\n", pt1 == pt2 ? '=' : '!');
161#StdOut
162pt1 == pt2
163##
164##
165
166#SeeAlso iset() Make
167
168#Method ##
169
170# ------------------------------------------------------------------------------
171
172#Method void iset(int32_t x, int32_t y)
Cary Clark4855f782018-02-06 09:41:53 -0500173#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500174#Line # sets to integer input ##
Cary Clarka560c472017-11-27 10:44:06 -0500175Sets fX to x and fY to y, promoting integers to SkScalar values.
176
177Assigning a large integer value directly to fX or fY may cause a compiler
178error, triggered by narrowing conversion of int to SkScalar. This safely
179casts 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500185SkPoint pt1, pt2 = { SK_MinS16, SK_MaxS16 };
186pt1.iset(SK_MinS16, SK_MaxS16);
Cary Clarka560c472017-11-27 10:44:06 -0500187SkDebugf("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
198Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values.
199
200Assigning an IPoint containing a large integer value directly to fX or fY may
201cause a compiler error, triggered by narrowing conversion of int to SkScalar.
202This safely casts p.fX and p.fY to avoid the error.
203
204#Param p IPoint members promoted to SkScalar ##
205
206#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500207SkIPoint iPt = { SK_MinS32, SK_MaxS32 };
208SkPoint fPt;
209fPt.iset(iPt);
Cary Clarka560c472017-11-27 10:44:06 -0500210SkDebugf("iPt: %d, %d\n", iPt.fX, iPt.fY);
211SkDebugf("fPt: %g, %g\n", fPt.fX, fPt.fY);
212#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500213iPt: -2147483647, 2147483647
Cary Clarka560c472017-11-27 10:44:06 -0500214fPt: -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)
Cary Clark4855f782018-02-06 09:41:53 -0500225#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500226#Line # sets sign of both members to positive ##
Cary Clarka560c472017-11-27 10:44:06 -0500227Sets 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500232SkPoint test[] = { {0.f, -0.f}, {-1, -2},
233 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
234 { SK_ScalarNaN, -SK_ScalarNaN } };
235for (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);
Cary Clarka560c472017-11-27 10:44:06 -0500239}
240#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500241pt: 0, -0 abs: 0, 0
242pt: -1, -2 abs: 1, 2
243pt: inf, -inf abs: inf, inf
Cary Clarka560c472017-11-27 10:44:06 -0500244pt: nan, -nan abs: nan, nan
245##
246##
247
248#SeeAlso set Make negate
249
250#Method ##
251
252# ------------------------------------------------------------------------------
253
Cary Clark4855f782018-02-06 09:41:53 -0500254#Subtopic Offset
255#Line # moves sides ##
256#Populate
257##
Cary Clarka560c472017-11-27 10:44:06 -0500258
Cary Clark4855f782018-02-06 09:41:53 -0500259#Method static void Offset(SkPoint points[], int count, const SkVector& offset)
260#In Offset
Cary Clark08895c42018-02-01 09:37:32 -0500261#Line # translates Point array ##
Cary Clarka560c472017-11-27 10:44:06 -0500262Adds 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500269 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);
Cary Clarka560c472017-11-27 10:44:06 -0500280##
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
290Adds 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500298 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);
Cary Clarka560c472017-11-27 10:44:06 -0500309##
310
311#SeeAlso offset operator+=(const SkVector& v)
312
313#Method ##
314
315# ------------------------------------------------------------------------------
316
317#Method void offset(SkScalar dx, SkScalar dy)
Cary Clark4855f782018-02-06 09:41:53 -0500318#In Offset
Cary Clark08895c42018-02-01 09:37:32 -0500319#Line # translates Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500320Adds offset (dx, dy) to Point.
321
322#Param dx added to fX ##
323#Param dy added to fY ##
324
325#Example
326#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500327 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);
Cary Clarka560c472017-11-27 10:44:06 -0500339##
340
341#SeeAlso Offset operator+=(const SkVector& v)
342
343#Method ##
344
345# ------------------------------------------------------------------------------
346
347#Method SkScalar length() const
Cary Clark4855f782018-02-06 09:41:53 -0500348#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500349#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -0500350Returns the Euclidean_Distance from origin, computed as:
351#Code
352#Literal
353sqrt(fX * fX + fY * fY)
354##
355.
356
357#Return straight-line distance to origin ##
358
359#Example
360#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -0500361 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500374##
375
376#SeeAlso distanceToOrigin Length setLength Distance
377
378#Method ##
379
380# ------------------------------------------------------------------------------
381
382#Method SkScalar distanceToOrigin() const
Cary Clark4855f782018-02-06 09:41:53 -0500383#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500384#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -0500385Returns the Euclidean_Distance from origin, computed as:
386#Code
387#Literal
388sqrt(fX * fX + fY * fY)
389##
390.
391
392#Return straight-line distance to origin ##
393
394#Example
395#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -0500396 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500410##
411
412#SeeAlso length Length setLength Distance
413
414#Method ##
415
416# ------------------------------------------------------------------------------
417
418#Method bool normalize()
Cary Clark4855f782018-02-06 09:41:53 -0500419#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500420#Line # sets length to one, preserving direction ##
Cary Clarka560c472017-11-27 10:44:06 -0500421Scales (fX, fY) so that length() returns one, while preserving ratio of fX to fY,
422if possible. If prior length is nearly zero, sets Vector to (0, 0) and returns
423false; otherwise returns true.
424
425#Return true if former length is not zero or nearly zero ##
426
427#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500428 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500442##
443
444#SeeAlso Normalize setLength length Length
445
446#Method ##
447
448# ------------------------------------------------------------------------------
449
450#Method bool setNormalize(SkScalar x, SkScalar y)
Cary Clark4855f782018-02-06 09:41:53 -0500451#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500452#Line # sets length to one, in direction of (x, y) ##
Cary Clarka560c472017-11-27 10:44:06 -0500453Sets 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,
455sets 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500463 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500479##
480
481#SeeAlso normalize setLength
482
483#Method ##
484
485# ------------------------------------------------------------------------------
486
487#Method bool setLength(SkScalar length)
Cary Clark4855f782018-02-06 09:41:53 -0500488#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500489#Line # sets straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -0500490Scales Vector so that distanceToOrigin returns length, if possible. If former
491length is nearly zero, sets Vector to (0, 0) and return false; otherwise returns
492true.
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
Cary Clark0c5f5462017-12-15 11:21:51 -0500500 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500515##
516
517#SeeAlso length Length setNormalize setAbs
518
519#Method ##
520
521# ------------------------------------------------------------------------------
522
523#Method bool setLength(SkScalar x, SkScalar y, SkScalar length)
524
525Sets Vector to (x, y) scaled to length, if possible. If former
526length is nearly zero, sets Vector to (0, 0) and return false; otherwise returns
527true.
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
Cary Clark0c5f5462017-12-15 11:21:51 -0500537 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500552##
553
554#SeeAlso length Length setNormalize setAbs
555
556#Method ##
557
558# ------------------------------------------------------------------------------
559
Cary Clark4855f782018-02-06 09:41:53 -0500560#Subtopic Operator
561#Populate
562##
Cary Clarka560c472017-11-27 10:44:06 -0500563
Cary Clark4855f782018-02-06 09:41:53 -0500564#Method void scale(SkScalar scale, SkPoint* dst) const
565#In Offset
566#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500567#Line # multiplies Point by scale factor ##
Cary Clarka560c472017-11-27 10:44:06 -0500568Sets 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500574 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500584##
585
586#SeeAlso operator*(SkScalar scale)_const operator*=(SkScalar scale) setLength
587
588#Method ##
589
590# ------------------------------------------------------------------------------
591
592#Method void scale(SkScalar value)
593
594Scales Point in place by scale.
595
596#Param value factor to multiply Point by ##
597
598#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500599 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 }
Cary Clarka560c472017-11-27 10:44:06 -0500609##
610
611#SeeAlso operator*(SkScalar scale)_const operator*=(SkScalar scale) setLength
612
613#Method ##
614
615# ------------------------------------------------------------------------------
616
617#Method void negate()
Cary Clark4855f782018-02-06 09:41:53 -0500618#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500619#Line # reverses the sign of both members ##
Cary Clarka560c472017-11-27 10:44:06 -0500620Changes the sign of fX and fY.
621
622#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500623SkPoint test[] = { {0.f, -0.f}, {-1, -2},
624 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
625 { SK_ScalarNaN, -SK_ScalarNaN } };
626for (const SkPoint& pt : test) {
627 SkPoint negPt = pt;
628 negPt.negate();
629 SkDebugf("pt: %g, %g negate: %g, %g\n", pt.fX, pt.fY, negPt.fX, negPt.fY);
Cary Clarka560c472017-11-27 10:44:06 -0500630}
631#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500632pt: 0, -0 negate: -0, 0
633pt: -1, -2 negate: 1, 2
634pt: inf, -inf negate: -inf, inf
Cary Clarka560c472017-11-27 10:44:06 -0500635pt: 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
Cary Clark08895c42018-02-01 09:37:32 -0500647#Line # reverses sign of Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500648Returns Point changing the signs of fX and fY.
649
650#Return Point as (-fX, -fY) ##
651
652#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500653SkPoint test[] = { {0.f, -0.f}, {-1, -2},
654 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
655 { SK_ScalarNaN, -SK_ScalarNaN } };
656for (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);
Cary Clarka560c472017-11-27 10:44:06 -0500659}
660#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500661pt: 0, -0 negate: -0, 0
662pt: -1, -2 negate: 1, 2
663pt: inf, -inf negate: -inf, inf
Cary Clarka560c472017-11-27 10:44:06 -0500664pt: 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
Cary Clark08895c42018-02-01 09:37:32 -0500676#Line # adds Vector to Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500677Adds 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500687 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);
Cary Clarka560c472017-11-27 10:44:06 -0500700##
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
Cary Clark08895c42018-02-01 09:37:32 -0500710#Line # subtracts Vector from Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500711Subtracts 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500721 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);
Cary Clarka560c472017-11-27 10:44:06 -0500734##
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
Cary Clark08895c42018-02-01 09:37:32 -0500744#Line # returns Point multiplied by scale ##
Cary Clarka560c472017-11-27 10:44:06 -0500745Returns 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500753 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);
Cary Clarka560c472017-11-27 10:44:06 -0500767##
768
769#SeeAlso operator*=(SkScalar scale) scale() setLength setNormalize
770
771#Method ##
772
773# ------------------------------------------------------------------------------
774
775#Method SkPoint& operator*=(SkScalar scale)
776
Cary Clark08895c42018-02-01 09:37:32 -0500777#Line # multiplies Point by scale factor ##
Cary Clarka560c472017-11-27 10:44:06 -0500778Multiplies 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500789 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);
Cary Clarka560c472017-11-27 10:44:06 -0500803##
804
805#SeeAlso operator*(SkScalar scale)_const scale() setLength setNormalize
806
807#Method ##
808
809# ------------------------------------------------------------------------------
810
811#Method bool isFinite() const
Cary Clark4855f782018-02-06 09:41:53 -0500812#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500813#Line # returns true if no member is infinite or NaN ##
Cary Clarka560c472017-11-27 10:44:06 -0500814Returns true if both fX and fY are measurable values.
815
816#Return true for values other than infinities and NaN ##
817
818#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500819SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
820for (const SkPoint& pt : test) {
821 SkDebugf("pt: %g, %g finite: %s\n", pt.fX, pt.fY, pt.isFinite() ? "true" : "false");
Cary Clarka560c472017-11-27 10:44:06 -0500822}
823#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500824pt: 0, -0 finite: true
825pt: -1, -2 finite: true
826pt: inf, 1 finite: false
Cary Clarka560c472017-11-27 10:44:06 -0500827pt: 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
Cary Clark4855f782018-02-06 09:41:53 -0500838#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500839#Line # returns true if Points are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500840Returns 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500848SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
849for (const SkPoint& pt : test) {
850 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt.equals(pt.fX, pt.fY) ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500851}
852#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500853pt: 0, -0 == pt
854pt: -1, -2 == pt
855pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500856pt: 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
Cary Clark08895c42018-02-01 09:37:32 -0500868#Line # returns true if Point are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500869Returns 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500877SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
878for (const SkPoint& pt : test) {
879 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt == pt ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500880}
881#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500882pt: 0, -0 == pt
883pt: -1, -2 == pt
884pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500885pt: 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
Cary Clark08895c42018-02-01 09:37:32 -0500897#Line # returns true if Point are unequal ##
Cary Clarka560c472017-11-27 10:44:06 -0500898Returns 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500906SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
907for (const SkPoint& pt : test) {
908 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt != pt ? '!' : '=');
Cary Clarka560c472017-11-27 10:44:06 -0500909}
910#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500911pt: 0, -0 == pt
912pt: -1, -2 == pt
913pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500914pt: 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
Cary Clark08895c42018-02-01 09:37:32 -0500926#Line # returns Vector between Points ##
Cary Clarka560c472017-11-27 10:44:06 -0500927Returns Vector from b to a, computed as
928#Formula
929(a.fX - b.fX, a.fY - b.fY)
930##
931.
932
933Can also be used to subtract Vector from Point, returning Point.
934Can 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500942 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);
Cary Clarka560c472017-11-27 10:44:06 -0500955##
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
Cary Clark08895c42018-02-01 09:37:32 -0500965#Line # returns Point offset by Vector ##
Cary Clarka560c472017-11-27 10:44:06 -0500966Returns 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
972Can also be used to offset Point b by Vector a, returning Point.
973Can 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
Cary Clark0c5f5462017-12-15 11:21:51 -0500981 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);
Cary Clarka560c472017-11-27 10:44:06 -0500998##
999
1000#SeeAlso operator+=(const SkVector& v) offset()
1001
1002#Method ##
1003
1004# ------------------------------------------------------------------------------
1005
1006#Method static SkScalar Length(SkScalar x, SkScalar y)
Cary Clark4855f782018-02-06 09:41:53 -05001007#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001008#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -05001009Returns the Euclidean_Distance from origin, computed as:
1010#Code
1011#Literal
1012sqrt(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
Cary Clark0c5f5462017-12-15 11:21:51 -05001023 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 }
Cary Clarka560c472017-11-27 10:44:06 -05001036##
1037
1038#SeeAlso length() Distance setLength
1039
1040#Method ##
1041
1042# ------------------------------------------------------------------------------
1043
1044#Method static SkScalar Normalize(SkVector* vec)
Cary Clark4855f782018-02-06 09:41:53 -05001045#In Offset
Cary Clark08895c42018-02-01 09:37:32 -05001046#Line # sets length to one, and returns prior length ##
Cary Clarka560c472017-11-27 10:44:06 -05001047Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX to vec->fY,
1048if possible. If original length is nearly zero, sets vec to (0, 0) and returns zero;
1049otherwise, returns length of vec before vec is scaled.
1050
1051Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar.
1052
1053Note 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
Cary Clark0c5f5462017-12-15 11:21:51 -05001060 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 }
Cary Clarka560c472017-11-27 10:44:06 -05001076##
1077
1078#SeeAlso normalize() setLength Length
1079
1080#Method ##
1081
1082# ------------------------------------------------------------------------------
1083
1084#Method static SkScalar Distance(const SkPoint& a, const SkPoint& b)
Cary Clark4855f782018-02-06 09:41:53 -05001085#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001086#Line # returns straight-line distance between points ##
Cary Clarka560c472017-11-27 10:44:06 -05001087Returns 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
Cary Clark0c5f5462017-12-15 11:21:51 -05001096 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 }
Cary Clarka560c472017-11-27 10:44:06 -05001111##
1112
1113#SeeAlso length() setLength
1114
1115#Method ##
1116
1117# ------------------------------------------------------------------------------
1118
1119#Method static SkScalar DotProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001120#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001121#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001122Returns 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
Cary Clark0c5f5462017-12-15 11:21:51 -05001130 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 }
Cary Clarka560c472017-11-27 10:44:06 -05001143##
1144
1145#SeeAlso dot CrossProduct
1146
1147#Method ##
1148
1149# ------------------------------------------------------------------------------
1150
1151#Method static SkScalar CrossProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001152#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001153#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001154Returns the cross product of Vector a and Vector b.
1155
Cary Clark4855f782018-02-06 09:41:53 -05001156a and b form three-dimensional vectors with z-axis value equal to zero. The
1157cross product is a three-dimensional vector with x-axis and y-axis values equal
1158to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001159
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
Cary Clark0c5f5462017-12-15 11:21:51 -05001166 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 }
Cary Clarka560c472017-11-27 10:44:06 -05001183##
1184
1185#SeeAlso cross DotProduct
1186
1187#Method ##
1188
1189# ------------------------------------------------------------------------------
1190
1191#Method SkScalar cross(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001192#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001193#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001194Returns the cross product of Vector and vec.
1195
Cary Clark4855f782018-02-06 09:41:53 -05001196Vector and vec form three-dimensional vectors with z-axis value equal to zero.
1197The cross product is a three-dimensional vector with x-axis and y-axis values
1198equal to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001199
1200#Param vec right side of cross product ##
1201
1202#Return area spanned by Vectors signed by angle direction ##
1203
1204#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001205 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 }
Cary Clarka560c472017-11-27 10:44:06 -05001222##
1223
1224#SeeAlso CrossProduct dot
1225
1226#Method ##
1227
1228# ------------------------------------------------------------------------------
1229
1230#Method SkScalar dot(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001231#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001232#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001233Returns 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
Cary Clark0c5f5462017-12-15 11:21:51 -05001240 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 }
Cary Clarka560c472017-11-27 10:44:06 -05001253##
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##