blob: 86a7910efc4cdeb5218fe8a553251bc8394cdbc5 [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 Clark2be81cf2018-09-13 12:04:30 -0400675Adds Vector v to Point. Sets Point to: #Formula # (fX + v.fX, fY + v.fY) ##.
Cary Clarka560c472017-11-27 10:44:06 -0500676
677#Param v Vector to add ##
678
679#Example
680#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500681 SkPaint paint;
682 paint.setAntiAlias(true);
683 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
684 { 6, 4 }, { 7, 5 }, { 5, 7 },
685 { 4, 6 }, { 3, 7 }, { 1, 5 },
686 { 2, 4 }, { 1, 3 }, { 3, 1 } };
687 canvas->scale(30, 15);
688 paint.setStyle(SkPaint::kStroke_Style);
689 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
690 points[1] += {1, 1};
691 points[2] += {-1, -1};
692 paint.setColor(SK_ColorRED);
693 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500694##
695
696#SeeAlso offset() operator+(const SkPoint& a, const SkVector& b) SkIPoint::operator+=(const SkIVector& v)
697
698#Method ##
699
700# ------------------------------------------------------------------------------
701
702#Method void operator-=(const SkVector& v)
703
Cary Clark08895c42018-02-01 09:37:32 -0500704#Line # subtracts Vector from Point ##
Cary Clark2be81cf2018-09-13 12:04:30 -0400705Subtracts Vector v from Point. Sets Point to: #Formula # (fX - v.fX, fY - v.fY) ##.
Cary Clarka560c472017-11-27 10:44:06 -0500706
707#Param v Vector to subtract ##
708
709#Example
710#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500711 SkPaint paint;
712 paint.setAntiAlias(true);
713 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
714 { 6, 4 }, { 7, 5 }, { 5, 7 },
715 { 4, 6 }, { 3, 7 }, { 1, 5 },
716 { 2, 4 }, { 1, 3 }, { 3, 1 } };
717 canvas->scale(30, 15);
718 paint.setStyle(SkPaint::kStroke_Style);
719 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
720 points[1] -= {1, 1};
721 points[2] -= {-1, -1};
722 paint.setColor(SK_ColorRED);
723 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500724##
725
726#SeeAlso offset() operator-(const SkPoint& a, const SkPoint& b) SkIPoint::operator-=(const SkIVector& v)
727
728#Method ##
729
730# ------------------------------------------------------------------------------
731
732#Method SkPoint operator*(SkScalar scale)_const
733
Cary Clark08895c42018-02-01 09:37:32 -0500734#Line # returns Point multiplied by scale ##
Cary Clarka560c472017-11-27 10:44:06 -0500735Returns Point multiplied by scale.
736
737#Param scale Scalar to multiply by ##
738
739#Return Point as (fX * scale, fY * scale) ##
740
741#Example
742#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500743 SkPaint paint;
744 paint.setAntiAlias(true);
745 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
746 { 6, 4 }, { 7, 5 }, { 5, 7 },
747 { 4, 6 }, { 3, 7 }, { 1, 5 },
748 { 2, 4 }, { 1, 3 }, { 3, 1 } };
749 canvas->scale(15, 10);
750 paint.setStyle(SkPaint::kStroke_Style);
751 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
752 for (auto& point : points) {
753 point = point * 1.5f;
754 }
755 paint.setColor(SK_ColorRED);
756 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500757##
758
759#SeeAlso operator*=(SkScalar scale) scale() setLength setNormalize
760
761#Method ##
762
763# ------------------------------------------------------------------------------
764
765#Method SkPoint& operator*=(SkScalar scale)
766
Cary Clark08895c42018-02-01 09:37:32 -0500767#Line # multiplies Point by scale factor ##
Cary Clark2be81cf2018-09-13 12:04:30 -0400768Multiplies Point by scale. Sets Point to: #Formula # (fX * scale, fY * scale) ##.
Cary Clarka560c472017-11-27 10:44:06 -0500769
770#Param scale Scalar to multiply by ##
771
772#Return reference to Point ##
773
774#Example
775#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500776 SkPaint paint;
777 paint.setAntiAlias(true);
778 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
779 { 6, 4 }, { 7, 5 }, { 5, 7 },
780 { 4, 6 }, { 3, 7 }, { 1, 5 },
781 { 2, 4 }, { 1, 3 }, { 3, 1 } };
782 canvas->scale(15, 10);
783 paint.setStyle(SkPaint::kStroke_Style);
784 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
785 for (auto& point : points) {
786 point *= 2;
787 }
788 paint.setColor(SK_ColorRED);
789 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500790##
791
792#SeeAlso operator*(SkScalar scale)_const scale() setLength setNormalize
793
794#Method ##
795
796# ------------------------------------------------------------------------------
797
798#Method bool isFinite() const
Cary Clark4855f782018-02-06 09:41:53 -0500799#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500800#Line # returns true if no member is infinite or NaN ##
Cary Clarka560c472017-11-27 10:44:06 -0500801Returns true if both fX and fY are measurable values.
802
803#Return true for values other than infinities and NaN ##
804
805#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500806SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
807for (const SkPoint& pt : test) {
808 SkDebugf("pt: %g, %g finite: %s\n", pt.fX, pt.fY, pt.isFinite() ? "true" : "false");
Cary Clarka560c472017-11-27 10:44:06 -0500809}
810#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500811pt: 0, -0 finite: true
812pt: -1, -2 finite: true
813pt: inf, 1 finite: false
Cary Clarka560c472017-11-27 10:44:06 -0500814pt: nan, -1 finite: false
815##
816##
817
818#SeeAlso SkRect::isFinite SkPath::isFinite
819
820#Method ##
821
822# ------------------------------------------------------------------------------
823
824#Method bool equals(SkScalar x, SkScalar y) const
Cary Clark4855f782018-02-06 09:41:53 -0500825#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500826#Line # returns true if Points are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500827Returns true if Point is equivalent to Point constructed from (x, y).
828
829#Param x value compared with fX ##
830#Param y value compared with fY ##
831
832#Return true if Point equals (x, y) ##
833
834#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500835SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
836for (const SkPoint& pt : test) {
837 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt.equals(pt.fX, pt.fY) ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500838}
839#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500840pt: 0, -0 == pt
841pt: -1, -2 == pt
842pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500843pt: nan, -1 != pt
844##
845##
846
847#SeeAlso operator==(const SkPoint& a, const SkPoint& b)
848
849#Method ##
850
851# ------------------------------------------------------------------------------
852
853#Method bool operator==(const SkPoint& a, const SkPoint& b)
854
Cary Clark08895c42018-02-01 09:37:32 -0500855#Line # returns true if Point are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500856Returns true if a is equivalent to b.
857
858#Param a Point to compare ##
859#Param b Point to compare ##
860
861#Return true if a.fX == b.fX and a.fY == b.fY ##
862
863#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500864SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
865for (const SkPoint& pt : test) {
866 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt == pt ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500867}
868#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500869pt: 0, -0 == pt
870pt: -1, -2 == pt
871pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500872pt: nan, -1 != pt
873##
874##
875
876#SeeAlso equals() operator!=(const SkPoint& a, const SkPoint& b)
877
878#Method ##
879
880# ------------------------------------------------------------------------------
881
882#Method bool operator!=(const SkPoint& a, const SkPoint& b)
883
Cary Clark08895c42018-02-01 09:37:32 -0500884#Line # returns true if Point are unequal ##
Cary Clarka560c472017-11-27 10:44:06 -0500885Returns true if a is not equivalent to b.
886
887#Param a Point to compare ##
888#Param b Point to compare ##
889
890#Return true if a.fX != b.fX or a.fY != b.fY ##
891
892#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500893SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
894for (const SkPoint& pt : test) {
895 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt != pt ? '!' : '=');
Cary Clarka560c472017-11-27 10:44:06 -0500896}
897#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500898pt: 0, -0 == pt
899pt: -1, -2 == pt
900pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500901pt: nan, -1 != pt
902##
903##
904
905#SeeAlso operator==(const SkPoint& a, const SkPoint& b) equals()
906
907#Method ##
908
909# ------------------------------------------------------------------------------
910
911#Method SkVector operator-(const SkPoint& a, const SkPoint& b)
912
Cary Clark08895c42018-02-01 09:37:32 -0500913#Line # returns Vector between Points ##
Cary Clark2be81cf2018-09-13 12:04:30 -0400914Returns Vector from b to a, computed as #Formula # (a.fX - b.fX, a.fY - b.fY) ##.
Cary Clarka560c472017-11-27 10:44:06 -0500915
916Can also be used to subtract Vector from Point, returning Point.
917Can also be used to subtract Vector from Vector, returning Vector.
918
919#Param a Point to subtract from ##
920#Param b Point to subtract ##
921
922#Return Vector from b to a ##
923
924#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500925 SkPaint paint;
926 paint.setAntiAlias(true);
927 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
928 { 6, 4 }, { 7, 5 }, { 5, 7 },
929 { 4, 6 }, { 3, 7 }, { 1, 5 },
930 { 2, 4 }, { 1, 3 }, { 3, 1 } };
931 canvas->scale(30, 15);
932 paint.setStyle(SkPaint::kStroke_Style);
933 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
934 points[1] += points[0] - points[2];
935 points[2] -= points[3] - points[5];
936 paint.setColor(SK_ColorRED);
937 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500938##
939
940#SeeAlso operator-=(const SkVector& v) offset()
941
942#Method ##
943
944# ------------------------------------------------------------------------------
945
946#Method SkPoint operator+(const SkPoint& a, const SkVector& b)
947
Cary Clark08895c42018-02-01 09:37:32 -0500948#Line # returns Point offset by Vector ##
Cary Clarka560c472017-11-27 10:44:06 -0500949Returns Point resulting from Point a offset by Vector b, computed as:
Cary Clark2be81cf2018-09-13 12:04:30 -0400950#Formula # (a.fX + b.fX, a.fY + b.fY) ##.
Cary Clarka560c472017-11-27 10:44:06 -0500951
952Can also be used to offset Point b by Vector a, returning Point.
953Can also be used to add Vector to Vector, returning Vector.
954
955#Param a Point or Vector to add to ##
956#Param b Point or Vector to add ##
957
958#Return Point equal to a offset by b ##
959
960#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500961 SkPaint paint;
962 paint.setAntiAlias(true);
963 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
964 { 6, 4 }, { 7, 5 }, { 5, 7 },
965 { 4, 6 }, { 3, 7 }, { 1, 5 },
966 { 2, 4 }, { 1, 3 }, { 3, 1 } };
967 canvas->scale(30, 15);
968 paint.setStyle(SkPaint::kStroke_Style);
969 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
970 SkVector mod = {1, 1};
971 for (auto& point : points) {
972 point = point + mod;
973 mod.fX *= 1.1f;
974 mod.fY += .2f;
975 }
976 paint.setColor(SK_ColorRED);
977 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500978##
979
980#SeeAlso operator+=(const SkVector& v) offset()
981
982#Method ##
983
984# ------------------------------------------------------------------------------
985
986#Method static SkScalar Length(SkScalar x, SkScalar y)
Cary Clark4855f782018-02-06 09:41:53 -0500987#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500988#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -0500989Returns the Euclidean_Distance from origin, computed as:
990#Code
991#Literal
992sqrt(x * x + y * y)
993##
994.
995
996#Param x component of length ##
997#Param y component of length ##
998
999#Return straight-line distance to origin ##
1000
1001#Example
1002#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -05001003 SkPaint paint;
1004 paint.setAntiAlias(true);
1005 const SkPoint points[] = { { 90, 30 }, { 120, 150 }, { 150, 30 }, { 210, 90 } };
1006 const SkPoint origin = {30, 140};
1007 for (auto point : points) {
1008 canvas->drawLine(origin, point, paint);
1009 SkAutoCanvasRestore acr(canvas, true);
1010 SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX);
1011 canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY);
1012 SkString length("length = ");
1013 length.appendScalar(SkPoint::Length(point.fX, point.fY));
1014 canvas->drawString(length, origin.fX + 25, origin.fY - 4, paint);
1015 }
Cary Clarka560c472017-11-27 10:44:06 -05001016##
1017
1018#SeeAlso length() Distance setLength
1019
1020#Method ##
1021
1022# ------------------------------------------------------------------------------
1023
1024#Method static SkScalar Normalize(SkVector* vec)
Cary Clark4855f782018-02-06 09:41:53 -05001025#In Offset
Cary Clark08895c42018-02-01 09:37:32 -05001026#Line # sets length to one, and returns prior length ##
Cary Clarka560c472017-11-27 10:44:06 -05001027Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX to vec->fY,
1028if possible. If original length is nearly zero, sets vec to (0, 0) and returns zero;
1029otherwise, returns length of vec before vec is scaled.
1030
1031Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar.
1032
1033Note that normalize() is faster if prior length is not required.
1034
1035#Param vec normalized to unit length ##
1036
1037#Return original vec length ##
1038
1039#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001040 SkPaint paint;
1041 paint.setAntiAlias(true);
1042 const SkPoint lines[][2] = { {{ 30, 110 }, { 190, 30 }},
1043 {{ 30, 220 }, { 120, 140 }}};
1044 for (auto line : lines) {
1045 canvas->drawLine(line[0], line[1], paint);
1046 SkVector vector = line[1] - line[0];
1047 SkScalar priorLength = SkPoint::Normalize(&vector);
1048 SkVector rotate90 = { -vector.fY, vector.fX };
1049 rotate90 *= 10.f;
1050 canvas->drawLine(line[0] - rotate90, line[0] + rotate90, paint);
1051 canvas->drawLine(line[1] - rotate90, line[1] + rotate90, paint);
1052 SkString length("length = ");
1053 length.appendScalar(priorLength);
1054 canvas->drawString(length, line[0].fX + 25, line[0].fY - 4, paint);
1055 }
Cary Clarka560c472017-11-27 10:44:06 -05001056##
1057
1058#SeeAlso normalize() setLength Length
1059
1060#Method ##
1061
1062# ------------------------------------------------------------------------------
1063
1064#Method static SkScalar Distance(const SkPoint& a, const SkPoint& b)
Cary Clark4855f782018-02-06 09:41:53 -05001065#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001066#Line # returns straight-line distance between points ##
Cary Clarka560c472017-11-27 10:44:06 -05001067Returns the Euclidean_Distance between a and b.
1068
1069#Param a line end point ##
1070#Param b line end point ##
1071
1072#Return straight-line distance from a to b ##
1073
1074#Example
1075#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -05001076 SkPaint paint;
1077 paint.setAntiAlias(true);
1078 const SkPoint lines[][2] = {{{-10, -10}, {90, 30}}, {{0, 0}, {150, 30}}, {{10, 25}, {120, 150}}};
1079 const SkPoint origin = {30, 160};
1080 for (auto line : lines) {
1081 SkPoint a = origin + line[0];
1082 const SkPoint& b = line[1];
1083 canvas->drawLine(a, b, paint);
1084 SkAutoCanvasRestore acr(canvas, true);
1085 SkScalar angle = SkScalarATan2((b.fY - a.fY), b.fX - a.fX);
1086 canvas->rotate(angle * 180 / SK_ScalarPI, a.fX, a.fY);
1087 SkString distance("distance = ");
1088 distance.appendScalar(SkPoint::Distance(a, b));
1089 canvas->drawString(distance, a.fX + 25, a.fY - 4, paint);
1090 }
Cary Clarka560c472017-11-27 10:44:06 -05001091##
1092
1093#SeeAlso length() setLength
1094
1095#Method ##
1096
1097# ------------------------------------------------------------------------------
1098
1099#Method static SkScalar DotProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001100#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001101#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001102Returns the dot product of Vector a and Vector b.
1103
1104#Param a left side of dot product ##
1105#Param b right side of dot product ##
1106
1107#Return product of input magnitudes and cosine of the angle between them ##
1108
1109#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001110 SkPaint paint;
1111 paint.setAntiAlias(true);
1112 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1113 {{-20, -24}, {-24, -20}}};
1114 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1115 paint.setStrokeWidth(2);
1116 for (size_t i = 0; i < 4; ++i) {
1117 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1118 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1119 SkString str;
1120 str.printf("dot = %g", SkPoint::DotProduct(vectors[i][0], vectors[i][1]));
1121 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1122 }
Cary Clarka560c472017-11-27 10:44:06 -05001123##
1124
1125#SeeAlso dot CrossProduct
1126
1127#Method ##
1128
1129# ------------------------------------------------------------------------------
1130
1131#Method static SkScalar CrossProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001132#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001133#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001134Returns the cross product of Vector a and Vector b.
1135
Cary Clark4855f782018-02-06 09:41:53 -05001136a and b form three-dimensional vectors with z-axis value equal to zero. The
1137cross product is a three-dimensional vector with x-axis and y-axis values equal
1138to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001139
1140#Param a left side of cross product ##
1141#Param b right side of cross product ##
1142
1143#Return area spanned by Vectors signed by angle direction ##
1144
1145#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001146 SkPaint paint;
1147 paint.setAntiAlias(true);
1148 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1149 {{-20, -24}, {-24, -20}}};
1150 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1151 paint.setStrokeWidth(2);
1152 for (size_t i = 0; i < 4; ++i) {
1153 paint.setColor(SK_ColorRED);
1154 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1155 paint.setColor(SK_ColorBLUE);
1156 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1157 SkString str;
1158 SkScalar cross = SkPoint::CrossProduct(vectors[i][1], vectors[i][0]);
1159 str.printf("cross = %g", cross);
1160 paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE);
1161 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1162 }
Cary Clarka560c472017-11-27 10:44:06 -05001163##
1164
1165#SeeAlso cross DotProduct
1166
1167#Method ##
1168
1169# ------------------------------------------------------------------------------
1170
1171#Method SkScalar cross(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001172#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001173#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001174Returns the cross product of Vector and vec.
1175
Cary Clark4855f782018-02-06 09:41:53 -05001176Vector and vec form three-dimensional vectors with z-axis value equal to zero.
1177The cross product is a three-dimensional vector with x-axis and y-axis values
1178equal to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001179
1180#Param vec right side of cross product ##
1181
1182#Return area spanned by Vectors signed by angle direction ##
1183
1184#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001185 SkPaint paint;
1186 paint.setAntiAlias(true);
1187 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1188 {{-20, -24}, {-24, -20}}};
1189 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1190 paint.setStrokeWidth(2);
1191 for (size_t i = 0; i < 4; ++i) {
1192 paint.setColor(SK_ColorRED);
1193 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1194 paint.setColor(SK_ColorBLUE);
1195 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1196 SkString str;
1197 SkScalar cross = vectors[i][0].cross(vectors[i][1]);
1198 str.printf("cross = %g", cross);
1199 paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE);
1200 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1201 }
Cary Clarka560c472017-11-27 10:44:06 -05001202##
1203
1204#SeeAlso CrossProduct dot
1205
1206#Method ##
1207
1208# ------------------------------------------------------------------------------
1209
1210#Method SkScalar dot(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001211#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001212#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001213Returns the dot product of Vector and Vector vec.
1214
1215#Param vec right side of dot product ##
1216
1217#Return product of input magnitudes and cosine of the angle between them ##
1218
1219#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001220 SkPaint paint;
1221 paint.setAntiAlias(true);
1222 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1223 {{-20, -24}, {-24, -20}}};
1224 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1225 paint.setStrokeWidth(2);
1226 for (size_t i = 0; i < 4; ++i) {
1227 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1228 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1229 SkString str;
1230 str.printf("dot = %g", vectors[i][0].dot(vectors[i][1]));
1231 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1232 }
Cary Clarka560c472017-11-27 10:44:06 -05001233##
1234
1235#SeeAlso DotProduct cross
1236
1237#Method ##
1238
1239#Struct SkPoint ##
1240
Cary Clarka560c472017-11-27 10:44:06 -05001241
1242# ------------------------------------------------------------------------------
1243
Cary Clark682c58d2018-05-16 07:07:07 -04001244#Subtopic Vector
1245#Line # alias for Point ##
Cary Clark137b8742018-05-30 09:21:49 -04001246 #Alias Vector ##
1247 #Alias Vectors ##
Cary Clarka560c472017-11-27 10:44:06 -05001248 #Typedef SkPoint SkVector
Cary Clark682c58d2018-05-16 07:07:07 -04001249 #Line # alias for Point ##
1250 #Code
1251 typedef SkPoint SkVector;
1252 ##
1253 SkVector provides an alternative name for SkPoint. SkVector and SkPoint can
Cary Clark5538c132018-06-14 12:28:14 -04001254 be used interchangeably for all purposes.
Cary Clarka560c472017-11-27 10:44:06 -05001255 #Typedef ##
1256##
Cary Clark682c58d2018-05-16 07:07:07 -04001257
1258#Topic Point ##