blob: 598f963ca98109cd17b6fbedf24503709b46697e [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Point
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Points ##
3#Alias Point_Reference ##
Cary Clarka560c472017-11-27 10:44:06 -05004
Cary Clark08895c42018-02-01 09:37:32 -05005#Struct SkPoint
6
Cary Clark682c58d2018-05-16 07:07:07 -04007SkPoint holds two 32-bit floating point coordinates.
8
9#Subtopic Overview
10#Populate
11##
Cary Clark4855f782018-02-06 09:41:53 -050012
13#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050014#Populate
15##
Cary Clark5081eed2018-01-22 07:55:48 -050016
Cary Clark4855f782018-02-06 09:41:53 -050017#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050018#Populate
19##
Cary Clarka560c472017-11-27 10:44:06 -050020
Cary Clark4855f782018-02-06 09:41:53 -050021#Subtopic Member
Cary Clark08895c42018-02-01 09:37:32 -050022#Populate
Cary Clarka560c472017-11-27 10:44:06 -050023
24#Member SkScalar fX
Cary Clark08895c42018-02-01 09:37:32 -050025#Line # x-axis value ##
Cary Clarka560c472017-11-27 10:44:06 -050026x-axis value used by both Point and Vector. May contain any value, including
27infinities and NaN.
28##
29
30#Member SkScalar fY
Cary Clark08895c42018-02-01 09:37:32 -050031#Line # y-axis value ##
Cary Clarka560c472017-11-27 10:44:06 -050032y-axis value used by both Point and Vector. May contain any value, including
33infinities and NaN.
34##
35
Cary Clark4855f782018-02-06 09:41:53 -050036#Subtopic Member ##
Cary Clark08895c42018-02-01 09:37:32 -050037
Cary Clarka560c472017-11-27 10:44:06 -050038# ------------------------------------------------------------------------------
39
Cary Clark4855f782018-02-06 09:41:53 -050040#Subtopic Constructor
41#Populate
42##
Cary Clarka560c472017-11-27 10:44:06 -050043
Cary Clark4855f782018-02-06 09:41:53 -050044#Method static constexpr SkPoint Make(SkScalar x, SkScalar y)
45#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -050046#Line # constructs from SkScalar inputs ##
Cary Clarka560c472017-11-27 10:44:06 -050047Sets 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
Cary Clark4855f782018-02-06 09:41:53 -050071#Subtopic Property
72#Line # member values ##
73#Populate
74##
Cary Clarka560c472017-11-27 10:44:06 -050075
Cary Clark4855f782018-02-06 09:41:53 -050076#Method SkScalar x() const
77#In Property
Cary Clark08895c42018-02-01 09:37:32 -050078#Line # returns fX ##
Cary Clarka560c472017-11-27 10:44:06 -050079Returns x-axis value of Point or Vector.
80
81#Return fX ##
82
83#Example
84SkPoint pt1 = {45, 66};
85SkDebugf("pt1.fX %c= pt1.x()\n", pt1.fX == pt1.x() ? '=' : '!');
86#StdOut
87pt1.fX == pt1.x()
88##
89##
90
91#SeeAlso y() SkIPoint::x()
92
93#Method ##
94
95# ------------------------------------------------------------------------------
96
97#Method SkScalar y() const
Cary Clark4855f782018-02-06 09:41:53 -050098#In Property
Cary Clark08895c42018-02-01 09:37:32 -050099#Line # returns fY ##
Cary Clarka560c472017-11-27 10:44:06 -0500100Returns y-axis value of Point or Vector.
101
102#Return fY ##
103
104#Example
105SkPoint pt1 = {45, 66};
106SkDebugf("pt1.fY %c= pt1.y()\n", pt1.fY == pt1.y() ? '=' : '!');
107#StdOut
108pt1.fY == pt1.y()
109##
110##
111
112#SeeAlso x() SkIPoint::y()
113
114#Method ##
115
116# ------------------------------------------------------------------------------
117
118#Method bool isZero() const
Cary Clark4855f782018-02-06 09:41:53 -0500119#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500120#Line # returns true if both members equal zero ##
Cary Clarka560c472017-11-27 10:44:06 -0500121Returns true if fX and fY are both zero.
122
123#Return true if fX is zero and fY is zero ##
124
125#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500126SkPoint pt = { 0.f, -0.f};
127SkDebugf("pt.fX=%c%g pt.fY=%c%g\n", std::signbit(pt.fX) ? '-' : '+', fabsf(pt.fX),
128 std::signbit(pt.fY) ? '-' : '+', fabsf(pt.fY));
Cary Clarka560c472017-11-27 10:44:06 -0500129SkDebugf("pt.isZero() == %s\n", pt.isZero() ? "true" : "false");
130#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500131pt.fX=+0 pt.fY=-0
Cary Clarka560c472017-11-27 10:44:06 -0500132pt.isZero() == true
133##
134##
135
136#SeeAlso isFinite SkIPoint::isZero
137
138#Method ##
139
140# ------------------------------------------------------------------------------
141
Cary Clark4855f782018-02-06 09:41:53 -0500142#Subtopic Set
143#Populate
144#Line # replaces all values ##
145##
Cary Clarka560c472017-11-27 10:44:06 -0500146
Cary Clark4855f782018-02-06 09:41:53 -0500147#Method void set(SkScalar x, SkScalar y)
148#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500149#Line # sets to SkScalar input ##
Cary Clarka560c472017-11-27 10:44:06 -0500150Sets fX to x and fY to y.
151
152#Param x new value for fX ##
153#Param y new value for fY ##
154
155#Example
156SkPoint pt1, pt2 = { SK_ScalarPI, SK_ScalarSqrt2 };
157pt1.set(SK_ScalarPI, SK_ScalarSqrt2);
158SkDebugf("pt1 %c= pt2\n", pt1 == pt2 ? '=' : '!');
159#StdOut
160pt1 == pt2
161##
162##
163
164#SeeAlso iset() Make
165
166#Method ##
167
168# ------------------------------------------------------------------------------
169
170#Method void iset(int32_t x, int32_t y)
Cary Clark4855f782018-02-06 09:41:53 -0500171#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500172#Line # sets to integer input ##
Cary Clarka560c472017-11-27 10:44:06 -0500173Sets fX to x and fY to y, promoting integers to SkScalar values.
174
Cary Clark682c58d2018-05-16 07:07:07 -0400175Assigning a large integer value directly to fX or fY may cause a compiler
Cary Clarka560c472017-11-27 10:44:06 -0500176error, triggered by narrowing conversion of int to SkScalar. This safely
Cary Clark682c58d2018-05-16 07:07:07 -0400177casts x and y to avoid the error.
Cary Clarka560c472017-11-27 10:44:06 -0500178
179#Param x new value for fX ##
180#Param y new value for fY ##
181
182#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500183SkPoint pt1, pt2 = { SK_MinS16, SK_MaxS16 };
184pt1.iset(SK_MinS16, SK_MaxS16);
Cary Clarka560c472017-11-27 10:44:06 -0500185SkDebugf("pt1 %c= pt2\n", pt1 == pt2 ? '=' : '!');
186##
187
188#SeeAlso set Make SkIPoint::set
189
190#Method ##
191
192# ------------------------------------------------------------------------------
193
194#Method void iset(const SkIPoint& p)
195
196Sets fX to p.fX and fY to p.fY, promoting integers to SkScalar values.
197
198Assigning an IPoint containing a large integer value directly to fX or fY may
199cause a compiler error, triggered by narrowing conversion of int to SkScalar.
Cary Clark682c58d2018-05-16 07:07:07 -0400200This safely casts p.fX and p.fY to avoid the error.
Cary Clarka560c472017-11-27 10:44:06 -0500201
202#Param p IPoint members promoted to SkScalar ##
203
204#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500205SkIPoint iPt = { SK_MinS32, SK_MaxS32 };
206SkPoint fPt;
207fPt.iset(iPt);
Cary Clarka560c472017-11-27 10:44:06 -0500208SkDebugf("iPt: %d, %d\n", iPt.fX, iPt.fY);
209SkDebugf("fPt: %g, %g\n", fPt.fX, fPt.fY);
210#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500211iPt: -2147483647, 2147483647
Cary Clarka560c472017-11-27 10:44:06 -0500212fPt: -2.14748e+09, 2.14748e+09
213##
214##
215
216#SeeAlso set Make SkIPoint::set
217
218#Method ##
219
220# ------------------------------------------------------------------------------
221
222#Method void setAbs(const SkPoint& pt)
Cary Clark4855f782018-02-06 09:41:53 -0500223#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500224#Line # sets sign of both members to positive ##
Cary Clarka560c472017-11-27 10:44:06 -0500225Sets fX to absolute value of pt.fX; and fY to absolute value of pt.fY.
226
227#Param pt members providing magnitude for fX and fY ##
228
229#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500230SkPoint test[] = { {0.f, -0.f}, {-1, -2},
231 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
232 { SK_ScalarNaN, -SK_ScalarNaN } };
233for (const SkPoint& pt : test) {
234 SkPoint absPt;
235 absPt.setAbs(pt);
236 SkDebugf("pt: %g, %g abs: %g, %g\n", pt.fX, pt.fY, absPt.fX, absPt.fY);
Cary Clarka560c472017-11-27 10:44:06 -0500237}
238#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500239pt: 0, -0 abs: 0, 0
240pt: -1, -2 abs: 1, 2
241pt: inf, -inf abs: inf, inf
Cary Clarka560c472017-11-27 10:44:06 -0500242pt: nan, -nan abs: nan, nan
243##
244##
245
Cary Clark682c58d2018-05-16 07:07:07 -0400246#SeeAlso set Make negate
Cary Clarka560c472017-11-27 10:44:06 -0500247
248#Method ##
249
250# ------------------------------------------------------------------------------
251
Cary Clark4855f782018-02-06 09:41:53 -0500252#Subtopic Offset
253#Line # moves sides ##
254#Populate
255##
Cary Clarka560c472017-11-27 10:44:06 -0500256
Cary Clark4855f782018-02-06 09:41:53 -0500257#Method static void Offset(SkPoint points[], int count, const SkVector& offset)
258#In Offset
Cary Clark08895c42018-02-01 09:37:32 -0500259#Line # translates Point array ##
Cary Clarka560c472017-11-27 10:44:06 -0500260Adds offset to each Point in points array with count entries.
261
262#Param points Point array ##
263#Param count entries in array ##
264#Param offset Vector added to points ##
265
266#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500267 SkPaint paint;
268 paint.setAntiAlias(true);
269 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
270 { 6, 4 }, { 7, 5 }, { 5, 7 },
271 { 4, 6 }, { 3, 7 }, { 1, 5 },
272 { 2, 4 }, { 1, 3 }, { 3, 1 } };
273 canvas->scale(30, 15);
274 paint.setStyle(SkPaint::kStroke_Style);
275 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
276 SkPoint::Offset(points, SK_ARRAY_COUNT(points), { 1, 9 } );
277 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500278##
279
280#SeeAlso offset operator+=(const SkVector& v)
281
282#Method ##
283
284# ------------------------------------------------------------------------------
285
286#Method static void Offset(SkPoint points[], int count, SkScalar dx, SkScalar dy)
287
288Adds offset (dx, dy) to each Point in points array of length count.
289
290#Param points Point array ##
291#Param count entries in array ##
292#Param dx added to fX in points ##
293#Param dy added to fY in points ##
294
295#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500296 SkPaint paint;
297 paint.setAntiAlias(true);
298 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
299 { 6, 4 }, { 7, 5 }, { 5, 7 },
300 { 4, 6 }, { 3, 7 }, { 1, 5 },
301 { 2, 4 }, { 1, 3 }, { 3, 1 } };
302 canvas->scale(30, 15);
303 paint.setStyle(SkPaint::kStroke_Style);
304 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
305 SkPoint::Offset(points, SK_ARRAY_COUNT(points), 1, 9);
306 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500307##
308
309#SeeAlso offset operator+=(const SkVector& v)
310
311#Method ##
312
313# ------------------------------------------------------------------------------
314
315#Method void offset(SkScalar dx, SkScalar dy)
Cary Clark4855f782018-02-06 09:41:53 -0500316#In Offset
Cary Clark08895c42018-02-01 09:37:32 -0500317#Line # translates Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500318Adds offset (dx, dy) to Point.
319
320#Param dx added to fX ##
321#Param dy added to fY ##
322
323#Example
324#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500325 SkPaint paint;
326 paint.setAntiAlias(true);
327 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
328 { 6, 4 }, { 7, 5 }, { 5, 7 },
329 { 4, 6 }, { 3, 7 }, { 1, 5 },
330 { 2, 4 }, { 1, 3 }, { 3, 1 } };
331 canvas->scale(30, 15);
332 paint.setStyle(SkPaint::kStroke_Style);
333 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
334 points[1].offset(1, 1);
335 paint.setColor(SK_ColorRED);
336 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500337##
338
339#SeeAlso Offset operator+=(const SkVector& v)
340
341#Method ##
342
343# ------------------------------------------------------------------------------
344
345#Method SkScalar length() const
Cary Clark4855f782018-02-06 09:41:53 -0500346#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500347#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -0500348Returns the Euclidean_Distance from origin, computed as:
349#Code
350#Literal
351sqrt(fX * fX + fY * fY)
352##
353.
354
355#Return straight-line distance to origin ##
356
357#Example
358#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -0500359 SkPaint paint;
360 paint.setAntiAlias(true);
361 const SkPoint points[] = { { 90, 30 }, { 120, 150 }, { 150, 30 }, { 210, 90 } };
362 const SkPoint origin = {30, 140};
363 for (auto point : points) {
364 canvas->drawLine(origin, point, paint);
365 SkAutoCanvasRestore acr(canvas, true);
366 SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX);
367 canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY);
368 SkString length("length = ");
369 length.appendScalar(point.length());
370 canvas->drawString(length, origin.fX + 25, origin.fY - 4, paint);
371 }
Cary Clarka560c472017-11-27 10:44:06 -0500372##
373
374#SeeAlso distanceToOrigin Length setLength Distance
375
376#Method ##
377
378# ------------------------------------------------------------------------------
379
380#Method SkScalar distanceToOrigin() const
Cary Clark4855f782018-02-06 09:41:53 -0500381#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500382#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -0500383Returns the Euclidean_Distance from origin, computed as:
384#Code
385#Literal
386sqrt(fX * fX + fY * fY)
387##
388.
389
390#Return straight-line distance to origin ##
391
392#Example
393#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -0500394 SkPaint paint;
395 paint.setAntiAlias(true);
396 const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } };
397 const SkPoint origin = {0, 0};
398 canvas->translate(30, 140);
399 for (auto point : points) {
400 canvas->drawLine(origin, point, paint);
401 SkAutoCanvasRestore acr(canvas, true);
402 SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX);
403 canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY);
404 SkString distance("distance = ");
405 distance.appendScalar(point.distanceToOrigin());
406 canvas->drawString(distance, origin.fX + 25, origin.fY - 4, paint);
407 }
Cary Clarka560c472017-11-27 10:44:06 -0500408##
409
410#SeeAlso length Length setLength Distance
411
412#Method ##
413
414# ------------------------------------------------------------------------------
415
416#Method bool normalize()
Cary Clark4855f782018-02-06 09:41:53 -0500417#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500418#Line # sets length to one, preserving direction ##
Cary Clarka560c472017-11-27 10:44:06 -0500419Scales (fX, fY) so that length() returns one, while preserving ratio of fX to fY,
420if possible. If prior length is nearly zero, sets Vector to (0, 0) and returns
421false; otherwise returns true.
422
423#Return true if former length is not zero or nearly zero ##
424
425#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500426 SkPaint paint;
427 paint.setAntiAlias(true);
428 const SkPoint lines[][2] = { {{ 30, 110 }, { 190, 30 }},
429 {{ 120, 140 }, { 30, 220 }}};
430 for (auto line : lines) {
431 canvas->drawLine(line[0], line[1], paint);
432 SkVector vector = line[1] - line[0];
433 if (vector.normalize()) {
434 SkVector rotate90 = { -vector.fY, vector.fX };
435 rotate90 *= 10.f;
436 canvas->drawLine(line[0] - rotate90, line[0] + rotate90, paint);
437 canvas->drawLine(line[1] - rotate90, line[1] + rotate90, paint);
438 }
439 }
Cary Clarka560c472017-11-27 10:44:06 -0500440##
441
442#SeeAlso Normalize setLength length Length
443
444#Method ##
445
446# ------------------------------------------------------------------------------
447
448#Method bool setNormalize(SkScalar x, SkScalar y)
Cary Clark4855f782018-02-06 09:41:53 -0500449#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500450#Line # sets length to one, in direction of (x, y) ##
Cary Clarka560c472017-11-27 10:44:06 -0500451Sets Vector to (x, y) scaled so length() returns one, and so that
452(fX, fY) is proportional to (x, y). If (x, y) length is nearly zero,
453sets Vector to (0, 0) and returns false; otherwise returns true.
454
455#Param x proportional value for fX ##
456#Param y proportional value for fY ##
457
458#Return true if (x, y) length is not zero or nearly zero ##
459
460#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500461 SkPaint paint;
462 paint.setAntiAlias(true);
463 const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } };
464 const SkPoint origin = {0, 0};
465 canvas->translate(30, 140);
466 for (auto point : points) {
467 paint.setStrokeWidth(1);
468 paint.setColor(SK_ColorBLACK);
469 canvas->drawLine(origin, point, paint);
470 SkVector normal;
471 normal.setNormalize(point.fX, point.fY);
472 normal *= 100;
473 paint.setStrokeWidth(10);
474 paint.setColor(0x3f4512bf);
475 canvas->drawLine(origin, normal, paint);
476 }
Cary Clarka560c472017-11-27 10:44:06 -0500477##
478
479#SeeAlso normalize setLength
480
481#Method ##
482
483# ------------------------------------------------------------------------------
484
485#Method bool setLength(SkScalar length)
Cary Clark4855f782018-02-06 09:41:53 -0500486#In Set
Cary Clark08895c42018-02-01 09:37:32 -0500487#Line # sets straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -0500488Scales Vector so that distanceToOrigin returns length, if possible. If former
489length is nearly zero, sets Vector to (0, 0) and return false; otherwise returns
490true.
491
492#Param length straight-line distance to origin ##
493
494#Return true if former length is not zero or nearly zero ##
495
496#Example
497#Height 160
Cary Clark0c5f5462017-12-15 11:21:51 -0500498 SkPaint paint;
499 paint.setAntiAlias(true);
500 const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } };
501 const SkPoint origin = {0, 0};
502 canvas->translate(30, 140);
503 for (auto point : points) {
504 paint.setStrokeWidth(1);
505 paint.setColor(SK_ColorBLACK);
506 canvas->drawLine(origin, point, paint);
507 SkVector normal = point;
508 normal.setLength(100);
509 paint.setStrokeWidth(10);
510 paint.setColor(0x3f45bf12);
511 canvas->drawLine(origin, normal, paint);
512 }
Cary Clarka560c472017-11-27 10:44:06 -0500513##
514
515#SeeAlso length Length setNormalize setAbs
516
517#Method ##
518
519# ------------------------------------------------------------------------------
520
521#Method bool setLength(SkScalar x, SkScalar y, SkScalar length)
522
523Sets Vector to (x, y) scaled to length, if possible. If former
524length is nearly zero, sets Vector to (0, 0) and return false; otherwise returns
525true.
526
527#Param x proportional value for fX ##
528#Param y proportional value for fY ##
529#Param length straight-line distance to origin ##
530
531#Return true if (x, y) length is not zero or nearly zero ##
532
533#Example
534#Height 160
Cary Clark0c5f5462017-12-15 11:21:51 -0500535 SkPaint paint;
536 paint.setAntiAlias(true);
537 const SkPoint points[] = { { 60, -110 }, { 90, 10 }, { 120, -110 }, { 180, -50 } };
538 const SkPoint origin = {0, 0};
539 canvas->translate(30, 140);
540 for (auto point : points) {
541 paint.setStrokeWidth(1);
542 paint.setColor(SK_ColorBLACK);
543 canvas->drawLine(origin, point, paint);
544 SkVector normal;
545 normal.setLength(point.fX, point.fY, 100);
546 paint.setStrokeWidth(10);
547 paint.setColor(0x3fbf4512);
548 canvas->drawLine(origin, normal, paint);
549 }
Cary Clarka560c472017-11-27 10:44:06 -0500550##
551
552#SeeAlso length Length setNormalize setAbs
553
554#Method ##
555
556# ------------------------------------------------------------------------------
557
Cary Clark4855f782018-02-06 09:41:53 -0500558#Subtopic Operator
559#Populate
560##
Cary Clarka560c472017-11-27 10:44:06 -0500561
Cary Clark4855f782018-02-06 09:41:53 -0500562#Method void scale(SkScalar scale, SkPoint* dst) const
563#In Offset
564#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500565#Line # multiplies Point by scale factor ##
Cary Clarka560c472017-11-27 10:44:06 -0500566Sets dst to Point times scale. dst may be Point to modify Point in place.
567
568#Param scale factor to multiply Point by ##
569#Param dst storage for scaled Point ##
570
571#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500572 SkPaint paint;
573 paint.setAntiAlias(true);
574 SkPoint point = {40, -15}, scaled;
575 SkPoint origin = {30, 110};
576 for (auto scale : {1, 2, 3, 5}) {
577 paint.setStrokeWidth(scale * 5);
578 paint.setARGB(0x7f, 0x9f, 0xbf, 0x33 * scale);
579 point.scale(scale, &scaled);
580 canvas->drawLine(origin, origin + scaled, paint);
581 }
Cary Clarka560c472017-11-27 10:44:06 -0500582##
583
584#SeeAlso operator*(SkScalar scale)_const operator*=(SkScalar scale) setLength
585
586#Method ##
587
588# ------------------------------------------------------------------------------
589
590#Method void scale(SkScalar value)
591
592Scales Point in place by scale.
593
594#Param value factor to multiply Point by ##
595
596#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500597 SkPaint paint;
598 paint.setAntiAlias(true);
599 SkPoint point = {40, -15};
600 SkPoint origin = {30, 110};
601 for (auto scale : {1, 2, 3, 5}) {
602 paint.setStrokeWidth(scale * 5);
603 paint.setARGB(0x7f, 0x9f, 0xbf, 0x33 * scale);
604 point.scale(scale);
605 canvas->drawLine(origin, origin + point, paint);
606 }
Cary Clarka560c472017-11-27 10:44:06 -0500607##
608
609#SeeAlso operator*(SkScalar scale)_const operator*=(SkScalar scale) setLength
610
611#Method ##
612
613# ------------------------------------------------------------------------------
614
615#Method void negate()
Cary Clark4855f782018-02-06 09:41:53 -0500616#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500617#Line # reverses the sign of both members ##
Cary Clarka560c472017-11-27 10:44:06 -0500618Changes the sign of fX and fY.
619
620#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500621SkPoint test[] = { {0.f, -0.f}, {-1, -2},
622 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
623 { SK_ScalarNaN, -SK_ScalarNaN } };
624for (const SkPoint& pt : test) {
625 SkPoint negPt = pt;
626 negPt.negate();
627 SkDebugf("pt: %g, %g negate: %g, %g\n", pt.fX, pt.fY, negPt.fX, negPt.fY);
Cary Clarka560c472017-11-27 10:44:06 -0500628}
629#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500630pt: 0, -0 negate: -0, 0
631pt: -1, -2 negate: 1, 2
632pt: inf, -inf negate: -inf, inf
Cary Clarka560c472017-11-27 10:44:06 -0500633pt: nan, -nan negate: -nan, nan
634##
635##
636
637#SeeAlso operator-()_const setAbs
638
639#Method ##
640
641# ------------------------------------------------------------------------------
642
643#Method SkPoint operator-()_const
644
Cary Clark08895c42018-02-01 09:37:32 -0500645#Line # reverses sign of Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500646Returns Point changing the signs of fX and fY.
647
648#Return Point as (-fX, -fY) ##
649
650#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500651SkPoint test[] = { {0.f, -0.f}, {-1, -2},
652 { SK_ScalarInfinity, SK_ScalarNegativeInfinity },
653 { SK_ScalarNaN, -SK_ScalarNaN } };
654for (const SkPoint& pt : test) {
655 SkPoint negPt = -pt;
656 SkDebugf("pt: %g, %g negate: %g, %g\n", pt.fX, pt.fY, negPt.fX, negPt.fY);
Cary Clarka560c472017-11-27 10:44:06 -0500657}
658#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500659pt: 0, -0 negate: -0, 0
660pt: -1, -2 negate: 1, 2
661pt: inf, -inf negate: -inf, inf
Cary Clarka560c472017-11-27 10:44:06 -0500662pt: nan, -nan negate: -nan, nan
663##
664##
665
666#SeeAlso negate operator-(const SkPoint& a, const SkPoint& b) operator-=(const SkVector& v) SkIPoint::operator-()_const
667
668#Method ##
669
670# ------------------------------------------------------------------------------
671
672#Method void operator+=(const SkVector& v)
673
Cary Clark08895c42018-02-01 09:37:32 -0500674#Line # adds Vector to Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500675Adds Vector v to Point. Sets Point to:
676#Formula
677(fX + v.fX, fY + v.fY)
678##
679.
680
681#Param v Vector to add ##
682
683#Example
684#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500685 SkPaint paint;
686 paint.setAntiAlias(true);
687 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
688 { 6, 4 }, { 7, 5 }, { 5, 7 },
689 { 4, 6 }, { 3, 7 }, { 1, 5 },
690 { 2, 4 }, { 1, 3 }, { 3, 1 } };
691 canvas->scale(30, 15);
692 paint.setStyle(SkPaint::kStroke_Style);
693 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
694 points[1] += {1, 1};
695 points[2] += {-1, -1};
696 paint.setColor(SK_ColorRED);
697 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500698##
699
700#SeeAlso offset() operator+(const SkPoint& a, const SkVector& b) SkIPoint::operator+=(const SkIVector& v)
701
702#Method ##
703
704# ------------------------------------------------------------------------------
705
706#Method void operator-=(const SkVector& v)
707
Cary Clark08895c42018-02-01 09:37:32 -0500708#Line # subtracts Vector from Point ##
Cary Clarka560c472017-11-27 10:44:06 -0500709Subtracts Vector v from Point. Sets Point to:
710#Formula
711(fX - v.fX, fY - v.fY)
712##
713.
714
715#Param v Vector to subtract ##
716
717#Example
718#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500719 SkPaint paint;
720 paint.setAntiAlias(true);
721 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
722 { 6, 4 }, { 7, 5 }, { 5, 7 },
723 { 4, 6 }, { 3, 7 }, { 1, 5 },
724 { 2, 4 }, { 1, 3 }, { 3, 1 } };
725 canvas->scale(30, 15);
726 paint.setStyle(SkPaint::kStroke_Style);
727 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
728 points[1] -= {1, 1};
729 points[2] -= {-1, -1};
730 paint.setColor(SK_ColorRED);
731 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500732##
733
734#SeeAlso offset() operator-(const SkPoint& a, const SkPoint& b) SkIPoint::operator-=(const SkIVector& v)
735
736#Method ##
737
738# ------------------------------------------------------------------------------
739
740#Method SkPoint operator*(SkScalar scale)_const
741
Cary Clark08895c42018-02-01 09:37:32 -0500742#Line # returns Point multiplied by scale ##
Cary Clarka560c472017-11-27 10:44:06 -0500743Returns Point multiplied by scale.
744
745#Param scale Scalar to multiply by ##
746
747#Return Point as (fX * scale, fY * scale) ##
748
749#Example
750#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500751 SkPaint paint;
752 paint.setAntiAlias(true);
753 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
754 { 6, 4 }, { 7, 5 }, { 5, 7 },
755 { 4, 6 }, { 3, 7 }, { 1, 5 },
756 { 2, 4 }, { 1, 3 }, { 3, 1 } };
757 canvas->scale(15, 10);
758 paint.setStyle(SkPaint::kStroke_Style);
759 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
760 for (auto& point : points) {
761 point = point * 1.5f;
762 }
763 paint.setColor(SK_ColorRED);
764 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500765##
766
767#SeeAlso operator*=(SkScalar scale) scale() setLength setNormalize
768
769#Method ##
770
771# ------------------------------------------------------------------------------
772
773#Method SkPoint& operator*=(SkScalar scale)
774
Cary Clark08895c42018-02-01 09:37:32 -0500775#Line # multiplies Point by scale factor ##
Cary Clarka560c472017-11-27 10:44:06 -0500776Multiplies Point by scale. Sets Point to:
777#Formula
778(fX * scale, fY * scale)
779##
Cary Clark80247e52018-07-11 16:18:41 -0400780.
Cary Clarka560c472017-11-27 10:44:06 -0500781
782#Param scale Scalar to multiply by ##
783
784#Return reference to Point ##
785
786#Example
787#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500788 SkPaint paint;
789 paint.setAntiAlias(true);
790 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
791 { 6, 4 }, { 7, 5 }, { 5, 7 },
792 { 4, 6 }, { 3, 7 }, { 1, 5 },
793 { 2, 4 }, { 1, 3 }, { 3, 1 } };
794 canvas->scale(15, 10);
795 paint.setStyle(SkPaint::kStroke_Style);
796 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
797 for (auto& point : points) {
798 point *= 2;
799 }
800 paint.setColor(SK_ColorRED);
801 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500802##
803
804#SeeAlso operator*(SkScalar scale)_const scale() setLength setNormalize
805
806#Method ##
807
808# ------------------------------------------------------------------------------
809
810#Method bool isFinite() const
Cary Clark4855f782018-02-06 09:41:53 -0500811#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500812#Line # returns true if no member is infinite or NaN ##
Cary Clarka560c472017-11-27 10:44:06 -0500813Returns true if both fX and fY are measurable values.
814
815#Return true for values other than infinities and NaN ##
816
817#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500818SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
819for (const SkPoint& pt : test) {
820 SkDebugf("pt: %g, %g finite: %s\n", pt.fX, pt.fY, pt.isFinite() ? "true" : "false");
Cary Clarka560c472017-11-27 10:44:06 -0500821}
822#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500823pt: 0, -0 finite: true
824pt: -1, -2 finite: true
825pt: inf, 1 finite: false
Cary Clarka560c472017-11-27 10:44:06 -0500826pt: nan, -1 finite: false
827##
828##
829
830#SeeAlso SkRect::isFinite SkPath::isFinite
831
832#Method ##
833
834# ------------------------------------------------------------------------------
835
836#Method bool equals(SkScalar x, SkScalar y) const
Cary Clark4855f782018-02-06 09:41:53 -0500837#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500838#Line # returns true if Points are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500839Returns true if Point is equivalent to Point constructed from (x, y).
840
841#Param x value compared with fX ##
842#Param y value compared with fY ##
843
844#Return true if Point equals (x, y) ##
845
846#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500847SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
848for (const SkPoint& pt : test) {
849 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt.equals(pt.fX, pt.fY) ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500850}
851#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500852pt: 0, -0 == pt
853pt: -1, -2 == pt
854pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500855pt: nan, -1 != pt
856##
857##
858
859#SeeAlso operator==(const SkPoint& a, const SkPoint& b)
860
861#Method ##
862
863# ------------------------------------------------------------------------------
864
865#Method bool operator==(const SkPoint& a, const SkPoint& b)
866
Cary Clark08895c42018-02-01 09:37:32 -0500867#Line # returns true if Point are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500868Returns true if a is equivalent to b.
869
870#Param a Point to compare ##
871#Param b Point to compare ##
872
873#Return true if a.fX == b.fX and a.fY == b.fY ##
874
875#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500876SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
877for (const SkPoint& pt : test) {
878 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt == pt ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500879}
880#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500881pt: 0, -0 == pt
882pt: -1, -2 == pt
883pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500884pt: nan, -1 != pt
885##
886##
887
888#SeeAlso equals() operator!=(const SkPoint& a, const SkPoint& b)
889
890#Method ##
891
892# ------------------------------------------------------------------------------
893
894#Method bool operator!=(const SkPoint& a, const SkPoint& b)
895
Cary Clark08895c42018-02-01 09:37:32 -0500896#Line # returns true if Point are unequal ##
Cary Clarka560c472017-11-27 10:44:06 -0500897Returns true if a is not equivalent to b.
898
899#Param a Point to compare ##
900#Param b Point to compare ##
901
902#Return true if a.fX != b.fX or a.fY != b.fY ##
903
904#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500905SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
906for (const SkPoint& pt : test) {
907 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt != pt ? '!' : '=');
Cary Clarka560c472017-11-27 10:44:06 -0500908}
909#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500910pt: 0, -0 == pt
911pt: -1, -2 == pt
912pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500913pt: nan, -1 != pt
914##
915##
916
917#SeeAlso operator==(const SkPoint& a, const SkPoint& b) equals()
918
919#Method ##
920
921# ------------------------------------------------------------------------------
922
923#Method SkVector operator-(const SkPoint& a, const SkPoint& b)
924
Cary Clark08895c42018-02-01 09:37:32 -0500925#Line # returns Vector between Points ##
Cary Clarka560c472017-11-27 10:44:06 -0500926Returns Vector from b to a, computed as
927#Formula
928(a.fX - b.fX, a.fY - b.fY)
929##
930.
931
932Can also be used to subtract Vector from Point, returning Point.
933Can also be used to subtract Vector from Vector, returning Vector.
934
935#Param a Point to subtract from ##
936#Param b Point to subtract ##
937
938#Return Vector from b to a ##
939
940#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500941 SkPaint paint;
942 paint.setAntiAlias(true);
943 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
944 { 6, 4 }, { 7, 5 }, { 5, 7 },
945 { 4, 6 }, { 3, 7 }, { 1, 5 },
946 { 2, 4 }, { 1, 3 }, { 3, 1 } };
947 canvas->scale(30, 15);
948 paint.setStyle(SkPaint::kStroke_Style);
949 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
950 points[1] += points[0] - points[2];
951 points[2] -= points[3] - points[5];
952 paint.setColor(SK_ColorRED);
953 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500954##
955
956#SeeAlso operator-=(const SkVector& v) offset()
957
958#Method ##
959
960# ------------------------------------------------------------------------------
961
962#Method SkPoint operator+(const SkPoint& a, const SkVector& b)
963
Cary Clark08895c42018-02-01 09:37:32 -0500964#Line # returns Point offset by Vector ##
Cary Clarka560c472017-11-27 10:44:06 -0500965Returns Point resulting from Point a offset by Vector b, computed as:
966#Formula
967(a.fX + b.fX, a.fY + b.fY)
968##
969.
970
971Can also be used to offset Point b by Vector a, returning Point.
972Can also be used to add Vector to Vector, returning Vector.
973
974#Param a Point or Vector to add to ##
975#Param b Point or Vector to add ##
976
977#Return Point equal to a offset by b ##
978
979#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500980 SkPaint paint;
981 paint.setAntiAlias(true);
982 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
983 { 6, 4 }, { 7, 5 }, { 5, 7 },
984 { 4, 6 }, { 3, 7 }, { 1, 5 },
985 { 2, 4 }, { 1, 3 }, { 3, 1 } };
986 canvas->scale(30, 15);
987 paint.setStyle(SkPaint::kStroke_Style);
988 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
989 SkVector mod = {1, 1};
990 for (auto& point : points) {
991 point = point + mod;
992 mod.fX *= 1.1f;
993 mod.fY += .2f;
994 }
995 paint.setColor(SK_ColorRED);
996 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500997##
998
999#SeeAlso operator+=(const SkVector& v) offset()
1000
1001#Method ##
1002
1003# ------------------------------------------------------------------------------
1004
1005#Method static SkScalar Length(SkScalar x, SkScalar y)
Cary Clark4855f782018-02-06 09:41:53 -05001006#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001007#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -05001008Returns the Euclidean_Distance from origin, computed as:
1009#Code
1010#Literal
1011sqrt(x * x + y * y)
1012##
1013.
1014
1015#Param x component of length ##
1016#Param y component of length ##
1017
1018#Return straight-line distance to origin ##
1019
1020#Example
1021#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -05001022 SkPaint paint;
1023 paint.setAntiAlias(true);
1024 const SkPoint points[] = { { 90, 30 }, { 120, 150 }, { 150, 30 }, { 210, 90 } };
1025 const SkPoint origin = {30, 140};
1026 for (auto point : points) {
1027 canvas->drawLine(origin, point, paint);
1028 SkAutoCanvasRestore acr(canvas, true);
1029 SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX);
1030 canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY);
1031 SkString length("length = ");
1032 length.appendScalar(SkPoint::Length(point.fX, point.fY));
1033 canvas->drawString(length, origin.fX + 25, origin.fY - 4, paint);
1034 }
Cary Clarka560c472017-11-27 10:44:06 -05001035##
1036
1037#SeeAlso length() Distance setLength
1038
1039#Method ##
1040
1041# ------------------------------------------------------------------------------
1042
1043#Method static SkScalar Normalize(SkVector* vec)
Cary Clark4855f782018-02-06 09:41:53 -05001044#In Offset
Cary Clark08895c42018-02-01 09:37:32 -05001045#Line # sets length to one, and returns prior length ##
Cary Clarka560c472017-11-27 10:44:06 -05001046Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX to vec->fY,
1047if possible. If original length is nearly zero, sets vec to (0, 0) and returns zero;
1048otherwise, returns length of vec before vec is scaled.
1049
1050Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar.
1051
1052Note that normalize() is faster if prior length is not required.
1053
1054#Param vec normalized to unit length ##
1055
1056#Return original vec length ##
1057
1058#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001059 SkPaint paint;
1060 paint.setAntiAlias(true);
1061 const SkPoint lines[][2] = { {{ 30, 110 }, { 190, 30 }},
1062 {{ 30, 220 }, { 120, 140 }}};
1063 for (auto line : lines) {
1064 canvas->drawLine(line[0], line[1], paint);
1065 SkVector vector = line[1] - line[0];
1066 SkScalar priorLength = SkPoint::Normalize(&vector);
1067 SkVector rotate90 = { -vector.fY, vector.fX };
1068 rotate90 *= 10.f;
1069 canvas->drawLine(line[0] - rotate90, line[0] + rotate90, paint);
1070 canvas->drawLine(line[1] - rotate90, line[1] + rotate90, paint);
1071 SkString length("length = ");
1072 length.appendScalar(priorLength);
1073 canvas->drawString(length, line[0].fX + 25, line[0].fY - 4, paint);
1074 }
Cary Clarka560c472017-11-27 10:44:06 -05001075##
1076
1077#SeeAlso normalize() setLength Length
1078
1079#Method ##
1080
1081# ------------------------------------------------------------------------------
1082
1083#Method static SkScalar Distance(const SkPoint& a, const SkPoint& b)
Cary Clark4855f782018-02-06 09:41:53 -05001084#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001085#Line # returns straight-line distance between points ##
Cary Clarka560c472017-11-27 10:44:06 -05001086Returns the Euclidean_Distance between a and b.
1087
1088#Param a line end point ##
1089#Param b line end point ##
1090
1091#Return straight-line distance from a to b ##
1092
1093#Example
1094#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -05001095 SkPaint paint;
1096 paint.setAntiAlias(true);
1097 const SkPoint lines[][2] = {{{-10, -10}, {90, 30}}, {{0, 0}, {150, 30}}, {{10, 25}, {120, 150}}};
1098 const SkPoint origin = {30, 160};
1099 for (auto line : lines) {
1100 SkPoint a = origin + line[0];
1101 const SkPoint& b = line[1];
1102 canvas->drawLine(a, b, paint);
1103 SkAutoCanvasRestore acr(canvas, true);
1104 SkScalar angle = SkScalarATan2((b.fY - a.fY), b.fX - a.fX);
1105 canvas->rotate(angle * 180 / SK_ScalarPI, a.fX, a.fY);
1106 SkString distance("distance = ");
1107 distance.appendScalar(SkPoint::Distance(a, b));
1108 canvas->drawString(distance, a.fX + 25, a.fY - 4, paint);
1109 }
Cary Clarka560c472017-11-27 10:44:06 -05001110##
1111
1112#SeeAlso length() setLength
1113
1114#Method ##
1115
1116# ------------------------------------------------------------------------------
1117
1118#Method static SkScalar DotProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001119#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001120#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001121Returns the dot product of Vector a and Vector b.
1122
1123#Param a left side of dot product ##
1124#Param b right side of dot product ##
1125
1126#Return product of input magnitudes and cosine of the angle between them ##
1127
1128#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001129 SkPaint paint;
1130 paint.setAntiAlias(true);
1131 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1132 {{-20, -24}, {-24, -20}}};
1133 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1134 paint.setStrokeWidth(2);
1135 for (size_t i = 0; i < 4; ++i) {
1136 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1137 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1138 SkString str;
1139 str.printf("dot = %g", SkPoint::DotProduct(vectors[i][0], vectors[i][1]));
1140 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1141 }
Cary Clarka560c472017-11-27 10:44:06 -05001142##
1143
1144#SeeAlso dot CrossProduct
1145
1146#Method ##
1147
1148# ------------------------------------------------------------------------------
1149
1150#Method static SkScalar CrossProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001151#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001152#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001153Returns the cross product of Vector a and Vector b.
1154
Cary Clark4855f782018-02-06 09:41:53 -05001155a and b form three-dimensional vectors with z-axis value equal to zero. The
1156cross product is a three-dimensional vector with x-axis and y-axis values equal
1157to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001158
1159#Param a left side of cross product ##
1160#Param b right side of cross product ##
1161
1162#Return area spanned by Vectors signed by angle direction ##
1163
1164#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001165 SkPaint paint;
1166 paint.setAntiAlias(true);
1167 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1168 {{-20, -24}, {-24, -20}}};
1169 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1170 paint.setStrokeWidth(2);
1171 for (size_t i = 0; i < 4; ++i) {
1172 paint.setColor(SK_ColorRED);
1173 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1174 paint.setColor(SK_ColorBLUE);
1175 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1176 SkString str;
1177 SkScalar cross = SkPoint::CrossProduct(vectors[i][1], vectors[i][0]);
1178 str.printf("cross = %g", cross);
1179 paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE);
1180 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1181 }
Cary Clarka560c472017-11-27 10:44:06 -05001182##
1183
1184#SeeAlso cross DotProduct
1185
1186#Method ##
1187
1188# ------------------------------------------------------------------------------
1189
1190#Method SkScalar cross(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001191#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001192#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001193Returns the cross product of Vector and vec.
1194
Cary Clark4855f782018-02-06 09:41:53 -05001195Vector and vec form three-dimensional vectors with z-axis value equal to zero.
1196The cross product is a three-dimensional vector with x-axis and y-axis values
1197equal to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001198
1199#Param vec right side of cross product ##
1200
1201#Return area spanned by Vectors signed by angle direction ##
1202
1203#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001204 SkPaint paint;
1205 paint.setAntiAlias(true);
1206 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1207 {{-20, -24}, {-24, -20}}};
1208 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1209 paint.setStrokeWidth(2);
1210 for (size_t i = 0; i < 4; ++i) {
1211 paint.setColor(SK_ColorRED);
1212 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1213 paint.setColor(SK_ColorBLUE);
1214 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1215 SkString str;
1216 SkScalar cross = vectors[i][0].cross(vectors[i][1]);
1217 str.printf("cross = %g", cross);
1218 paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE);
1219 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1220 }
Cary Clarka560c472017-11-27 10:44:06 -05001221##
1222
1223#SeeAlso CrossProduct dot
1224
1225#Method ##
1226
1227# ------------------------------------------------------------------------------
1228
1229#Method SkScalar dot(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001230#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001231#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001232Returns the dot product of Vector and Vector vec.
1233
1234#Param vec right side of dot product ##
1235
1236#Return product of input magnitudes and cosine of the angle between them ##
1237
1238#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001239 SkPaint paint;
1240 paint.setAntiAlias(true);
1241 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1242 {{-20, -24}, {-24, -20}}};
1243 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1244 paint.setStrokeWidth(2);
1245 for (size_t i = 0; i < 4; ++i) {
1246 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1247 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1248 SkString str;
1249 str.printf("dot = %g", vectors[i][0].dot(vectors[i][1]));
1250 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1251 }
Cary Clarka560c472017-11-27 10:44:06 -05001252##
1253
1254#SeeAlso DotProduct cross
1255
1256#Method ##
1257
1258#Struct SkPoint ##
1259
Cary Clarka560c472017-11-27 10:44:06 -05001260
1261# ------------------------------------------------------------------------------
1262
Cary Clark682c58d2018-05-16 07:07:07 -04001263#Subtopic Vector
1264#Line # alias for Point ##
Cary Clark137b8742018-05-30 09:21:49 -04001265 #Alias Vector ##
1266 #Alias Vectors ##
Cary Clarka560c472017-11-27 10:44:06 -05001267 #Typedef SkPoint SkVector
Cary Clark682c58d2018-05-16 07:07:07 -04001268 #Line # alias for Point ##
1269 #Code
1270 typedef SkPoint SkVector;
1271 ##
1272 SkVector provides an alternative name for SkPoint. SkVector and SkPoint can
Cary Clark5538c132018-06-14 12:28:14 -04001273 be used interchangeably for all purposes.
Cary Clarka560c472017-11-27 10:44:06 -05001274 #Typedef ##
1275##
Cary Clark682c58d2018-05-16 07:07:07 -04001276
1277#Topic Point ##