blob: 87f1b618e6ecfd609cf020d4279bee57e9ac44d3 [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##
780
781#Param scale Scalar to multiply by ##
782
783#Return reference to Point ##
784
785#Example
786#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500787 SkPaint paint;
788 paint.setAntiAlias(true);
789 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
790 { 6, 4 }, { 7, 5 }, { 5, 7 },
791 { 4, 6 }, { 3, 7 }, { 1, 5 },
792 { 2, 4 }, { 1, 3 }, { 3, 1 } };
793 canvas->scale(15, 10);
794 paint.setStyle(SkPaint::kStroke_Style);
795 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
796 for (auto& point : points) {
797 point *= 2;
798 }
799 paint.setColor(SK_ColorRED);
800 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500801##
802
803#SeeAlso operator*(SkScalar scale)_const scale() setLength setNormalize
804
805#Method ##
806
807# ------------------------------------------------------------------------------
808
809#Method bool isFinite() const
Cary Clark4855f782018-02-06 09:41:53 -0500810#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500811#Line # returns true if no member is infinite or NaN ##
Cary Clarka560c472017-11-27 10:44:06 -0500812Returns true if both fX and fY are measurable values.
813
814#Return true for values other than infinities and NaN ##
815
816#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500817SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
818for (const SkPoint& pt : test) {
819 SkDebugf("pt: %g, %g finite: %s\n", pt.fX, pt.fY, pt.isFinite() ? "true" : "false");
Cary Clarka560c472017-11-27 10:44:06 -0500820}
821#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500822pt: 0, -0 finite: true
823pt: -1, -2 finite: true
824pt: inf, 1 finite: false
Cary Clarka560c472017-11-27 10:44:06 -0500825pt: nan, -1 finite: false
826##
827##
828
829#SeeAlso SkRect::isFinite SkPath::isFinite
830
831#Method ##
832
833# ------------------------------------------------------------------------------
834
835#Method bool equals(SkScalar x, SkScalar y) const
Cary Clark4855f782018-02-06 09:41:53 -0500836#In Operator
Cary Clark08895c42018-02-01 09:37:32 -0500837#Line # returns true if Points are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500838Returns true if Point is equivalent to Point constructed from (x, y).
839
840#Param x value compared with fX ##
841#Param y value compared with fY ##
842
843#Return true if Point equals (x, y) ##
844
845#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500846SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
847for (const SkPoint& pt : test) {
848 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt.equals(pt.fX, pt.fY) ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500849}
850#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500851pt: 0, -0 == pt
852pt: -1, -2 == pt
853pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500854pt: nan, -1 != pt
855##
856##
857
858#SeeAlso operator==(const SkPoint& a, const SkPoint& b)
859
860#Method ##
861
862# ------------------------------------------------------------------------------
863
864#Method bool operator==(const SkPoint& a, const SkPoint& b)
865
Cary Clark08895c42018-02-01 09:37:32 -0500866#Line # returns true if Point are equal ##
Cary Clarka560c472017-11-27 10:44:06 -0500867Returns true if a is equivalent to b.
868
869#Param a Point to compare ##
870#Param b Point to compare ##
871
872#Return true if a.fX == b.fX and a.fY == b.fY ##
873
874#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500875SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
876for (const SkPoint& pt : test) {
877 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt == pt ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500878}
879#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500880pt: 0, -0 == pt
881pt: -1, -2 == pt
882pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500883pt: nan, -1 != pt
884##
885##
886
887#SeeAlso equals() operator!=(const SkPoint& a, const SkPoint& b)
888
889#Method ##
890
891# ------------------------------------------------------------------------------
892
893#Method bool operator!=(const SkPoint& a, const SkPoint& b)
894
Cary Clark08895c42018-02-01 09:37:32 -0500895#Line # returns true if Point are unequal ##
Cary Clarka560c472017-11-27 10:44:06 -0500896Returns true if a is not equivalent to b.
897
898#Param a Point to compare ##
899#Param b Point to compare ##
900
901#Return true if a.fX != b.fX or a.fY != b.fY ##
902
903#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500904SkPoint test[] = { {0, -0.f}, {-1, -2}, {SK_ScalarInfinity, 1}, {SK_ScalarNaN, -1} };
905for (const SkPoint& pt : test) {
906 SkDebugf("pt: %g, %g %c= pt\n", pt.fX, pt.fY, pt != pt ? '!' : '=');
Cary Clarka560c472017-11-27 10:44:06 -0500907}
908#StdOut
Cary Clark0c5f5462017-12-15 11:21:51 -0500909pt: 0, -0 == pt
910pt: -1, -2 == pt
911pt: inf, 1 == pt
Cary Clarka560c472017-11-27 10:44:06 -0500912pt: nan, -1 != pt
913##
914##
915
916#SeeAlso operator==(const SkPoint& a, const SkPoint& b) equals()
917
918#Method ##
919
920# ------------------------------------------------------------------------------
921
922#Method SkVector operator-(const SkPoint& a, const SkPoint& b)
923
Cary Clark08895c42018-02-01 09:37:32 -0500924#Line # returns Vector between Points ##
Cary Clarka560c472017-11-27 10:44:06 -0500925Returns Vector from b to a, computed as
926#Formula
927(a.fX - b.fX, a.fY - b.fY)
928##
929.
930
931Can also be used to subtract Vector from Point, returning Point.
932Can also be used to subtract Vector from Vector, returning Vector.
933
934#Param a Point to subtract from ##
935#Param b Point to subtract ##
936
937#Return Vector from b to a ##
938
939#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500940 SkPaint paint;
941 paint.setAntiAlias(true);
942 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
943 { 6, 4 }, { 7, 5 }, { 5, 7 },
944 { 4, 6 }, { 3, 7 }, { 1, 5 },
945 { 2, 4 }, { 1, 3 }, { 3, 1 } };
946 canvas->scale(30, 15);
947 paint.setStyle(SkPaint::kStroke_Style);
948 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
949 points[1] += points[0] - points[2];
950 points[2] -= points[3] - points[5];
951 paint.setColor(SK_ColorRED);
952 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500953##
954
955#SeeAlso operator-=(const SkVector& v) offset()
956
957#Method ##
958
959# ------------------------------------------------------------------------------
960
961#Method SkPoint operator+(const SkPoint& a, const SkVector& b)
962
Cary Clark08895c42018-02-01 09:37:32 -0500963#Line # returns Point offset by Vector ##
Cary Clarka560c472017-11-27 10:44:06 -0500964Returns Point resulting from Point a offset by Vector b, computed as:
965#Formula
966(a.fX + b.fX, a.fY + b.fY)
967##
968.
969
970Can also be used to offset Point b by Vector a, returning Point.
971Can also be used to add Vector to Vector, returning Vector.
972
973#Param a Point or Vector to add to ##
974#Param b Point or Vector to add ##
975
976#Return Point equal to a offset by b ##
977
978#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500979 SkPaint paint;
980 paint.setAntiAlias(true);
981 SkPoint points[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 },
982 { 6, 4 }, { 7, 5 }, { 5, 7 },
983 { 4, 6 }, { 3, 7 }, { 1, 5 },
984 { 2, 4 }, { 1, 3 }, { 3, 1 } };
985 canvas->scale(30, 15);
986 paint.setStyle(SkPaint::kStroke_Style);
987 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
988 SkVector mod = {1, 1};
989 for (auto& point : points) {
990 point = point + mod;
991 mod.fX *= 1.1f;
992 mod.fY += .2f;
993 }
994 paint.setColor(SK_ColorRED);
995 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(points), points, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500996##
997
998#SeeAlso operator+=(const SkVector& v) offset()
999
1000#Method ##
1001
1002# ------------------------------------------------------------------------------
1003
1004#Method static SkScalar Length(SkScalar x, SkScalar y)
Cary Clark4855f782018-02-06 09:41:53 -05001005#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001006#Line # returns straight-line distance to origin ##
Cary Clarka560c472017-11-27 10:44:06 -05001007Returns the Euclidean_Distance from origin, computed as:
1008#Code
1009#Literal
1010sqrt(x * x + y * y)
1011##
1012.
1013
1014#Param x component of length ##
1015#Param y component of length ##
1016
1017#Return straight-line distance to origin ##
1018
1019#Example
1020#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -05001021 SkPaint paint;
1022 paint.setAntiAlias(true);
1023 const SkPoint points[] = { { 90, 30 }, { 120, 150 }, { 150, 30 }, { 210, 90 } };
1024 const SkPoint origin = {30, 140};
1025 for (auto point : points) {
1026 canvas->drawLine(origin, point, paint);
1027 SkAutoCanvasRestore acr(canvas, true);
1028 SkScalar angle = SkScalarATan2((point.fY - origin.fY), point.fX - origin.fX);
1029 canvas->rotate(angle * 180 / SK_ScalarPI, origin.fX, origin.fY);
1030 SkString length("length = ");
1031 length.appendScalar(SkPoint::Length(point.fX, point.fY));
1032 canvas->drawString(length, origin.fX + 25, origin.fY - 4, paint);
1033 }
Cary Clarka560c472017-11-27 10:44:06 -05001034##
1035
1036#SeeAlso length() Distance setLength
1037
1038#Method ##
1039
1040# ------------------------------------------------------------------------------
1041
1042#Method static SkScalar Normalize(SkVector* vec)
Cary Clark4855f782018-02-06 09:41:53 -05001043#In Offset
Cary Clark08895c42018-02-01 09:37:32 -05001044#Line # sets length to one, and returns prior length ##
Cary Clarka560c472017-11-27 10:44:06 -05001045Scales (vec->fX, vec->fY) so that length() returns one, while preserving ratio of vec->fX to vec->fY,
1046if possible. If original length is nearly zero, sets vec to (0, 0) and returns zero;
1047otherwise, returns length of vec before vec is scaled.
1048
1049Returned prior length may be SK_ScalarInfinity if it can not be represented by SkScalar.
1050
1051Note that normalize() is faster if prior length is not required.
1052
1053#Param vec normalized to unit length ##
1054
1055#Return original vec length ##
1056
1057#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001058 SkPaint paint;
1059 paint.setAntiAlias(true);
1060 const SkPoint lines[][2] = { {{ 30, 110 }, { 190, 30 }},
1061 {{ 30, 220 }, { 120, 140 }}};
1062 for (auto line : lines) {
1063 canvas->drawLine(line[0], line[1], paint);
1064 SkVector vector = line[1] - line[0];
1065 SkScalar priorLength = SkPoint::Normalize(&vector);
1066 SkVector rotate90 = { -vector.fY, vector.fX };
1067 rotate90 *= 10.f;
1068 canvas->drawLine(line[0] - rotate90, line[0] + rotate90, paint);
1069 canvas->drawLine(line[1] - rotate90, line[1] + rotate90, paint);
1070 SkString length("length = ");
1071 length.appendScalar(priorLength);
1072 canvas->drawString(length, line[0].fX + 25, line[0].fY - 4, paint);
1073 }
Cary Clarka560c472017-11-27 10:44:06 -05001074##
1075
1076#SeeAlso normalize() setLength Length
1077
1078#Method ##
1079
1080# ------------------------------------------------------------------------------
1081
1082#Method static SkScalar Distance(const SkPoint& a, const SkPoint& b)
Cary Clark4855f782018-02-06 09:41:53 -05001083#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001084#Line # returns straight-line distance between points ##
Cary Clarka560c472017-11-27 10:44:06 -05001085Returns the Euclidean_Distance between a and b.
1086
1087#Param a line end point ##
1088#Param b line end point ##
1089
1090#Return straight-line distance from a to b ##
1091
1092#Example
1093#Height 192
Cary Clark0c5f5462017-12-15 11:21:51 -05001094 SkPaint paint;
1095 paint.setAntiAlias(true);
1096 const SkPoint lines[][2] = {{{-10, -10}, {90, 30}}, {{0, 0}, {150, 30}}, {{10, 25}, {120, 150}}};
1097 const SkPoint origin = {30, 160};
1098 for (auto line : lines) {
1099 SkPoint a = origin + line[0];
1100 const SkPoint& b = line[1];
1101 canvas->drawLine(a, b, paint);
1102 SkAutoCanvasRestore acr(canvas, true);
1103 SkScalar angle = SkScalarATan2((b.fY - a.fY), b.fX - a.fX);
1104 canvas->rotate(angle * 180 / SK_ScalarPI, a.fX, a.fY);
1105 SkString distance("distance = ");
1106 distance.appendScalar(SkPoint::Distance(a, b));
1107 canvas->drawString(distance, a.fX + 25, a.fY - 4, paint);
1108 }
Cary Clarka560c472017-11-27 10:44:06 -05001109##
1110
1111#SeeAlso length() setLength
1112
1113#Method ##
1114
1115# ------------------------------------------------------------------------------
1116
1117#Method static SkScalar DotProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001118#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001119#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001120Returns the dot product of Vector a and Vector b.
1121
1122#Param a left side of dot product ##
1123#Param b right side of dot product ##
1124
1125#Return product of input magnitudes and cosine of the angle between them ##
1126
1127#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001128 SkPaint paint;
1129 paint.setAntiAlias(true);
1130 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1131 {{-20, -24}, {-24, -20}}};
1132 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1133 paint.setStrokeWidth(2);
1134 for (size_t i = 0; i < 4; ++i) {
1135 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1136 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1137 SkString str;
1138 str.printf("dot = %g", SkPoint::DotProduct(vectors[i][0], vectors[i][1]));
1139 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1140 }
Cary Clarka560c472017-11-27 10:44:06 -05001141##
1142
1143#SeeAlso dot CrossProduct
1144
1145#Method ##
1146
1147# ------------------------------------------------------------------------------
1148
1149#Method static SkScalar CrossProduct(const SkVector& a, const SkVector& b)
Cary Clark4855f782018-02-06 09:41:53 -05001150#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001151#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001152Returns the cross product of Vector a and Vector b.
1153
Cary Clark4855f782018-02-06 09:41:53 -05001154a and b form three-dimensional vectors with z-axis value equal to zero. The
1155cross product is a three-dimensional vector with x-axis and y-axis values equal
1156to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001157
1158#Param a left side of cross product ##
1159#Param b right side of cross product ##
1160
1161#Return area spanned by Vectors signed by angle direction ##
1162
1163#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001164 SkPaint paint;
1165 paint.setAntiAlias(true);
1166 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1167 {{-20, -24}, {-24, -20}}};
1168 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1169 paint.setStrokeWidth(2);
1170 for (size_t i = 0; i < 4; ++i) {
1171 paint.setColor(SK_ColorRED);
1172 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1173 paint.setColor(SK_ColorBLUE);
1174 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1175 SkString str;
1176 SkScalar cross = SkPoint::CrossProduct(vectors[i][1], vectors[i][0]);
1177 str.printf("cross = %g", cross);
1178 paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE);
1179 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1180 }
Cary Clarka560c472017-11-27 10:44:06 -05001181##
1182
1183#SeeAlso cross DotProduct
1184
1185#Method ##
1186
1187# ------------------------------------------------------------------------------
1188
1189#Method SkScalar cross(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001190#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001191#Line # returns cross product ##
Cary Clarka560c472017-11-27 10:44:06 -05001192Returns the cross product of Vector and vec.
1193
Cary Clark4855f782018-02-06 09:41:53 -05001194Vector and vec form three-dimensional vectors with z-axis value equal to zero.
1195The cross product is a three-dimensional vector with x-axis and y-axis values
1196equal to zero. The cross product z-axis component is returned.
Cary Clarka560c472017-11-27 10:44:06 -05001197
1198#Param vec right side of cross product ##
1199
1200#Return area spanned by Vectors signed by angle direction ##
1201
1202#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001203 SkPaint paint;
1204 paint.setAntiAlias(true);
1205 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1206 {{-20, -24}, {-24, -20}}};
1207 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1208 paint.setStrokeWidth(2);
1209 for (size_t i = 0; i < 4; ++i) {
1210 paint.setColor(SK_ColorRED);
1211 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1212 paint.setColor(SK_ColorBLUE);
1213 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1214 SkString str;
1215 SkScalar cross = vectors[i][0].cross(vectors[i][1]);
1216 str.printf("cross = %g", cross);
1217 paint.setColor(cross >= 0 ? SK_ColorRED : SK_ColorBLUE);
1218 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1219 }
Cary Clarka560c472017-11-27 10:44:06 -05001220##
1221
1222#SeeAlso CrossProduct dot
1223
1224#Method ##
1225
1226# ------------------------------------------------------------------------------
1227
1228#Method SkScalar dot(const SkVector& vec) const
Cary Clark4855f782018-02-06 09:41:53 -05001229#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001230#Line # returns dot product ##
Cary Clarka560c472017-11-27 10:44:06 -05001231Returns the dot product of Vector and Vector vec.
1232
1233#Param vec right side of dot product ##
1234
1235#Return product of input magnitudes and cosine of the angle between them ##
1236
1237#Example
Cary Clark0c5f5462017-12-15 11:21:51 -05001238 SkPaint paint;
1239 paint.setAntiAlias(true);
1240 SkVector vectors[][2] = {{{50, 2}, {-14, 20}}, {{0, 50}, {-50, 0}}, {{-20, 25}, {25, -20}},
1241 {{-20, -24}, {-24, -20}}};
1242 SkPoint center[] = {{32, 32}, {160, 32}, {32, 160}, {160, 160}};
1243 paint.setStrokeWidth(2);
1244 for (size_t i = 0; i < 4; ++i) {
1245 canvas->drawLine(center[i], center[i] + vectors[i][0], paint);
1246 canvas->drawLine(center[i], center[i] + vectors[i][1], paint);
1247 SkString str;
1248 str.printf("dot = %g", vectors[i][0].dot(vectors[i][1]));
1249 canvas->drawString(str, center[i].fX, center[i].fY, paint);
1250 }
Cary Clarka560c472017-11-27 10:44:06 -05001251##
1252
1253#SeeAlso DotProduct cross
1254
1255#Method ##
1256
1257#Struct SkPoint ##
1258
Cary Clarka560c472017-11-27 10:44:06 -05001259
1260# ------------------------------------------------------------------------------
1261
Cary Clark682c58d2018-05-16 07:07:07 -04001262#Subtopic Vector
1263#Line # alias for Point ##
Cary Clark137b8742018-05-30 09:21:49 -04001264 #Alias Vector ##
1265 #Alias Vectors ##
Cary Clarka560c472017-11-27 10:44:06 -05001266 #Typedef SkPoint SkVector
Cary Clark682c58d2018-05-16 07:07:07 -04001267 #Line # alias for Point ##
1268 #Code
1269 typedef SkPoint SkVector;
1270 ##
1271 SkVector provides an alternative name for SkPoint. SkVector and SkPoint can
Cary Clark5538c132018-06-14 12:28:14 -04001272 be used interchangeably for all purposes.
Cary Clarka560c472017-11-27 10:44:06 -05001273 #Typedef ##
1274##
Cary Clark682c58d2018-05-16 07:07:07 -04001275
1276#Topic Point ##