blob: 52c301a18e3c5b856e7ab473defd93b1a3c0cc29 [file] [log] [blame]
Cary Clarkbc5697d2017-10-04 14:31:33 -04001#Topic Matrix
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Matrices ##
3#Alias Matrix_Reference ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004
5#Class SkMatrix
6
Cary Clark154beea2017-10-26 07:58:48 -04007Matrix holds a 3x3 matrix for transforming coordinates. This allows mapping
8Points and Vectors with translation, scaling, skewing, rotation, and
9perspective.
Cary Clark884dd7d2017-10-11 10:37:52 -040010
Cary Clark154beea2017-10-26 07:58:48 -040011Matrix elements are in row major order. Matrix does not have a constructor,
12so it must be explicitly initialized. setIdentity initializes Matrix
13so it has no effect. setTranslate, setScale, setSkew, setRotate, set9 and setAll
14initializes all Matrix elements with the corresponding mapping.
Cary Clark884dd7d2017-10-11 10:37:52 -040015
Cary Clark682c58d2018-05-16 07:07:07 -040016Matrix includes a hidden variable that classifies the type of matrix to
Cary Clark154beea2017-10-26 07:58:48 -040017improve performance. Matrix is not thread safe unless getType is called first.
Cary Clarkbc5697d2017-10-04 14:31:33 -040018
Cary Clark682c58d2018-05-16 07:07:07 -040019#Subtopic Overview
20#Populate
21##
22
Cary Clark4855f782018-02-06 09:41:53 -050023#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050024#Populate
25##
Cary Clarkbc5697d2017-10-04 14:31:33 -040026
Cary Clark78de7512018-02-07 07:27:09 -050027#Subtopic Related_Function
28#Populate
29##
30
Cary Clarkbc5697d2017-10-04 14:31:33 -040031# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -050032#Subtopic Constructor
33#Populate
34##
Cary Clarkbc5697d2017-10-04 14:31:33 -040035
36#Method static SkMatrix SK_WARN_UNUSED_RESULT MakeScale(SkScalar sx, SkScalar sy)
Cary Clark4855f782018-02-06 09:41:53 -050037#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -050038#Line # constructs from scale in x and y ##
Cary Clark154beea2017-10-26 07:58:48 -040039Sets Matrix to scale by (sx, sy). Returned matrix is:
Cary Clarkbc5697d2017-10-04 14:31:33 -040040
Cary Clark154beea2017-10-26 07:58:48 -040041#Code
42#Literal
43| sx 0 0 |
44| 0 sy 0 |
45| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -040046##
47
Cary Clark154beea2017-10-26 07:58:48 -040048#Param sx horizontal scale factor ##
49#Param sy vertical scale factor ##
50
51#Return Matrix with scale ##
52
53#Example
54#Image 4
55canvas->concat(SkMatrix::MakeScale(4, 3));
56canvas->drawBitmap(source, 0, 0);
57##
58
59#SeeAlso setScale postScale preScale
Cary Clarkbc5697d2017-10-04 14:31:33 -040060
61##
62
63# ------------------------------------------------------------------------------
64
65#Method static SkMatrix SK_WARN_UNUSED_RESULT MakeScale(SkScalar scale)
66
Cary Clark154beea2017-10-26 07:58:48 -040067Sets Matrix to scale by (scale, scale). Returned matrix is:
Cary Clarkbc5697d2017-10-04 14:31:33 -040068
Cary Clark154beea2017-10-26 07:58:48 -040069#Code
70#Literal
71| scale 0 0 |
72| 0 scale 0 |
73| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -040074##
75
Cary Clark154beea2017-10-26 07:58:48 -040076#Param scale horizontal and vertical scale factor ##
77
78#Return Matrix with scale ##
79
80#Example
81#Image 4
82canvas->concat(SkMatrix::MakeScale(4));
83canvas->drawBitmap(source, 0, 0);
84##
85
86#SeeAlso setScale postScale preScale
Cary Clarkbc5697d2017-10-04 14:31:33 -040087
88##
89
90# ------------------------------------------------------------------------------
91
92#Method static SkMatrix SK_WARN_UNUSED_RESULT MakeTrans(SkScalar dx, SkScalar dy)
Cary Clark4855f782018-02-06 09:41:53 -050093#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -050094#Line # constructs from translate in x and y ##
Cary Clark154beea2017-10-26 07:58:48 -040095Sets Matrix to translate by (dx, dy). Returned matrix is:
Cary Clarkbc5697d2017-10-04 14:31:33 -040096
Cary Clark154beea2017-10-26 07:58:48 -040097#Code
98#Literal
Cary Clarkcfee6ec2017-10-26 10:34:05 -040099| 1 0 dx |
100| 0 1 dy |
101| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -0400102##
103
Cary Clark154beea2017-10-26 07:58:48 -0400104#Param dx horizontal translation ##
105#Param dy vertical translation ##
106
107#Return Matrix with translation ##
108
109#Example
110#Image 4
111SkMatrix matrix = SkMatrix::MakeTrans(64, 48);
112for (int i = 0; i < 4; ++i) {
113 canvas->drawBitmap(source, 0, 0);
114 canvas->concat(matrix);
115}
116##
117
118#SeeAlso setTranslate postTranslate preTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -0400119
120##
121
122# ------------------------------------------------------------------------------
123
Cary Clarkbef063a2017-10-31 15:44:45 -0400124#Method static SkMatrix SK_WARN_UNUSED_RESULT MakeAll(SkScalar scaleX, SkScalar skewX, SkScalar transX,
125 SkScalar skewY, SkScalar scaleY, SkScalar transY,
126 SkScalar pers0, SkScalar pers1, SkScalar pers2)
Cary Clark4855f782018-02-06 09:41:53 -0500127#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -0500128#Line # constructs all nine values ##
Cary Clarkbef063a2017-10-31 15:44:45 -0400129
130
131Sets Matrix to:
132
133#Code
134#Literal
135| scaleX skewX transX |
136| skewY scaleY transY |
137| pers0 pers1 pers2 |
138##
139
140#Param scaleX horizontal scale factor ##
141#Param skewX horizontal skew factor ##
142#Param transX horizontal translation ##
143#Param skewY vertical skew factor ##
144#Param scaleY vertical scale factor ##
145#Param transY vertical translation ##
Cary Clark5538c132018-06-14 12:28:14 -0400146#Param pers0 input x-axis perspective factor ##
147#Param pers1 input y-axis perspective factor ##
Cary Clarkbef063a2017-10-31 15:44:45 -0400148#Param pers2 perspective scale factor ##
149
150#Return Matrix constructed from parameters ##
151
152#Example
153 SkPaint p;
154 p.setAntiAlias(true);
155 p.setTextSize(64);
156 for (SkScalar sx : { -1, 1 } ) {
157 for (SkScalar sy : { -1, 1 } ) {
158 SkAutoCanvasRestore autoRestore(canvas, true);
159 SkMatrix m = SkMatrix::MakeAll(sx, 1, 128, 0, sy, 128, 0, 0, 1);
160 canvas->concat(m);
161 canvas->drawString("K", 0, 0, p);
162 }
163 }
164##
165
166#SeeAlso setAll set9 postConcat preConcat
167
168##
169
170
171# ------------------------------------------------------------------------------
172
Cary Clarkbc5697d2017-10-04 14:31:33 -0400173#Enum TypeMask
Cary Clark682c58d2018-05-16 07:07:07 -0400174#Line # bit field for Matrix complexity ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400175#Code
176 enum TypeMask {
177 kIdentity_Mask = 0,
178 kTranslate_Mask = 0x01,
179 kScale_Mask = 0x02,
180 kAffine_Mask = 0x04,
181 kPerspective_Mask = 0x08,
182 };
183##
184
Cary Clark154beea2017-10-26 07:58:48 -0400185Enum of bit fields for mask returned by getType.
186Used to identify the complexity of Matrix, to optimize performance.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400187
Cary Clark154beea2017-10-26 07:58:48 -0400188#Const kIdentity_Mask 0
Cary Clark682c58d2018-05-16 07:07:07 -0400189#Line # identity Matrix; all bits clear ##
Cary Clark154beea2017-10-26 07:58:48 -0400190all bits clear if Matrix is identity
Cary Clarkbc5697d2017-10-04 14:31:33 -0400191##
Cary Clark154beea2017-10-26 07:58:48 -0400192#Const kTranslate_Mask 1
Cary Clark682c58d2018-05-16 07:07:07 -0400193#Line # translation Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -0400194set if Matrix has translation
Cary Clarkbc5697d2017-10-04 14:31:33 -0400195##
Cary Clark154beea2017-10-26 07:58:48 -0400196#Const kScale_Mask 2
Cary Clark682c58d2018-05-16 07:07:07 -0400197#Line # scale Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -0400198set if Matrix has x or y scale
Cary Clarkbc5697d2017-10-04 14:31:33 -0400199##
Cary Clark154beea2017-10-26 07:58:48 -0400200#Const kAffine_Mask 4
Cary Clark682c58d2018-05-16 07:07:07 -0400201#Line # skew or rotate Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -0400202set if Matrix skews or rotates
Cary Clarkbc5697d2017-10-04 14:31:33 -0400203##
Cary Clark154beea2017-10-26 07:58:48 -0400204#Const kPerspective_Mask 8
Cary Clark682c58d2018-05-16 07:07:07 -0400205#Line # perspective Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -0400206set if Matrix has perspective
Cary Clarkbc5697d2017-10-04 14:31:33 -0400207##
208
209#Example
Cary Clark154beea2017-10-26 07:58:48 -0400210 auto debugster = [](const char* prefix, const SkMatrix& matrix) -> void {
211 SkString typeMask;
212 typeMask += SkMatrix::kIdentity_Mask == matrix.getType() ? "kIdentity_Mask " : "";
213 typeMask += SkMatrix::kTranslate_Mask & matrix.getType() ? "kTranslate_Mask " : "";
214 typeMask += SkMatrix::kScale_Mask & matrix.getType() ? "kScale_Mask " : "";
215 typeMask += SkMatrix::kAffine_Mask & matrix.getType() ? "kAffine_Mask " : "";
216 typeMask += SkMatrix::kPerspective_Mask & matrix.getType() ? "kPerspective_Mask" : "";
217 SkDebugf("after %s: %s\n", prefix, typeMask.c_str());
218 };
219SkMatrix matrix;
220matrix.reset();
221debugster("reset", matrix);
222matrix.postTranslate(1, 0);
223debugster("postTranslate", matrix);
224matrix.postScale(2, 1);
225debugster("postScale", matrix);
226matrix.postRotate(45);
227debugster("postScale", matrix);
228SkPoint polys[][4] = {{{0, 0}, {0, 1}, {1, 1}, {1, 0}}, {{0, 0}, {0, 1}, {2, 1}, {1, 0}}};
229matrix.setPolyToPoly(polys[0], polys[1], 4);
230debugster("setPolyToPoly", matrix);
231#StdOut
Cary Clark682c58d2018-05-16 07:07:07 -0400232after reset: kIdentity_Mask
233after postTranslate: kTranslate_Mask
234after postScale: kTranslate_Mask kScale_Mask
235after postScale: kTranslate_Mask kScale_Mask kAffine_Mask
Cary Clark154beea2017-10-26 07:58:48 -0400236after setPolyToPoly: kTranslate_Mask kScale_Mask kAffine_Mask kPerspective_Mask
237##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400238##
239
Cary Clark154beea2017-10-26 07:58:48 -0400240#SeeAlso getType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400241
242##
243
244# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500245#Subtopic Property
246#Populate
247#Line # values and attributes ##
248##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400249
250#Method TypeMask getType() const
Cary Clark4855f782018-02-06 09:41:53 -0500251#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500252#Line # returns transform complexity ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400253Returns a bit field describing the transformations the matrix may
254perform. The bit field is computed conservatively, so it may include
Cary Clark154beea2017-10-26 07:58:48 -0400255false positives. For example, when kPerspective_Mask is set, all
256other bits are set.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400257
Cary Clark154beea2017-10-26 07:58:48 -0400258#Return kIdentity_Mask, or combinations of: kTranslate_Mask, kScale_Mask,
Cary Clark682c58d2018-05-16 07:07:07 -0400259 kAffine_Mask, kPerspective_Mask
Cary Clarkbc5697d2017-10-04 14:31:33 -0400260##
261
Cary Clark154beea2017-10-26 07:58:48 -0400262#Example
263SkMatrix matrix;
264matrix.setAll(1, 0, 0, 0, 1, 0, 0, 0, 1);
265SkDebugf("identity flags hex: %0x decimal: %d\n", matrix.getType(), matrix.getType());
266matrix.setAll(1, 0, 0, 0, 1, 0, 0, 0, .5f);
267SkDebugf("set all flags hex: %0x decimal: %d\n", matrix.getType(), matrix.getType());
268#StdOut
269identity flags hex: 0 decimal: 0
270set all flags hex: f decimal: 15
271##
272##
273
274#SeeAlso TypeMask
Cary Clarkbc5697d2017-10-04 14:31:33 -0400275
276##
277
278# ------------------------------------------------------------------------------
279
280#Method bool isIdentity() const
Cary Clark4855f782018-02-06 09:41:53 -0500281#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500282#Line # returns if matrix equals the identity Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -0400283Returns true if Matrix is identity. Identity matrix is:
Cary Clarkbc5697d2017-10-04 14:31:33 -0400284
Cary Clark154beea2017-10-26 07:58:48 -0400285#Code
286#Literal
287| 1 0 0 |
288| 0 1 0 |
289| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -0400290##
291
Cary Clark154beea2017-10-26 07:58:48 -0400292#Return true if Matrix has no effect ##
293
294#Example
295SkMatrix matrix;
296matrix.setAll(1, 0, 0, 0, 1, 0, 0, 0, 1);
297SkDebugf("is identity: %s\n", matrix.isIdentity() ? "true" : "false");
298matrix.setAll(1, 0, 0, 0, 1, 0, 0, 0, 2);
299SkDebugf("is identity: %s\n", matrix.isIdentity() ? "true" : "false");
300#StdOut
301is identity: true
302is identity: false
303##
304##
305
306#SeeAlso reset() setIdentity getType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400307
308##
309
310# ------------------------------------------------------------------------------
311
312#Method bool isScaleTranslate() const
Cary Clark4855f782018-02-06 09:41:53 -0500313#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500314#Line # returns if transform is limited to scale and translate ##
Cary Clark154beea2017-10-26 07:58:48 -0400315Returns true if Matrix at most scales and translates. Matrix may be identity,
316contain only scale elements, only translate elements, or both. Matrix form is:
Cary Clarkbc5697d2017-10-04 14:31:33 -0400317
Cary Clark154beea2017-10-26 07:58:48 -0400318#Code
319#Literal
320| scale-x 0 translate-x |
321| 0 scale-y translate-y |
322| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -0400323##
324
Cary Clark154beea2017-10-26 07:58:48 -0400325#Return true if Matrix is identity; or scales, translates, or both ##
326
327#Example
328SkMatrix matrix;
329for (SkScalar scaleX : { 1, 2 } ) {
330 for (SkScalar translateX : { 0, 20 } ) {
331 matrix.setAll(scaleX, 0, translateX, 0, 1, 0, 0, 0, 1);
332 SkDebugf("is scale-translate: %s\n", matrix.isScaleTranslate() ? "true" : "false");
333 }
334}
335#StdOut
336is scale-translate: true
337is scale-translate: true
338is scale-translate: true
339is scale-translate: true
340##
341##
342
343#SeeAlso setScale isTranslate setTranslate getType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400344
345##
346
347# ------------------------------------------------------------------------------
348
349#Method bool isTranslate() const
Cary Clark4855f782018-02-06 09:41:53 -0500350#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500351#Line # returns if transform is limited to translate ##
Cary Clark154beea2017-10-26 07:58:48 -0400352Returns true if Matrix is identity, or translates. Matrix form is:
Cary Clarkbc5697d2017-10-04 14:31:33 -0400353
Cary Clark154beea2017-10-26 07:58:48 -0400354#Code
355#Literal
356| 1 0 translate-x |
357| 0 1 translate-y |
358| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -0400359##
360
Cary Clark154beea2017-10-26 07:58:48 -0400361#Return true if Matrix is identity, or translates ##
362
363#Example
364SkMatrix matrix;
365for (SkScalar scaleX : { 1, 2 } ) {
366 for (SkScalar translateX : { 0, 20 } ) {
367 matrix.setAll(scaleX, 0, translateX, 0, 1, 0, 0, 0, 1);
368 SkDebugf("is translate: %s\n", matrix.isTranslate() ? "true" : "false");
369 }
370}
371#StdOut
372is translate: true
373is translate: true
374is translate: false
375is translate: false
376##
377##
378
379#SeeAlso setTranslate getType
Cary Clarkbc5697d2017-10-04 14:31:33 -0400380
381##
382
383# ------------------------------------------------------------------------------
384
385#Method bool rectStaysRect() const
Cary Clark4855f782018-02-06 09:41:53 -0500386#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500387#Line # returns if mapped Rect can be represented by another Rect ##
Cary Clark154beea2017-10-26 07:58:48 -0400388Returns true Matrix maps Rect to another Rect. If true, Matrix is identity,
Cary Clark5538c132018-06-14 12:28:14 -0400389or scales, or rotates a multiple of 90 degrees, or mirrors on axes. In all
Cary Clark154beea2017-10-26 07:58:48 -0400390cases, Matrix may also have translation. Matrix form is either:
Cary Clarkbc5697d2017-10-04 14:31:33 -0400391
Cary Clark154beea2017-10-26 07:58:48 -0400392#Code
393#Literal
394| scale-x 0 translate-x |
395| 0 scale-y translate-y |
396| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -0400397##
398
Cary Clark154beea2017-10-26 07:58:48 -0400399or
400
401#Code
402#Literal
403| 0 rotate-x translate-x |
404| rotate-y 0 translate-y |
405| 0 0 1 |
406##
407
408for non-zero values of scale-x, scale-y, rotate-x, and rotate-y.
409
410Also called preservesAxisAlignment; use the one that provides better inline
411documentation.
412
413#Return true if Matrix maps one Rect into another ##
414
415#Example
416SkMatrix matrix;
417for (SkScalar angle: { 0, 90, 180, 270 } ) {
418 matrix.setRotate(angle);
419 SkDebugf("rectStaysRect: %s\n", matrix.rectStaysRect() ? "true" : "false");
420}
421#StdOut
422rectStaysRect: true
423rectStaysRect: true
424rectStaysRect: true
425rectStaysRect: true
426##
427##
428
429#SeeAlso preservesAxisAlignment preservesRightAngles
Cary Clarkbc5697d2017-10-04 14:31:33 -0400430
431##
432
433# ------------------------------------------------------------------------------
434
435#Method bool preservesAxisAlignment() const
Cary Clark4855f782018-02-06 09:41:53 -0500436#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500437#Line # returns if mapping restricts to 90 degree multiples and mirroring ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400438
Cary Clark154beea2017-10-26 07:58:48 -0400439Returns true Matrix maps Rect to another Rect. If true, Matrix is identity,
Cary Clark5538c132018-06-14 12:28:14 -0400440or scales, or rotates a multiple of 90 degrees, or mirrors on axes. In all
Cary Clark154beea2017-10-26 07:58:48 -0400441cases, Matrix may also have translation. Matrix form is either:
Cary Clarkbc5697d2017-10-04 14:31:33 -0400442
Cary Clark154beea2017-10-26 07:58:48 -0400443#Code
444#Literal
445| scale-x 0 translate-x |
446| 0 scale-y translate-y |
447| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -0400448##
449
Cary Clark154beea2017-10-26 07:58:48 -0400450or
451
452#Code
453#Literal
454| 0 rotate-x translate-x |
455| rotate-y 0 translate-y |
456| 0 0 1 |
457##
458
459for non-zero values of scale-x, scale-y, rotate-x, and rotate-y.
460
461Also called rectStaysRect; use the one that provides better inline
462documentation.
463
464#Return true if Matrix maps one Rect into another ##
465
466#Example
467SkMatrix matrix;
468for (SkScalar angle: { 0, 90, 180, 270 } ) {
469 matrix.setRotate(angle);
470 SkDebugf("preservesAxisAlignment: %s\n", matrix.preservesAxisAlignment() ? "true" : "false");
471}
472#StdOut
473preservesAxisAlignment: true
474preservesAxisAlignment: true
475preservesAxisAlignment: true
476preservesAxisAlignment: true
477##
478##
479
480#SeeAlso rectStaysRect preservesRightAngles
Cary Clarkbc5697d2017-10-04 14:31:33 -0400481
482##
483
484# ------------------------------------------------------------------------------
485
486#Method bool hasPerspective() const
Cary Clark4855f782018-02-06 09:41:53 -0500487#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500488#Line # returns if transform includes perspective ##
Cary Clark154beea2017-10-26 07:58:48 -0400489Returns true if the matrix contains perspective elements. Matrix form is:
Cary Clarkbc5697d2017-10-04 14:31:33 -0400490
Cary Clark154beea2017-10-26 07:58:48 -0400491#Code
492#Literal
493| -- -- -- |
494| -- -- -- |
495| perspective-x perspective-y perspective-scale |
Cary Clarkbc5697d2017-10-04 14:31:33 -0400496##
497
Cary Clark682c58d2018-05-16 07:07:07 -0400498where perspective-x or perspective-y is non-zero, or perspective-scale is
Cary Clark154beea2017-10-26 07:58:48 -0400499not one. All other elements may have any value.
500
501#Return true if Matrix is in most general form ##
502
503#Example
504#Image 4
505SkMatrix matrix;
506SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
507SkRect::Make(source.bounds()).toQuad(bitmapBounds);
508matrix.setPolyToPoly(bitmapBounds, perspect, 4);
509canvas->concat(matrix);
510SkString string;
511string.printf("hasPerspective %s", matrix.hasPerspective() ? "true" : "false");
512canvas->drawBitmap(source, 0, 0);
513SkPaint paint;
514paint.setAntiAlias(true);
515paint.setTextSize(48);
516canvas->drawString(string, 0, source.bounds().height() + 48, paint);
517##
518
Cary Clarkbef063a2017-10-31 15:44:45 -0400519#SeeAlso setAll set9 MakeAll
Cary Clarkbc5697d2017-10-04 14:31:33 -0400520
521##
522
523# ------------------------------------------------------------------------------
524
525#Method bool isSimilarity(SkScalar tol = SK_ScalarNearlyZero) const
Cary Clark4855f782018-02-06 09:41:53 -0500526#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500527#Line # returns if transform is limited to square scale and rotation ##
Cary Clark154beea2017-10-26 07:58:48 -0400528Returns true if Matrix contains only translation, rotation, reflection, and
529uniform scale.
530Returns false if Matrix contains different scales, skewing, perspective, or
531degenerate forms that collapse to a line or point.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400532
Cary Clark682c58d2018-05-16 07:07:07 -0400533Describes that the Matrix makes rendering with and without the matrix are
Cary Clark154beea2017-10-26 07:58:48 -0400534visually alike; a transformed circle remains a circle. Mathematically, this is
Cary Clarka560c472017-11-27 10:44:06 -0500535referred to as similarity of a Euclidean_Space, or a similarity transformation.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400536
Cary Clark154beea2017-10-26 07:58:48 -0400537Preserves right angles, keeping the arms of the angle equal lengths.
538
539#Param tol to be deprecated ##
540
541#Return true if Matrix only rotates, uniformly scales, translates ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400542
543#Example
Cary Clark154beea2017-10-26 07:58:48 -0400544#Description
545String is drawn four times through but only two are visible. Drawing the pair
546with isSimilarity false reveals the pair not visible through the matrix.
547##
548 SkPaint p;
549 p.setAntiAlias(true);
550 SkMatrix m;
551 int below = 175;
552 for (SkScalar sx : { -1, 1 } ) {
553 for (SkScalar sy : { -1, 1 } ) {
554 m.setAll(sx, 1, 128, 1, sy, 32, 0, 0, 1);
555 bool isSimilarity = m.isSimilarity();
556 SkString str;
557 str.printf("sx: %g sy: %g sim: %s", sx, sy, isSimilarity ? "true" : "false");
558 {
559 SkAutoCanvasRestore autoRestore(canvas, true);
560 canvas->concat(m);
Cary Clark682c58d2018-05-16 07:07:07 -0400561 canvas->drawString(str, 0, 0, p);
Cary Clark154beea2017-10-26 07:58:48 -0400562 }
563 if (!isSimilarity) {
564 canvas->drawString(str, 40, below, p);
565 below += 20;
566 }
567 }
568 }
Cary Clarkbc5697d2017-10-04 14:31:33 -0400569##
570
Cary Clark154beea2017-10-26 07:58:48 -0400571#SeeAlso isScaleTranslate preservesRightAngles rectStaysRect isFixedStepInX
Cary Clarkbc5697d2017-10-04 14:31:33 -0400572
573##
574
575# ------------------------------------------------------------------------------
576
577#Method bool preservesRightAngles(SkScalar tol = SK_ScalarNearlyZero) const
Cary Clark4855f782018-02-06 09:41:53 -0500578#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500579#Line # returns if mapped 90 angle remains 90 degrees ##
Cary Clark154beea2017-10-26 07:58:48 -0400580Returns true if Matrix contains only translation, rotation, reflection, and
581scale. Scale may differ along rotated axes.
Cary Clark682c58d2018-05-16 07:07:07 -0400582Returns false if Matrix skewing, perspective, or degenerate forms that collapse
Cary Clark154beea2017-10-26 07:58:48 -0400583to a line or point.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400584
Cary Clark154beea2017-10-26 07:58:48 -0400585Preserves right angles, but not requiring that the arms of the angle
586retain equal lengths.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400587
Cary Clark154beea2017-10-26 07:58:48 -0400588#Param tol to be deprecated ##
589
590#Return true if Matrix only rotates, scales, translates ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400591
592#Example
Cary Clark154beea2017-10-26 07:58:48 -0400593#Height 128
594#Description
595Equal scale is both similar and preserves right angles.
596Unequal scale is not similar but preserves right angles.
597Skews are not similar and do not preserve right angles.
598##
599SkPaint p;
600p.setAntiAlias(true);
601SkMatrix m;
602int pos = 0;
603for (SkScalar sx : { 1, 2 } ) {
604 for (SkScalar kx : { 0, 1 } ) {
605 m.setAll(sx, kx, 16, 0, 1, 32, 0, 0, 1);
606 bool isSimilarity = m.isSimilarity();
607 bool preservesRightAngles = m.preservesRightAngles();
608 SkString str;
609 str.printf("sx: %g kx: %g %s %s", sx, kx, isSimilarity ? "sim" : "",
610 preservesRightAngles ? "right" : "");
611 SkAutoCanvasRestore autoRestore(canvas, true);
612 canvas->concat(m);
Cary Clark682c58d2018-05-16 07:07:07 -0400613 canvas->drawString(str, 0, pos, p);
Cary Clark154beea2017-10-26 07:58:48 -0400614 pos += 20;
615 }
616}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400617##
618
Cary Clark154beea2017-10-26 07:58:48 -0400619#SeeAlso isScaleTranslate isSimilarity rectStaysRect isFixedStepInX
Cary Clarkbc5697d2017-10-04 14:31:33 -0400620
621##
622
623# ------------------------------------------------------------------------------
624
Cary Clarkd98f78c2018-04-26 08:32:37 -0400625#Subtopic MemberIndex
626#In Constant
627#Line # member indices ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400628
629#Code
Cary Clarkd98f78c2018-04-26 08:32:37 -0400630 static constexpr int kMScaleX = 0;
631 static constexpr int kMSkewX = 1;
632 static constexpr int kMTransX = 2;
633 static constexpr int kMSkewY = 3;
634 static constexpr int kMScaleY = 4;
635 static constexpr int kMTransY = 5;
636 static constexpr int kMPersp0 = 6;
637 static constexpr int kMPersp1 = 7;
638 static constexpr int kMPersp2 = 8;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400639##
640
Cary Clark154beea2017-10-26 07:58:48 -0400641Matrix organizes its values in row order. These members correspond to
642each value in Matrix.
643
Cary Clarkbc5697d2017-10-04 14:31:33 -0400644#Const kMScaleX 0
Cary Clark682c58d2018-05-16 07:07:07 -0400645#Line # horizontal scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400646##
647#Const kMSkewX 1
Cary Clark682c58d2018-05-16 07:07:07 -0400648#Line # horizontal skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400649##
650#Const kMTransX 2
Cary Clark682c58d2018-05-16 07:07:07 -0400651#Line # horizontal translation ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400652##
653#Const kMSkewY 3
Cary Clark682c58d2018-05-16 07:07:07 -0400654#Line # vertical skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400655##
656#Const kMScaleY 4
Cary Clark682c58d2018-05-16 07:07:07 -0400657#Line # vertical scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400658##
659#Const kMTransY 5
Cary Clark682c58d2018-05-16 07:07:07 -0400660#Line # vertical translation ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400661##
662#Const kMPersp0 6
Cary Clark682c58d2018-05-16 07:07:07 -0400663#Line # input x perspective factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400664##
665#Const kMPersp1 7
Cary Clark682c58d2018-05-16 07:07:07 -0400666#Line # input y perspective factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400667##
668#Const kMPersp2 8
Cary Clark682c58d2018-05-16 07:07:07 -0400669#Line # perspective bias ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400670##
671
672#Example
Cary Clark154beea2017-10-26 07:58:48 -0400673SkPaint black;
674black.setAntiAlias(true);
675black.setTextSize(48);
676SkPaint gray = black;
677gray.setColor(0xFF9f9f9f);
678SkScalar offset[] = { 1.5f, 1.5f, 20, 1.5f, 1.5f, 20, .03f, .01f, 2 };
679for (int i : { SkMatrix::kMScaleX, SkMatrix::kMSkewX, SkMatrix::kMTransX,
680 SkMatrix::kMSkewY, SkMatrix::kMScaleY, SkMatrix::kMTransY,
681 SkMatrix::kMPersp0, SkMatrix::kMPersp1, SkMatrix::kMPersp2 } ) {
682 SkMatrix m;
683 m.setIdentity();
684 m.set(i, offset[i]);
685 SkAutoCanvasRestore autoRestore(canvas, true);
686 canvas->translate(22 + (i % 3) * 88, 44 + (i / 3) * 88);
687 canvas->drawString("&", 0, 0, gray);
688 canvas->concat(m);
689 canvas->drawString("&", 0, 0, black);
690}
Cary Clarkbc5697d2017-10-04 14:31:33 -0400691##
692
Cary Clark154beea2017-10-26 07:58:48 -0400693#SeeAlso get() set()
Cary Clarkbc5697d2017-10-04 14:31:33 -0400694
695##
696
697# ------------------------------------------------------------------------------
698
Cary Clarkd98f78c2018-04-26 08:32:37 -0400699#Subtopic AffineIndex
700#In Constant
701#Line # affine member indices ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400702
703#Code
Cary Clarkd98f78c2018-04-26 08:32:37 -0400704 static constexpr int kAScaleX = 0;
705 static constexpr int kASkewY = 1;
706 static constexpr int kASkewX = 2;
707 static constexpr int kAScaleY = 3;
708 static constexpr int kATransX = 4;
709 static constexpr int kATransY = 5;
Cary Clarkbc5697d2017-10-04 14:31:33 -0400710##
711
Cary Clark154beea2017-10-26 07:58:48 -0400712Affine arrays are in column major order to match the matrix used by
713PDF and XPS.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400714
715#Const kAScaleX 0
Cary Clark682c58d2018-05-16 07:07:07 -0400716#Line # horizontal scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400717##
718#Const kASkewY 1
Cary Clark682c58d2018-05-16 07:07:07 -0400719#Line # vertical skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400720##
721#Const kASkewX 2
Cary Clark682c58d2018-05-16 07:07:07 -0400722#Line # horizontal skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400723##
724#Const kAScaleY 3
Cary Clark682c58d2018-05-16 07:07:07 -0400725#Line # vertical scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400726##
727#Const kATransX 4
Cary Clark682c58d2018-05-16 07:07:07 -0400728#Line # horizontal translation ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400729##
730#Const kATransY 5
Cary Clark682c58d2018-05-16 07:07:07 -0400731#Line # vertical translation ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400732##
733
Cary Clark154beea2017-10-26 07:58:48 -0400734#NoExample
Cary Clarkbc5697d2017-10-04 14:31:33 -0400735##
736
Cary Clark154beea2017-10-26 07:58:48 -0400737#SeeAlso SetAffineIdentity asAffine setAffine
Cary Clarkbc5697d2017-10-04 14:31:33 -0400738
739##
740
741# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -0500742#Subtopic Operator
743#Populate
744##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400745
Cary Clarka560c472017-11-27 10:44:06 -0500746#Method SkScalar operator[](int index)_const
Cary Clarkbc5697d2017-10-04 14:31:33 -0400747
Cary Clark08895c42018-02-01 09:37:32 -0500748#Line # returns Matrix value ##
Cary Clark154beea2017-10-26 07:58:48 -0400749Returns one matrix value. Asserts if index is out of range and SK_DEBUG is
750defined.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400751
Cary Clark154beea2017-10-26 07:58:48 -0400752#Param index one of: kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY,
753 kMPersp0, kMPersp1, kMPersp2
Cary Clarkbc5697d2017-10-04 14:31:33 -0400754##
755
Cary Clark154beea2017-10-26 07:58:48 -0400756#Return value corresponding to index ##
757
758#Example
759SkMatrix matrix;
760matrix.setScale(42, 24);
761SkDebugf("matrix[SkMatrix::kMScaleX] %c= 42\n", matrix[SkMatrix::kMScaleX] == 42 ? '=' : '!');
762SkDebugf("matrix[SkMatrix::kMScaleY] %c= 24\n", matrix[SkMatrix::kMScaleY] == 24 ? '=' : '!');
763#StdOut
764matrix[SkMatrix::kMScaleX] == 42
765matrix[SkMatrix::kMScaleY] == 24
766##
767##
768
769#SeeAlso get set
Cary Clarkbc5697d2017-10-04 14:31:33 -0400770
771##
772
773# ------------------------------------------------------------------------------
774
775#Method SkScalar get(int index) const
Cary Clark4855f782018-02-06 09:41:53 -0500776#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500777#Line # returns one of nine Matrix values ##
Cary Clark154beea2017-10-26 07:58:48 -0400778Returns one matrix value. Asserts if index is out of range and SK_DEBUG is
779defined.
Cary Clarkbc5697d2017-10-04 14:31:33 -0400780
Cary Clark154beea2017-10-26 07:58:48 -0400781#Param index one of: kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY,
782 kMPersp0, kMPersp1, kMPersp2
Cary Clarkbc5697d2017-10-04 14:31:33 -0400783##
784
Cary Clark154beea2017-10-26 07:58:48 -0400785#Return value corresponding to index ##
786
787#Example
788SkMatrix matrix;
789matrix.setSkew(42, 24);
790SkDebugf("matrix.get(SkMatrix::kMSkewX) %c= 42\n",
791 matrix.get(SkMatrix::kMSkewX) == 42 ? '=' : '!');
792SkDebugf("matrix.get(SkMatrix::kMSkewY) %c= 24\n",
793 matrix.get(SkMatrix::kMSkewY) == 24 ? '=' : '!');
794#StdOut
795matrix.get(SkMatrix::kMSkewX) == 42
796matrix.get(SkMatrix::kMSkewY) == 24
797##
798##
799
800#SeeAlso operator[](int index) set
Cary Clarkbc5697d2017-10-04 14:31:33 -0400801
802##
803
804# ------------------------------------------------------------------------------
805
806#Method SkScalar getScaleX() const
Cary Clark4855f782018-02-06 09:41:53 -0500807#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500808#Line # returns horizontal scale factor ##
Cary Clark5538c132018-06-14 12:28:14 -0400809Returns scale factor multiplied by x-axis input, contributing to x-axis output.
Cary Clark154beea2017-10-26 07:58:48 -0400810With mapPoints, scales Points along the x-axis.
811
812#Return horizontal scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400813
814#Example
Cary Clark154beea2017-10-26 07:58:48 -0400815SkMatrix matrix;
816matrix.setScale(42, 24);
817SkDebugf("matrix.getScaleX() %c= 42\n", matrix.getScaleX() == 42 ? '=' : '!');
818#StdOut
819matrix.getScaleX() == 42
820##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400821##
822
Cary Clark154beea2017-10-26 07:58:48 -0400823#SeeAlso get getScaleY setScaleX setScale
Cary Clarkbc5697d2017-10-04 14:31:33 -0400824
825##
826
827# ------------------------------------------------------------------------------
828
829#Method SkScalar getScaleY() const
Cary Clark4855f782018-02-06 09:41:53 -0500830#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500831#Line # returns vertical scale factor ##
Cary Clark5538c132018-06-14 12:28:14 -0400832Returns scale factor multiplied by y-axis input, contributing to y-axis output.
Cary Clark154beea2017-10-26 07:58:48 -0400833With mapPoints, scales Points along the y-axis.
834
835#Return vertical scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400836
837#Example
Cary Clark154beea2017-10-26 07:58:48 -0400838SkMatrix matrix;
839matrix.setScale(42, 24);
840SkDebugf("matrix.getScaleY() %c= 24\n", matrix.getScaleY() == 24 ? '=' : '!');
841#StdOut
842matrix.getScaleY() == 24
843##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400844##
845
Cary Clark154beea2017-10-26 07:58:48 -0400846#SeeAlso get getScaleX setScaleY setScale
Cary Clarkbc5697d2017-10-04 14:31:33 -0400847
848##
849
850# ------------------------------------------------------------------------------
851
852#Method SkScalar getSkewY() const
Cary Clark4855f782018-02-06 09:41:53 -0500853#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500854#Line # returns vertical skew factor ##
Cary Clark5538c132018-06-14 12:28:14 -0400855Returns scale factor multiplied by x-axis input, contributing to y-axis output.
Cary Clark154beea2017-10-26 07:58:48 -0400856With mapPoints, skews Points along the y-axis.
Cary Clark5538c132018-06-14 12:28:14 -0400857Skewing both axes can rotate Points.
Cary Clark154beea2017-10-26 07:58:48 -0400858
859#Return vertical skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400860
861#Example
Cary Clark154beea2017-10-26 07:58:48 -0400862SkMatrix matrix;
863matrix.setSkew(42, 24);
864SkDebugf("matrix.getSkewY() %c= 24\n", matrix.getSkewY() == 24 ? '=' : '!');
865#StdOut
866matrix.getSkewY() == 24
867##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400868##
869
Cary Clark154beea2017-10-26 07:58:48 -0400870#SeeAlso get getSkewX setSkewY setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -0400871
872##
873
874# ------------------------------------------------------------------------------
875
876#Method SkScalar getSkewX() const
Cary Clark4855f782018-02-06 09:41:53 -0500877#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500878#Line # returns horizontal skew factor ##
Cary Clark5538c132018-06-14 12:28:14 -0400879Returns scale factor multiplied by y-axis input, contributing to x-axis output.
Cary Clark154beea2017-10-26 07:58:48 -0400880With mapPoints, skews Points along the x-axis.
Cary Clark5538c132018-06-14 12:28:14 -0400881Skewing both axes can rotate Points.
Cary Clark154beea2017-10-26 07:58:48 -0400882
883#Return horizontal scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400884
885#Example
Cary Clark154beea2017-10-26 07:58:48 -0400886SkMatrix matrix;
887matrix.setSkew(42, 24);
888SkDebugf("matrix.getSkewX() %c= 42\n", matrix.getSkewX() == 42 ? '=' : '!');
889#StdOut
890matrix.getSkewX() == 42
891##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400892##
893
Cary Clark154beea2017-10-26 07:58:48 -0400894#SeeAlso get getSkewY setSkewX setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -0400895
896##
897
898# ------------------------------------------------------------------------------
899
900#Method SkScalar getTranslateX() const
Cary Clark4855f782018-02-06 09:41:53 -0500901#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500902#Line # returns horizontal translation ##
Cary Clark5538c132018-06-14 12:28:14 -0400903Returns translation contributing to x-axis output.
Cary Clark154beea2017-10-26 07:58:48 -0400904With mapPoints, moves Points along the x-axis.
905
906#Return horizontal translation factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400907
908#Example
Cary Clark154beea2017-10-26 07:58:48 -0400909SkMatrix matrix;
910matrix.setTranslate(42, 24);
911SkDebugf("matrix.getTranslateX() %c= 42\n", matrix.getTranslateX() == 42 ? '=' : '!');
912#StdOut
913matrix.getTranslateX() == 42
914##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400915##
916
Cary Clark154beea2017-10-26 07:58:48 -0400917#SeeAlso get getTranslateY setTranslateX setTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -0400918
919##
920
921# ------------------------------------------------------------------------------
922
923#Method SkScalar getTranslateY() const
Cary Clark4855f782018-02-06 09:41:53 -0500924#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500925#Line # returns vertical translation ##
Cary Clark5538c132018-06-14 12:28:14 -0400926Returns translation contributing to y-axis output.
Cary Clark154beea2017-10-26 07:58:48 -0400927With mapPoints, moves Points along the y-axis.
928
929#Return vertical translation factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400930
931#Example
Cary Clark154beea2017-10-26 07:58:48 -0400932SkMatrix matrix;
933matrix.setTranslate(42, 24);
934SkDebugf("matrix.getTranslateY() %c= 24\n", matrix.getTranslateY() == 24 ? '=' : '!');
935#StdOut
936matrix.getTranslateY() == 24
937##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400938##
939
Cary Clark154beea2017-10-26 07:58:48 -0400940#SeeAlso get getTranslateX setTranslateY setTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -0400941
942##
943
944# ------------------------------------------------------------------------------
945
946#Method SkScalar getPerspX() const
Cary Clark4855f782018-02-06 09:41:53 -0500947#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500948#Line # returns input x perspective factor ##
Cary Clark5538c132018-06-14 12:28:14 -0400949Returns factor scaling input x-axis relative to input y-axis.
Cary Clark154beea2017-10-26 07:58:48 -0400950
Cary Clark5538c132018-06-14 12:28:14 -0400951#Return input x-axis perspective factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400952
953#Example
Cary Clark154beea2017-10-26 07:58:48 -0400954 SkMatrix m;
955 m.setIdentity();
956 m.set(SkMatrix::kMPersp0, -0.004f);
957 SkAutoCanvasRestore autoRestore(canvas, true);
958 canvas->translate(22, 144);
959 SkPaint black;
960 black.setAntiAlias(true);
961 black.setTextSize(24);
962 SkPaint gray = black;
963 gray.setColor(0xFF9f9f9f);
964 SkString string;
965 string.appendScalar(m.getPerspX());
966 canvas->drawString(string, 0, -72, gray);
967 canvas->concat(m);
968 canvas->drawString(string, 0, 0, black);
Cary Clarkbc5697d2017-10-04 14:31:33 -0400969##
970
Cary Clark154beea2017-10-26 07:58:48 -0400971#SeeAlso kMPersp0 getPerspY
Cary Clarkbc5697d2017-10-04 14:31:33 -0400972
973##
974
975# ------------------------------------------------------------------------------
976
977#Method SkScalar getPerspY() const
Cary Clark4855f782018-02-06 09:41:53 -0500978#In Property
Cary Clark08895c42018-02-01 09:37:32 -0500979#Line # returns input y perspective factor ##
Cary Clark154beea2017-10-26 07:58:48 -0400980
Cary Clark5538c132018-06-14 12:28:14 -0400981Returns factor scaling input y-axis relative to input x-axis.
Cary Clark154beea2017-10-26 07:58:48 -0400982
Cary Clark5538c132018-06-14 12:28:14 -0400983#Return input y-axis perspective factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -0400984
985#Example
Cary Clark154beea2017-10-26 07:58:48 -0400986 SkMatrix m;
987 m.setIdentity();
988 m.set(SkMatrix::kMPersp1, -0.004f);
989 SkAutoCanvasRestore autoRestore(canvas, true);
990 canvas->translate(22, 144);
991 SkPaint black;
992 black.setAntiAlias(true);
993 black.setTextSize(24);
994 SkPaint gray = black;
995 gray.setColor(0xFF9f9f9f);
996 SkString string;
997 string.appendScalar(m.getPerspY());
998 canvas->drawString(string, 0, -72, gray);
999 canvas->concat(m);
1000 canvas->drawString(string, 0, 0, black);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001001##
1002
Cary Clark154beea2017-10-26 07:58:48 -04001003#SeeAlso kMPersp1 getPerspX
Cary Clarkbc5697d2017-10-04 14:31:33 -04001004
1005##
1006
1007# ------------------------------------------------------------------------------
1008
Cary Clark154beea2017-10-26 07:58:48 -04001009#Method SkScalar& operator[](int index)
Cary Clarkbc5697d2017-10-04 14:31:33 -04001010
Cary Clark08895c42018-02-01 09:37:32 -05001011#Line # returns writable reference to Matrix value ##
Cary Clark154beea2017-10-26 07:58:48 -04001012Returns writable Matrix value. Asserts if index is out of range and SK_DEBUG is
1013defined. Clears internal cache anticipating that caller will change Matrix value.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001014
Cary Clark154beea2017-10-26 07:58:48 -04001015Next call to read Matrix state may recompute cache; subsequent writes to Matrix
1016value must be followed by dirtyMatrixTypeCache.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001017
Cary Clark154beea2017-10-26 07:58:48 -04001018#Param index one of: kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY,
1019 kMPersp0, kMPersp1, kMPersp2
Cary Clarkbc5697d2017-10-04 14:31:33 -04001020##
1021
Cary Clark154beea2017-10-26 07:58:48 -04001022#Return writable value corresponding to index ##
1023
1024#Example
1025SkMatrix matrix;
1026matrix.setIdentity();
1027SkDebugf("with identity matrix: x = %g\n", matrix.mapXY(24, 42).fX);
1028SkScalar& skewRef = matrix[SkMatrix::kMSkewX];
1029skewRef = 0;
1030SkDebugf("after skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
1031skewRef = 1;
1032SkDebugf("after 2nd skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
1033matrix.dirtyMatrixTypeCache();
1034SkDebugf("after dirty cache: x = %g\n", matrix.mapXY(24, 42).fX);
1035#StdOut
1036with identity matrix: x = 24
1037after skew x mod: x = 24
1038after 2nd skew x mod: x = 24
1039after dirty cache: x = 66
1040##
1041##
1042
1043#SeeAlso get dirtyMatrixTypeCache set
Cary Clarkbc5697d2017-10-04 14:31:33 -04001044
1045##
1046
1047# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001048#Subtopic Set
1049#Populate
Cary Clark682c58d2018-05-16 07:07:07 -04001050#Line # sets one or more matrix values ##
Cary Clark4855f782018-02-06 09:41:53 -05001051##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001052
1053#Method void set(int index, SkScalar value)
Cary Clark4855f782018-02-06 09:41:53 -05001054#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001055#Line # sets one value ##
Cary Clark154beea2017-10-26 07:58:48 -04001056Sets Matrix value. Asserts if index is out of range and SK_DEBUG is
1057defined. Safer than operator[]; internal cache is always maintained.
1058
1059#Param index one of: kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY,
1060 kMPersp0, kMPersp1, kMPersp2
1061##
1062#Param value Scalar to store in Matrix ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001063
1064#Example
Cary Clark154beea2017-10-26 07:58:48 -04001065SkMatrix matrix;
1066matrix.setIdentity();
1067SkDebugf("with identity matrix: x = %g\n", matrix.mapXY(24, 42).fX);
1068matrix.set(SkMatrix::kMSkewX, 0);
1069SkDebugf("after skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
1070matrix.set(SkMatrix::kMSkewX, 1);
1071SkDebugf("after 2nd skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
1072#StdOut
1073with identity matrix: x = 24
1074after skew x mod: x = 24
1075after 2nd skew x mod: x = 66
1076##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001077##
1078
Cary Clark154beea2017-10-26 07:58:48 -04001079#SeeAlso operator[] get
Cary Clarkbc5697d2017-10-04 14:31:33 -04001080
Cary Clark154beea2017-10-26 07:58:48 -04001081#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001082
1083# ------------------------------------------------------------------------------
1084
1085#Method void setScaleX(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001086#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001087#Line # sets horizontal scale factor ##
Cary Clark682c58d2018-05-16 07:07:07 -04001088Sets horizontal scale factor.
Cary Clark154beea2017-10-26 07:58:48 -04001089
1090#Param v horizontal scale factor to store ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001091
1092#Example
Cary Clark154beea2017-10-26 07:58:48 -04001093#Height 64
1094SkPaint paint;
1095paint.setAntiAlias(true);
1096paint.setTextSize(24);
1097canvas->drawString("normal", 12, 24, paint);
1098SkMatrix matrix;
1099matrix.setIdentity();
1100matrix.setScaleX(3);
1101canvas->concat(matrix);
1102canvas->drawString("x scale", 0, 48, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001103##
1104
Cary Clark154beea2017-10-26 07:58:48 -04001105#SeeAlso set setScale setScaleY
Cary Clarkbc5697d2017-10-04 14:31:33 -04001106
Cary Clark154beea2017-10-26 07:58:48 -04001107#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001108
1109# ------------------------------------------------------------------------------
1110
1111#Method void setScaleY(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001112#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001113#Line # sets vertical scale factor ##
Cary Clark682c58d2018-05-16 07:07:07 -04001114Sets vertical scale factor.
Cary Clark154beea2017-10-26 07:58:48 -04001115
1116#Param v vertical scale factor to store ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001117
1118#Example
Cary Clark154beea2017-10-26 07:58:48 -04001119#Height 192
1120SkPaint paint;
1121paint.setAntiAlias(true);
1122paint.setTextSize(24);
1123canvas->drawString("normal", 12, 24, paint);
1124SkMatrix matrix;
1125matrix.setIdentity();
1126matrix.setScaleY(3);
1127canvas->concat(matrix);
1128canvas->drawString("y scale", 12, 48, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001129##
1130
Cary Clark154beea2017-10-26 07:58:48 -04001131#SeeAlso set setScale setScaleX
Cary Clarkbc5697d2017-10-04 14:31:33 -04001132
Cary Clark154beea2017-10-26 07:58:48 -04001133#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001134
1135# ------------------------------------------------------------------------------
1136
1137#Method void setSkewY(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001138#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001139#Line # sets vertical skew factor ##
Cary Clark682c58d2018-05-16 07:07:07 -04001140Sets vertical skew factor.
Cary Clark154beea2017-10-26 07:58:48 -04001141
1142#Param v vertical skew factor to store ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001143
1144#Example
Cary Clark154beea2017-10-26 07:58:48 -04001145#Height 96
1146SkPaint paint;
1147paint.setAntiAlias(true);
1148paint.setTextSize(24);
1149canvas->drawString("normal", 12, 24, paint);
1150SkMatrix matrix;
1151matrix.setIdentity();
1152matrix.setSkewY(.3f);
1153canvas->concat(matrix);
1154canvas->drawString("y skew", 12, 48, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001155##
1156
Cary Clark154beea2017-10-26 07:58:48 -04001157#SeeAlso set setSkew setSkewX
Cary Clarkbc5697d2017-10-04 14:31:33 -04001158
Cary Clark154beea2017-10-26 07:58:48 -04001159#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001160
1161# ------------------------------------------------------------------------------
1162
1163#Method void setSkewX(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001164#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001165#Line # sets horizontal skew factor ##
Cary Clark682c58d2018-05-16 07:07:07 -04001166Sets horizontal skew factor.
Cary Clark154beea2017-10-26 07:58:48 -04001167
1168#Param v horizontal skew factor to store ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001169
1170#Example
Cary Clark154beea2017-10-26 07:58:48 -04001171#Height 64
1172SkPaint paint;
1173paint.setAntiAlias(true);
1174paint.setTextSize(24);
1175canvas->drawString("normal", 12, 24, paint);
1176SkMatrix matrix;
1177matrix.setIdentity();
1178matrix.setSkewX(-.7f);
1179canvas->concat(matrix);
1180canvas->drawString("x skew", 36, 48, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001181##
1182
Cary Clark154beea2017-10-26 07:58:48 -04001183#SeeAlso set setSkew setSkewX
Cary Clarkbc5697d2017-10-04 14:31:33 -04001184
Cary Clark154beea2017-10-26 07:58:48 -04001185#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001186
1187# ------------------------------------------------------------------------------
1188
1189#Method void setTranslateX(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001190#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001191#Line # sets horizontal translation ##
Cary Clark154beea2017-10-26 07:58:48 -04001192Sets horizontal translation.
1193
1194#Param v horizontal translation to store ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001195
1196#Example
Cary Clark154beea2017-10-26 07:58:48 -04001197#Height 48
1198SkPaint paint;
1199paint.setAntiAlias(true);
1200paint.setTextSize(24);
1201canvas->drawString("normal", 8, 24, paint);
1202SkMatrix matrix;
1203matrix.setIdentity();
1204matrix.setTranslateX(96);
1205canvas->concat(matrix);
1206canvas->drawString("x translate", 8, 24, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001207##
1208
Cary Clark154beea2017-10-26 07:58:48 -04001209#SeeAlso set setTranslate setTranslateY
Cary Clarkbc5697d2017-10-04 14:31:33 -04001210
Cary Clark154beea2017-10-26 07:58:48 -04001211#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001212
1213# ------------------------------------------------------------------------------
1214
1215#Method void setTranslateY(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001216#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001217#Line # sets vertical translation ##
Cary Clark154beea2017-10-26 07:58:48 -04001218Sets vertical translation.
1219
1220#Param v vertical translation to store ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001221
1222#Example
Cary Clark154beea2017-10-26 07:58:48 -04001223#Height 64
1224SkPaint paint;
1225paint.setAntiAlias(true);
1226paint.setTextSize(24);
1227canvas->drawString("normal", 8, 24, paint);
1228SkMatrix matrix;
1229matrix.setIdentity();
1230matrix.setTranslateY(24);
1231canvas->concat(matrix);
1232canvas->drawString("y translate", 8, 24, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001233##
1234
Cary Clark154beea2017-10-26 07:58:48 -04001235#SeeAlso set setTranslate setTranslateX
Cary Clarkbc5697d2017-10-04 14:31:33 -04001236
Cary Clark154beea2017-10-26 07:58:48 -04001237#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001238
1239# ------------------------------------------------------------------------------
1240
1241#Method void setPerspX(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001242#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001243#Line # sets input x perspective factor ##
Cary Clark5538c132018-06-14 12:28:14 -04001244Sets input x-axis perspective factor, which causes mapXY to vary input x-axis values
1245inversely proportional to input y-axis values.
Cary Clark154beea2017-10-26 07:58:48 -04001246
1247#Param v perspective factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001248
1249#Example
Cary Clark154beea2017-10-26 07:58:48 -04001250#Image 4
1251for (SkScalar perspX : { -.003f, 0.f, .003f, .012f } ) {
1252 SkMatrix matrix;
1253 matrix.setIdentity();
1254 matrix.setPerspX(perspX);
1255 canvas->save();
1256 canvas->concat(matrix);
1257 canvas->drawBitmap(source, 0, 0);
1258 canvas->restore();
1259 canvas->translate(64, 64);
1260}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001261##
1262
Cary Clarkbef063a2017-10-31 15:44:45 -04001263#SeeAlso getPerspX set setAll set9 MakeAll
Cary Clarkbc5697d2017-10-04 14:31:33 -04001264
Cary Clark154beea2017-10-26 07:58:48 -04001265#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001266
1267# ------------------------------------------------------------------------------
1268
1269#Method void setPerspY(SkScalar v)
Cary Clark4855f782018-02-06 09:41:53 -05001270#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001271#Line # sets input y perspective factor ##
Cary Clark5538c132018-06-14 12:28:14 -04001272Sets input y-axis perspective factor, which causes mapXY to vary input y-axis values
1273inversely proportional to input x-axis values.
Cary Clark154beea2017-10-26 07:58:48 -04001274
1275#Param v perspective factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001276
1277#Example
Cary Clark154beea2017-10-26 07:58:48 -04001278#Image 4
1279for (SkScalar perspX : { -.003f, 0.f, .003f, .012f } ) {
1280 SkMatrix matrix;
1281 matrix.setIdentity();
1282 matrix.setPerspY(perspX);
1283 canvas->save();
1284 canvas->concat(matrix);
1285 canvas->drawBitmap(source, 0, 0);
1286 canvas->restore();
1287 canvas->translate(64, 64);
1288}
Cary Clarkbc5697d2017-10-04 14:31:33 -04001289##
1290
Cary Clarkbef063a2017-10-31 15:44:45 -04001291#SeeAlso getPerspY set setAll set9 MakeAll
Cary Clarkbc5697d2017-10-04 14:31:33 -04001292
Cary Clark154beea2017-10-26 07:58:48 -04001293#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001294
1295# ------------------------------------------------------------------------------
1296
1297#Method void setAll(SkScalar scaleX, SkScalar skewX, SkScalar transX,
1298 SkScalar skewY, SkScalar scaleY, SkScalar transY,
1299 SkScalar persp0, SkScalar persp1, SkScalar persp2)
Cary Clark4855f782018-02-06 09:41:53 -05001300#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001301#Line # sets all values from parameters ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001302
Cary Clark154beea2017-10-26 07:58:48 -04001303Sets all values from parameters. Sets matrix to:
1304
1305#Code
1306#Literal
1307| scaleX skewX transX |
1308| skewY scaleY transY |
1309| persp0 persp1 persp2 |
1310##
1311
1312#Param scaleX horizontal scale factor to store ##
1313#Param skewX horizontal skew factor to store ##
1314#Param transX horizontal translation to store ##
1315#Param skewY vertical skew factor to store ##
1316#Param scaleY vertical scale factor to store ##
1317#Param transY vertical translation to store ##
Cary Clark5538c132018-06-14 12:28:14 -04001318#Param persp0 input x-axis values perspective factor to store ##
1319#Param persp1 input y-axis values perspective factor to store ##
Cary Clark154beea2017-10-26 07:58:48 -04001320#Param persp2 perspective scale factor to store ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001321
1322#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001323#Height 128
Cary Clark154beea2017-10-26 07:58:48 -04001324 SkPaint p;
1325 p.setAntiAlias(true);
1326 p.setTextSize(64);
1327 SkMatrix m;
1328 for (SkScalar sx : { -1, 1 } ) {
1329 for (SkScalar sy : { -1, 1 } ) {
1330 SkAutoCanvasRestore autoRestore(canvas, true);
Cary Clark2ade9972017-11-02 17:49:34 -04001331 m.setAll(sx, 1, 128, 0, sy, 64, 0, 0, 1);
Cary Clark154beea2017-10-26 07:58:48 -04001332 canvas->concat(m);
1333 canvas->drawString("K", 0, 0, p);
1334 }
1335 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001336##
1337
Cary Clarkbef063a2017-10-31 15:44:45 -04001338#SeeAlso set9 MakeAll
Cary Clarkbc5697d2017-10-04 14:31:33 -04001339
Cary Clark154beea2017-10-26 07:58:48 -04001340#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001341
1342# ------------------------------------------------------------------------------
1343
1344#Method void get9(SkScalar buffer[9]) const
Cary Clark4855f782018-02-06 09:41:53 -05001345#In Property
Cary Clark08895c42018-02-01 09:37:32 -05001346#Line # returns all nine Matrix values ##
Cary Clark154beea2017-10-26 07:58:48 -04001347Copies nine Scalar values contained by Matrix into buffer, in member value
1348ascending order: kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY,
1349kMPersp0, kMPersp1, kMPersp2.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001350
Cary Clark154beea2017-10-26 07:58:48 -04001351#Param buffer storage for nine Scalar values ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001352
1353#Example
Cary Clark154beea2017-10-26 07:58:48 -04001354SkMatrix matrix = SkMatrix::MakeRectToRect({0, 0, 1, 1}, {3, 4, 7, 9},
1355 SkMatrix::kFill_ScaleToFit);
1356SkScalar b[9];
1357matrix.get9(b);
Cary Clark682c58d2018-05-16 07:07:07 -04001358SkDebugf("{%g, %g, %g},\n{%g, %g, %g},\n{%g, %g, %g}\n", b[0], b[1], b[2],
Cary Clark154beea2017-10-26 07:58:48 -04001359 b[3], b[4], b[5], b[6], b[7], b[8]);
1360#StdOut
1361{4, 0, 3},
1362{0, 5, 4},
1363{0, 0, 1}
Cary Clark682c58d2018-05-16 07:07:07 -04001364##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001365##
1366
Cary Clark154beea2017-10-26 07:58:48 -04001367#SeeAlso set9
Cary Clarkbc5697d2017-10-04 14:31:33 -04001368
Cary Clark154beea2017-10-26 07:58:48 -04001369#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001370
1371# ------------------------------------------------------------------------------
1372
1373#Method void set9(const SkScalar buffer[9])
Cary Clark4855f782018-02-06 09:41:53 -05001374#In Set
1375#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05001376#Line # sets all values from Scalar array ##
Cary Clark154beea2017-10-26 07:58:48 -04001377Sets Matrix to nine Scalar values in buffer, in member value ascending order:
1378kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY, kMPersp0, kMPersp1,
1379kMPersp2.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001380
Cary Clark154beea2017-10-26 07:58:48 -04001381Sets matrix to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04001382
Cary Clark154beea2017-10-26 07:58:48 -04001383#Code
1384#Literal
1385| buffer[0] buffer[1] buffer[2] |
1386| buffer[3] buffer[4] buffer[5] |
1387| buffer[6] buffer[7] buffer[8] |
1388##
1389
1390In the future, set9 followed by get9 may not return the same values. Since Matrix
1391maps non-homogeneous coordinates, scaling all nine values produces an equivalent
1392transformation, possibly improving precision.
1393
1394#Param buffer nine Scalar values ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001395
1396#Example
Cary Clark154beea2017-10-26 07:58:48 -04001397#Image 4
1398SkMatrix m;
1399SkScalar buffer[9] = {4, 0, 3, 0, 5, 4, 0, 0, 1};
1400m.set9(buffer);
1401canvas->concat(m);
1402canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001403##
1404
Cary Clarkbef063a2017-10-31 15:44:45 -04001405#SeeAlso setAll get9 MakeAll
Cary Clarkbc5697d2017-10-04 14:31:33 -04001406
Cary Clark154beea2017-10-26 07:58:48 -04001407#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001408
1409# ------------------------------------------------------------------------------
1410
1411#Method void reset()
Cary Clark4855f782018-02-06 09:41:53 -05001412#In Constructor
1413#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001414#Line # sets Matrix to identity ##
Cary Clark154beea2017-10-26 07:58:48 -04001415Sets Matrix to identity; which has no effect on mapped Points. Sets Matrix to:
1416
1417#Code
1418#Literal
1419| 1 0 0 |
1420| 0 1 0 |
1421| 0 0 1 |
1422##
1423
1424Also called setIdentity(); use the one that provides better inline
1425documentation.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001426
1427#Example
Cary Clark154beea2017-10-26 07:58:48 -04001428SkMatrix m;
1429m.reset();
1430SkDebugf("m.isIdentity(): %s\n", m.isIdentity() ? "true" : "false");
1431#StdOut
1432m.isIdentity(): true
1433##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001434##
1435
Cary Clark154beea2017-10-26 07:58:48 -04001436#SeeAlso isIdentity setIdentity
Cary Clarkbc5697d2017-10-04 14:31:33 -04001437
Cary Clark154beea2017-10-26 07:58:48 -04001438#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001439
1440# ------------------------------------------------------------------------------
1441
1442#Method void setIdentity()
Cary Clark4855f782018-02-06 09:41:53 -05001443#In Constructor
1444#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001445#Line # sets Matrix to identity ##
Cary Clark154beea2017-10-26 07:58:48 -04001446Sets Matrix to identity; which has no effect on mapped Points. Sets Matrix to:
1447
1448#Code
1449#Literal
1450| 1 0 0 |
1451| 0 1 0 |
1452| 0 0 1 |
1453##
1454
1455Also called reset(); use the one that provides better inline
1456documentation.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001457
1458#Example
Cary Clark154beea2017-10-26 07:58:48 -04001459SkMatrix m;
1460m.setIdentity();
1461SkDebugf("m.isIdentity(): %s\n", m.isIdentity() ? "true" : "false");
1462#StdOut
1463m.isIdentity(): true
1464##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001465##
1466
Cary Clark154beea2017-10-26 07:58:48 -04001467#SeeAlso isIdentity reset
Cary Clarkbc5697d2017-10-04 14:31:33 -04001468
Cary Clark154beea2017-10-26 07:58:48 -04001469#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001470
1471# ------------------------------------------------------------------------------
1472
1473#Method void setTranslate(SkScalar dx, SkScalar dy)
Cary Clark4855f782018-02-06 09:41:53 -05001474#In Constructor
1475#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001476#Line # sets to translate in x and y ##
Cary Clark154beea2017-10-26 07:58:48 -04001477Sets Matrix to translate by (dx, dy).
Cary Clarkbc5697d2017-10-04 14:31:33 -04001478
Cary Clark154beea2017-10-26 07:58:48 -04001479#Param dx horizontal translation ##
1480#Param dy vertical translation ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001481
1482#Example
Cary Clark154beea2017-10-26 07:58:48 -04001483#Height 64
1484SkPaint paint;
1485paint.setAntiAlias(true);
1486paint.setTextSize(24);
1487canvas->drawString("normal", 8, 24, paint);
1488SkMatrix matrix;
1489matrix.setTranslate(96, 24);
1490canvas->concat(matrix);
1491canvas->drawString("translate", 8, 24, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001492##
1493
Cary Clark154beea2017-10-26 07:58:48 -04001494#SeeAlso setTranslateX setTranslateY
Cary Clarkbc5697d2017-10-04 14:31:33 -04001495
Cary Clark154beea2017-10-26 07:58:48 -04001496#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001497
1498# ------------------------------------------------------------------------------
1499
1500#Method void setTranslate(const SkVector& v)
1501
Cary Clark154beea2017-10-26 07:58:48 -04001502Sets Matrix to translate by (v.fX, v.fY).
1503
1504#Param v Vector containing horizontal and vertical translation ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001505
1506#Example
Cary Clark154beea2017-10-26 07:58:48 -04001507#Height 64
1508SkPaint paint;
1509paint.setAntiAlias(true);
1510paint.setTextSize(24);
1511canvas->drawString("normal", 8, 24, paint);
1512SkMatrix matrix;
1513matrix.setTranslate({96, 24});
1514canvas->concat(matrix);
1515canvas->drawString("translate", 8, 24, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001516##
1517
Cary Clark154beea2017-10-26 07:58:48 -04001518#SeeAlso setTranslateX setTranslateY MakeTrans
Cary Clarkbc5697d2017-10-04 14:31:33 -04001519
Cary Clark154beea2017-10-26 07:58:48 -04001520#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001521
1522# ------------------------------------------------------------------------------
1523
1524#Method void setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05001525#In Constructor
1526#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001527#Line # sets to scale about a point ##
Cary Clark154beea2017-10-26 07:58:48 -04001528Sets Matrix to scale by sx and sy, about a pivot point at (px, py).
1529The pivot point is unchanged when mapped with Matrix.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001530
Cary Clark154beea2017-10-26 07:58:48 -04001531#Param sx horizontal scale factor ##
1532#Param sy vertical scale factor ##
1533#Param px pivot x ##
1534#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001535
1536#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001537#Height 128
Cary Clark154beea2017-10-26 07:58:48 -04001538 SkPaint p;
1539 p.setAntiAlias(true);
1540 p.setTextSize(64);
1541 SkMatrix m;
1542 for (SkScalar sx : { -1, 1 } ) {
1543 for (SkScalar sy : { -1, 1 } ) {
1544 SkAutoCanvasRestore autoRestore(canvas, true);
Cary Clark2ade9972017-11-02 17:49:34 -04001545 m.setScale(sx, sy, 128, 64);
Cary Clark154beea2017-10-26 07:58:48 -04001546 canvas->concat(m);
Cary Clark2ade9972017-11-02 17:49:34 -04001547 canvas->drawString("%", 128, 64, p);
Cary Clark154beea2017-10-26 07:58:48 -04001548 }
1549 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001550##
1551
Cary Clark154beea2017-10-26 07:58:48 -04001552#SeeAlso setScaleX setScaleY MakeScale preScale postScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04001553
Cary Clark154beea2017-10-26 07:58:48 -04001554#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001555
1556# ------------------------------------------------------------------------------
1557
1558#Method void setScale(SkScalar sx, SkScalar sy)
1559
Cary Clark154beea2017-10-26 07:58:48 -04001560Sets Matrix to scale by sx and sy about at pivot point at (0, 0).
Cary Clarkbc5697d2017-10-04 14:31:33 -04001561
Cary Clark154beea2017-10-26 07:58:48 -04001562#Param sx horizontal scale factor ##
1563#Param sy vertical scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001564
1565#Example
Cary Clark2ade9972017-11-02 17:49:34 -04001566#Height 128
Cary Clark154beea2017-10-26 07:58:48 -04001567 SkPaint p;
1568 p.setAntiAlias(true);
1569 p.setTextSize(64);
1570 SkMatrix m;
1571 for (SkScalar sx : { -1, 1 } ) {
1572 for (SkScalar sy : { -1, 1 } ) {
1573 SkAutoCanvasRestore autoRestore(canvas, true);
1574 m.setScale(sx, sy);
Cary Clark2ade9972017-11-02 17:49:34 -04001575 m.postTranslate(128, 64);
Cary Clark154beea2017-10-26 07:58:48 -04001576 canvas->concat(m);
Cary Clark2ade9972017-11-02 17:49:34 -04001577 canvas->drawString("@", 0, 0, p);
Cary Clark154beea2017-10-26 07:58:48 -04001578 }
1579 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001580##
1581
Cary Clark154beea2017-10-26 07:58:48 -04001582#SeeAlso setScaleX setScaleY MakeScale preScale postScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04001583
Cary Clark154beea2017-10-26 07:58:48 -04001584#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001585
1586# ------------------------------------------------------------------------------
1587
1588#Method void setRotate(SkScalar degrees, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05001589#In Constructor
1590#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001591#Line # sets to rotate about a point ##
Cary Clark154beea2017-10-26 07:58:48 -04001592Sets Matrix to rotate by degrees about a pivot point at (px, py).
1593The pivot point is unchanged when mapped with Matrix.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001594
Cary Clark154beea2017-10-26 07:58:48 -04001595Positive degrees rotates clockwise.
1596
1597#Param degrees angle of axes relative to upright axes ##
1598#Param px pivot x ##
1599#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001600
1601#Example
Cary Clark154beea2017-10-26 07:58:48 -04001602#Height 128
1603 SkPaint paint;
1604 paint.setColor(SK_ColorGRAY);
1605 paint.setAntiAlias(true);
1606 SkRect rect = {20, 20, 100, 100};
1607 canvas->drawRect(rect, paint);
1608 paint.setColor(SK_ColorRED);
1609 SkMatrix matrix;
1610 matrix.setRotate(25, rect.centerX(), rect.centerY());
1611 canvas->concat(matrix);
1612 canvas->drawRect(rect, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001613##
1614
Cary Clark154beea2017-10-26 07:58:48 -04001615#SeeAlso setSinCos preRotate postRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001616
Cary Clark154beea2017-10-26 07:58:48 -04001617#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001618
1619# ------------------------------------------------------------------------------
1620
1621#Method void setRotate(SkScalar degrees)
1622
Cary Clark154beea2017-10-26 07:58:48 -04001623Sets Matrix to rotate by degrees about a pivot point at (0, 0).
1624Positive degrees rotates clockwise.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001625
Cary Clark154beea2017-10-26 07:58:48 -04001626#Param degrees angle of axes relative to upright axes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001627
1628#Example
Cary Clark154beea2017-10-26 07:58:48 -04001629#Height 128
1630 SkPaint paint;
1631 paint.setColor(SK_ColorGRAY);
1632 paint.setAntiAlias(true);
1633 SkRect rect = {20, 20, 100, 100};
1634 canvas->drawRect(rect, paint);
1635 paint.setColor(SK_ColorRED);
1636 SkMatrix matrix;
1637 matrix.setRotate(25);
1638 canvas->translate(rect.centerX(), rect.centerY());
1639 canvas->concat(matrix);
1640 canvas->translate(-rect.centerX(), -rect.centerY());
1641 canvas->drawRect(rect, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001642##
1643
Cary Clark154beea2017-10-26 07:58:48 -04001644#SeeAlso setSinCos preRotate postRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001645
Cary Clark154beea2017-10-26 07:58:48 -04001646#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001647
1648# ------------------------------------------------------------------------------
1649
1650#Method void setSinCos(SkScalar sinValue, SkScalar cosValue,
1651 SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05001652#In Constructor
1653#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001654#Line # sets to rotate and scale about a point ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001655
Cary Clark682c58d2018-05-16 07:07:07 -04001656Sets Matrix to rotate by sinValue and cosValue, about a pivot point at (px, py).
Cary Clark154beea2017-10-26 07:58:48 -04001657The pivot point is unchanged when mapped with Matrix.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001658
Cary Clark154beea2017-10-26 07:58:48 -04001659Vector (sinValue, cosValue) describes the angle of rotation relative to (0, 1).
1660Vector length specifies scale.
1661
Cary Clark5538c132018-06-14 12:28:14 -04001662#Param sinValue rotation vector x-axis component ##
1663#Param cosValue rotation vector y-axis component ##
1664#Param px pivot x-axis ##
1665#Param py pivot y-axis ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001666
1667#Example
Cary Clark154beea2017-10-26 07:58:48 -04001668#Height 128
1669 SkPaint paint;
1670 paint.setColor(SK_ColorGRAY);
1671 paint.setAntiAlias(true);
1672 SkRect rect = {20, 20, 100, 100};
1673 canvas->drawRect(rect, paint);
1674 paint.setColor(SK_ColorRED);
1675 SkMatrix matrix;
1676 matrix.setSinCos(.25f, .85f, rect.centerX(), rect.centerY());
1677 canvas->concat(matrix);
1678 canvas->drawRect(rect, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001679##
1680
Cary Clark154beea2017-10-26 07:58:48 -04001681#SeeAlso setRotate setScale setRSXform
Cary Clarkbc5697d2017-10-04 14:31:33 -04001682
Cary Clark154beea2017-10-26 07:58:48 -04001683#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001684
1685# ------------------------------------------------------------------------------
1686
1687#Method void setSinCos(SkScalar sinValue, SkScalar cosValue)
1688
Cary Clark682c58d2018-05-16 07:07:07 -04001689Sets Matrix to rotate by sinValue and cosValue, about a pivot point at (0, 0).
Cary Clarkbc5697d2017-10-04 14:31:33 -04001690
Cary Clark154beea2017-10-26 07:58:48 -04001691Vector (sinValue, cosValue) describes the angle of rotation relative to (0, 1).
1692Vector length specifies scale.
1693
Cary Clark5538c132018-06-14 12:28:14 -04001694#Param sinValue rotation vector x-axis component ##
1695#Param cosValue rotation vector y-axis component ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001696
1697#Example
Cary Clark154beea2017-10-26 07:58:48 -04001698#Description
1699Canvas needs offset after applying Matrix to pivot about Rect center.
1700##
1701#Height 128
1702 SkPaint paint;
1703 paint.setColor(SK_ColorGRAY);
1704 paint.setAntiAlias(true);
1705 SkRect rect = {20, 20, 100, 100};
1706 canvas->drawRect(rect, paint);
1707 paint.setColor(SK_ColorRED);
1708 SkMatrix matrix;
1709 matrix.setSinCos(.25f, .85f);
1710 matrix.postTranslate(rect.centerX(), rect.centerY());
1711 canvas->concat(matrix);
1712 canvas->translate(-rect.centerX(), -rect.centerY());
1713 canvas->drawRect(rect, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001714##
1715
Cary Clark154beea2017-10-26 07:58:48 -04001716#SeeAlso setRotate setScale setRSXform
Cary Clarkbc5697d2017-10-04 14:31:33 -04001717
Cary Clark154beea2017-10-26 07:58:48 -04001718#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001719
1720# ------------------------------------------------------------------------------
1721
1722#Method SkMatrix& setRSXform(const SkRSXform& rsxForm)
Cary Clark4855f782018-02-06 09:41:53 -05001723#In Constructor
1724#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001725#Line # sets to rotate, scale, and translate ##
Cary Clark154beea2017-10-26 07:58:48 -04001726Sets Matrix to rotate, scale, and translate using a compressed matrix form.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001727
Cary Clark154beea2017-10-26 07:58:48 -04001728Vector (rsxForm.fSSin, rsxForm.fSCos) describes the angle of rotation relative
1729to (0, 1). Vector length specifies scale. Mapped point is rotated and scaled
1730by Vector, then translated by (rsxForm.fTx, rsxForm.fTy).
1731
1732#Param rsxForm compressed RSXform matrix ##
1733
1734#Return reference to Matrix ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001735
1736#Example
Cary Clark154beea2017-10-26 07:58:48 -04001737#Description
1738Canvas needs offset after applying Matrix to pivot about Rect center.
1739##
1740#Height 128
1741 SkPaint paint;
1742 paint.setColor(SK_ColorGRAY);
1743 paint.setAntiAlias(true);
1744 SkRect rect = {20, 20, 100, 100};
1745 canvas->drawRect(rect, paint);
1746 paint.setColor(SK_ColorRED);
1747 SkMatrix matrix;
1748 matrix.setRSXform(SkRSXform::Make(.85f, .25f, rect.centerX(), rect.centerY()));
1749 canvas->concat(matrix);
1750 canvas->translate(-rect.centerX(), -rect.centerY());
1751 canvas->drawRect(rect, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001752##
1753
Cary Clark154beea2017-10-26 07:58:48 -04001754#SeeAlso setSinCos setScale setTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -04001755
Cary Clark154beea2017-10-26 07:58:48 -04001756#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001757
1758# ------------------------------------------------------------------------------
1759
1760#Method void setSkew(SkScalar kx, SkScalar ky, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05001761#In Constructor
1762#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001763#Line # sets to skew about a point ##
Cary Clark154beea2017-10-26 07:58:48 -04001764Sets Matrix to skew by kx and ky, about a pivot point at (px, py).
1765The pivot point is unchanged when mapped with Matrix.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001766
Cary Clark154beea2017-10-26 07:58:48 -04001767#Param kx horizontal skew factor ##
1768#Param ky vertical skew factor ##
1769#Param px pivot x ##
1770#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001771
1772#Example
Cary Clark154beea2017-10-26 07:58:48 -04001773 SkPaint p;
1774 p.setAntiAlias(true);
1775 p.setTextSize(48);
1776 SkMatrix m;
1777 for (SkScalar sx : { -1, 0, 1 } ) {
1778 for (SkScalar sy : { -1, 0, 1 } ) {
1779 SkAutoCanvasRestore autoRestore(canvas, true);
1780 m.setSkew(sx, sy, 96 + 64 * sx, 128 + 48 * sy);
1781 canvas->concat(m);
1782 canvas->drawString("K", 96 + 64 * sx, 128 + 48 * sy, p);
1783 }
1784 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001785##
1786
Cary Clark154beea2017-10-26 07:58:48 -04001787#SeeAlso setSkewX setSkewY preSkew postSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04001788
Cary Clark154beea2017-10-26 07:58:48 -04001789#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001790
1791# ------------------------------------------------------------------------------
1792
1793#Method void setSkew(SkScalar kx, SkScalar ky)
1794
Cary Clark154beea2017-10-26 07:58:48 -04001795Sets Matrix to skew by kx and ky, about a pivot point at (0, 0).
Cary Clarkbc5697d2017-10-04 14:31:33 -04001796
Cary Clark154beea2017-10-26 07:58:48 -04001797#Param kx horizontal skew factor ##
1798#Param ky vertical skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001799
1800#Example
Cary Clark154beea2017-10-26 07:58:48 -04001801 SkPaint p;
1802 p.setAntiAlias(true);
1803 p.setTextSize(48);
1804 SkMatrix m;
1805 for (SkScalar sx : { -1, 0, 1 } ) {
1806 for (SkScalar sy : { -1, 0, 1 } ) {
1807 SkAutoCanvasRestore autoRestore(canvas, true);
1808 m.setSkew(sx, sy);
1809 m.postTranslate(96 + 64 * sx, 128 + 48 * sy);
1810 canvas->concat(m);
1811 canvas->drawString("K", 0, 0, p);
1812 }
1813 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001814##
1815
Cary Clark154beea2017-10-26 07:58:48 -04001816#SeeAlso setSkewX setSkewY preSkew postSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04001817
Cary Clark154beea2017-10-26 07:58:48 -04001818#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001819
1820# ------------------------------------------------------------------------------
1821
1822#Method void setConcat(const SkMatrix& a, const SkMatrix& b)
Cary Clark4855f782018-02-06 09:41:53 -05001823#In Constructor
1824#In Set
Cary Clark08895c42018-02-01 09:37:32 -05001825#Line # sets to Matrix parameter multiplied by Matrix parameter ##
Cary Clark154beea2017-10-26 07:58:48 -04001826Sets Matrix to Matrix a multiplied by Matrix b. Either a or b may be this.
Cary Clarkbc5697d2017-10-04 14:31:33 -04001827
Cary Clark154beea2017-10-26 07:58:48 -04001828Given:
1829
1830#Code
1831#Literal
1832 | A B C | | J K L |
1833a = | D E F |, b = | M N O |
1834 | G H I | | P Q R |
1835##
1836
1837sets Matrix to:
1838
1839#Code
1840#Literal
1841 | A B C | | J K L | | AJ+BM+CP AK+BN+CQ AL+BO+CR |
1842a * b = | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR |
1843 | G H I | | P Q R | | GJ+HM+IP GK+HN+IQ GL+HO+IR |
1844##
1845
1846#Param a Matrix on left side of multiply expression ##
1847#Param b Matrix on right side of multiply expression ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001848
1849#Example
Cary Clark154beea2017-10-26 07:58:48 -04001850#Image 3
1851#Description
1852setPolyToPoly creates perspective matrices, one the inverse of the other.
1853Multiplying the matrix by its inverse turns into an identity matrix.
1854##
1855SkMatrix matrix, matrix2;
1856SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
1857SkRect::Make(source.bounds()).toQuad(bitmapBounds);
1858matrix.setPolyToPoly(bitmapBounds, perspect, 4);
1859matrix2.setPolyToPoly(perspect, bitmapBounds, 4);
1860matrix.setConcat(matrix, matrix2);
1861canvas->concat(matrix);
1862canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001863##
1864
Cary Clark154beea2017-10-26 07:58:48 -04001865#SeeAlso Concat preConcat postConcat SkCanvas::concat
Cary Clarkbc5697d2017-10-04 14:31:33 -04001866
Cary Clark154beea2017-10-26 07:58:48 -04001867#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001868
1869# ------------------------------------------------------------------------------
1870
1871#Method void preTranslate(SkScalar dx, SkScalar dy)
Cary Clark4855f782018-02-06 09:41:53 -05001872#In Set
1873#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001874#Line # pre-multiplies Matrix by translation ##
Cary Clark154beea2017-10-26 07:58:48 -04001875Sets Matrix to Matrix multiplied by Matrix constructed from translation (dx, dy).
1876This can be thought of as moving the point to be mapped before applying Matrix.
1877
1878Given:
1879
1880#Code
1881#Literal
1882 | A B C | | 1 0 dx |
1883Matrix = | D E F |, T(dx, dy) = | 0 1 dy |
1884 | G H I | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04001885##
1886
Cary Clark154beea2017-10-26 07:58:48 -04001887sets Matrix to:
1888
1889#Code
1890#Literal
1891 | A B C | | 1 0 dx | | A B A*dx+B*dy+C |
1892Matrix * T(dx, dy) = | D E F | | 0 1 dy | = | D E D*dx+E*dy+F |
1893 | G H I | | 0 0 1 | | G H G*dx+H*dy+I |
1894##
1895
Cary Clark5538c132018-06-14 12:28:14 -04001896#Param dx x-axis translation before applying Matrix ##
1897#Param dy y-axis translation before applying Matrix ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001898
1899#Example
Cary Clark154beea2017-10-26 07:58:48 -04001900#Height 160
1901 SkPaint paint;
1902 paint.setAntiAlias(true);
1903 SkRect rect = {20, 20, 100, 100};
1904 for (int i = 0; i < 2; ++i ) {
1905 SkMatrix matrix;
1906 i == 0 ? matrix.reset(): matrix.setRotate(25, rect.centerX(), 320);
Cary Clark682c58d2018-05-16 07:07:07 -04001907 {
Cary Clark154beea2017-10-26 07:58:48 -04001908 SkAutoCanvasRestore acr(canvas, true);
1909 canvas->concat(matrix);
1910 paint.setColor(SK_ColorGRAY);
1911 canvas->drawRect(rect, paint);
1912 }
1913 paint.setColor(SK_ColorRED);
1914 for (int j = 0; j < 2; ++j ) {
1915 SkAutoCanvasRestore acr(canvas, true);
1916 matrix.preTranslate(40, 40);
1917 canvas->concat(matrix);
1918 canvas->drawCircle(0, 0, 3, paint);
1919 }
1920 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04001921##
1922
Cary Clark154beea2017-10-26 07:58:48 -04001923#SeeAlso postTranslate setTranslate MakeTrans
Cary Clarkbc5697d2017-10-04 14:31:33 -04001924
Cary Clark154beea2017-10-26 07:58:48 -04001925#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001926
1927# ------------------------------------------------------------------------------
1928
1929#Method void preScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05001930#In Set
1931#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05001932#Line # pre-multiplies Matrix by scale ##
Cary Clark154beea2017-10-26 07:58:48 -04001933Sets Matrix to Matrix multiplied by Matrix constructed from scaling by (sx, sy)
1934about pivot point (px, py).
1935This can be thought of as scaling about a pivot point before applying Matrix.
1936
1937Given:
1938
1939#Code
1940#Literal
1941 | A B C | | sx 0 dx |
1942Matrix = | D E F |, S(sx, sy, px, py) = | 0 sy dy |
1943 | G H I | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04001944##
1945
Cary Clark682c58d2018-05-16 07:07:07 -04001946where
Cary Clark154beea2017-10-26 07:58:48 -04001947
1948#Code
1949#Literal
1950dx = px - sx * px
1951dy = py - sy * py
1952##
1953
1954sets Matrix to:
1955
1956#Code
1957#Literal
1958 | A B C | | sx 0 dx | | A*sx B*sy A*dx+B*dy+C |
1959Matrix * S(sx, sy, px, py) = | D E F | | 0 sy dy | = | D*sx E*sy D*dx+E*dy+F |
1960 | G H I | | 0 0 1 | | G*sx H*sy G*dx+H*dy+I |
1961##
1962
1963#Param sx horizontal scale factor ##
1964#Param sy vertical scale factor ##
1965#Param px pivot x ##
1966#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001967
1968#Example
Cary Clark154beea2017-10-26 07:58:48 -04001969#Image 3
1970SkMatrix matrix;
1971SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
1972SkRect::Make(source.bounds()).toQuad(bitmapBounds);
1973matrix.setPolyToPoly(bitmapBounds, perspect, 4);
1974matrix.preScale(.75f, 1.5f, source.width() / 2, source.height() / 2);
1975canvas->concat(matrix);
1976canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04001977##
1978
Cary Clark154beea2017-10-26 07:58:48 -04001979#SeeAlso postScale setScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04001980
Cary Clark154beea2017-10-26 07:58:48 -04001981#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04001982
1983# ------------------------------------------------------------------------------
1984
1985#Method void preScale(SkScalar sx, SkScalar sy)
Cary Clark4855f782018-02-06 09:41:53 -05001986#In Set
1987#In Operator
Cary Clark154beea2017-10-26 07:58:48 -04001988Sets Matrix to Matrix multiplied by Matrix constructed from scaling by (sx, sy)
1989about pivot point (0, 0).
1990This can be thought of as scaling about the origin before applying Matrix.
1991
1992Given:
1993
1994#Code
1995#Literal
1996 | A B C | | sx 0 0 |
1997Matrix = | D E F |, S(sx, sy) = | 0 sy 0 |
1998 | G H I | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04001999##
2000
Cary Clark154beea2017-10-26 07:58:48 -04002001sets Matrix to:
2002
2003#Code
2004#Literal
2005 | A B C | | sx 0 0 | | A*sx B*sy C |
2006Matrix * S(sx, sy) = | D E F | | 0 sy 0 | = | D*sx E*sy F |
2007 | G H I | | 0 0 1 | | G*sx H*sy I |
2008##
2009
2010#Param sx horizontal scale factor ##
2011#Param sy vertical scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002012
2013#Example
Cary Clark154beea2017-10-26 07:58:48 -04002014#Image 3
2015SkMatrix matrix;
2016SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2017SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2018matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2019matrix.preScale(.75f, 1.5f);
2020canvas->concat(matrix);
2021canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002022##
2023
Cary Clark154beea2017-10-26 07:58:48 -04002024#SeeAlso postScale setScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04002025
Cary Clark154beea2017-10-26 07:58:48 -04002026#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002027
2028# ------------------------------------------------------------------------------
2029
2030#Method void preRotate(SkScalar degrees, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05002031#In Set
2032#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002033#Line # pre-multiplies Matrix by rotation ##
Cary Clark154beea2017-10-26 07:58:48 -04002034Sets Matrix to Matrix multiplied by Matrix constructed from rotating by degrees
2035about pivot point (px, py).
2036This can be thought of as rotating about a pivot point before applying Matrix.
2037
2038Positive degrees rotates clockwise.
2039
2040Given:
2041
2042#Code
2043#Literal
2044 | A B C | | c -s dx |
2045Matrix = | D E F |, R(degrees, px, py) = | s c dy |
2046 | G H I | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002047##
2048
Cary Clark682c58d2018-05-16 07:07:07 -04002049where
Cary Clark154beea2017-10-26 07:58:48 -04002050
2051#Code
2052#Literal
2053c = cos(degrees)
2054s = sin(degrees)
2055dx = s * py + (1 - c) * px
2056dy = -s * px + (1 - c) * py
2057##
2058
2059sets Matrix to:
2060
2061#Code
2062#Literal
2063 | A B C | | c -s dx | | Ac+Bs -As+Bc A*dx+B*dy+C |
2064Matrix * R(degrees, px, py) = | D E F | | s c dy | = | Dc+Es -Ds+Ec D*dx+E*dy+F |
2065 | G H I | | 0 0 1 | | Gc+Hs -Gs+Hc G*dx+H*dy+I |
2066##
2067
2068#Param degrees angle of axes relative to upright axes ##
2069#Param px pivot x ##
2070#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002071
2072#Example
Cary Clark154beea2017-10-26 07:58:48 -04002073#Image 3
2074SkMatrix matrix;
2075SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2076SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2077matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2078matrix.preRotate(45, source.width() / 2, source.height() / 2);
2079canvas->concat(matrix);
2080canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002081##
2082
Cary Clark154beea2017-10-26 07:58:48 -04002083#SeeAlso postRotate setRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002084
Cary Clark154beea2017-10-26 07:58:48 -04002085#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002086
2087# ------------------------------------------------------------------------------
2088
2089#Method void preRotate(SkScalar degrees)
2090
Cary Clark154beea2017-10-26 07:58:48 -04002091Sets Matrix to Matrix multiplied by Matrix constructed from rotating by degrees
2092about pivot point (0, 0).
2093This can be thought of as rotating about the origin before applying Matrix.
2094
2095Positive degrees rotates clockwise.
2096
2097Given:
2098
2099#Code
2100#Literal
2101 | A B C | | c -s 0 |
2102Matrix = | D E F |, R(degrees, px, py) = | s c 0 |
2103 | G H I | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002104##
2105
Cary Clark682c58d2018-05-16 07:07:07 -04002106where
Cary Clark154beea2017-10-26 07:58:48 -04002107
2108#Code
2109#Literal
2110c = cos(degrees)
2111s = sin(degrees)
2112##
2113
2114sets Matrix to:
2115
2116#Code
2117#Literal
2118 | A B C | | c -s 0 | | Ac+Bs -As+Bc C |
2119Matrix * R(degrees, px, py) = | D E F | | s c 0 | = | Dc+Es -Ds+Ec F |
2120 | G H I | | 0 0 1 | | Gc+Hs -Gs+Hc I |
2121##
2122
2123#Param degrees angle of axes relative to upright axes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002124
2125#Example
Cary Clark154beea2017-10-26 07:58:48 -04002126#Image 3
2127SkMatrix matrix;
2128SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2129SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2130matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2131matrix.preRotate(45);
2132canvas->concat(matrix);
2133canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002134##
2135
Cary Clark154beea2017-10-26 07:58:48 -04002136#SeeAlso postRotate setRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002137
Cary Clark154beea2017-10-26 07:58:48 -04002138#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002139
2140# ------------------------------------------------------------------------------
2141
2142#Method void preSkew(SkScalar kx, SkScalar ky, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05002143#In Set
2144#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002145#Line # pre-multiplies Matrix by skew ##
Cary Clark154beea2017-10-26 07:58:48 -04002146Sets Matrix to Matrix multiplied by Matrix constructed from skewing by (kx, ky)
2147about pivot point (px, py).
2148This can be thought of as skewing about a pivot point before applying Matrix.
2149
2150Given:
2151
2152#Code
2153#Literal
2154 | A B C | | 1 kx dx |
2155Matrix = | D E F |, K(kx, ky, px, py) = | ky 1 dy |
2156 | G H I | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002157##
2158
Cary Clark682c58d2018-05-16 07:07:07 -04002159where
Cary Clark154beea2017-10-26 07:58:48 -04002160
2161#Code
2162#Literal
2163dx = -kx * py
2164dy = -ky * px
2165##
2166
2167sets Matrix to:
2168
2169#Code
2170#Literal
2171 | A B C | | 1 kx dx | | A+B*ky A*kx+B A*dx+B*dy+C |
2172Matrix * K(kx, ky, px, py) = | D E F | | ky 1 dy | = | D+E*ky D*kx+E D*dx+E*dy+F |
2173 | G H I | | 0 0 1 | | G+H*ky G*kx+H G*dx+H*dy+I |
2174##
2175
2176#Param kx horizontal skew factor ##
2177#Param ky vertical skew factor ##
2178#Param px pivot x ##
2179#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002180
2181#Example
Cary Clark154beea2017-10-26 07:58:48 -04002182#Image 3
2183SkMatrix matrix;
2184SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2185SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2186matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2187matrix.preSkew(.5f, 0, source.width() / 2, source.height() / 2);
2188canvas->concat(matrix);
2189canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002190##
2191
Cary Clark154beea2017-10-26 07:58:48 -04002192#SeeAlso postSkew setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04002193
Cary Clark154beea2017-10-26 07:58:48 -04002194#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002195
2196# ------------------------------------------------------------------------------
2197
2198#Method void preSkew(SkScalar kx, SkScalar ky)
2199
Cary Clark154beea2017-10-26 07:58:48 -04002200Sets Matrix to Matrix multiplied by Matrix constructed from skewing by (kx, ky)
2201about pivot point (0, 0).
2202This can be thought of as skewing about the origin before applying Matrix.
2203
2204Given:
2205
2206#Code
2207#Literal
2208 | A B C | | 1 kx 0 |
2209Matrix = | D E F |, K(kx, ky) = | ky 1 0 |
2210 | G H I | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002211##
2212
Cary Clark154beea2017-10-26 07:58:48 -04002213sets Matrix to:
2214
2215#Code
2216#Literal
2217 | A B C | | 1 kx 0 | | A+B*ky A*kx+B C |
2218Matrix * K(kx, ky) = | D E F | | ky 1 0 | = | D+E*ky D*kx+E F |
2219 | G H I | | 0 0 1 | | G+H*ky G*kx+H I |
2220##
2221
2222#Param kx horizontal skew factor ##
2223#Param ky vertical skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002224
2225#Example
Cary Clark154beea2017-10-26 07:58:48 -04002226#Image 3
2227SkMatrix matrix;
2228SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2229SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2230matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2231matrix.preSkew(.5f, 0);
2232canvas->concat(matrix);
2233canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002234##
2235
Cary Clark154beea2017-10-26 07:58:48 -04002236#SeeAlso postSkew setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04002237
Cary Clark154beea2017-10-26 07:58:48 -04002238#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002239
2240# ------------------------------------------------------------------------------
2241
2242#Method void preConcat(const SkMatrix& other)
Cary Clark4855f782018-02-06 09:41:53 -05002243#In Set
2244#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002245#Line # pre-multiplies Matrix by Matrix parameter ##
Cary Clark154beea2017-10-26 07:58:48 -04002246Sets Matrix to Matrix multiplied by Matrix other.
2247This can be thought of mapping by other before applying Matrix.
2248
2249Given:
2250
2251#Code
2252#Literal
2253 | A B C | | J K L |
2254Matrix = | D E F |, other = | M N O |
2255 | G H I | | P Q R |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002256##
2257
Cary Clark154beea2017-10-26 07:58:48 -04002258sets Matrix to:
2259
2260#Code
2261#Literal
2262 | A B C | | J K L | | AJ+BM+CP AK+BN+CQ AL+BO+CR |
2263Matrix * other = | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR |
2264 | G H I | | P Q R | | GJ+HM+IP GK+HN+IQ GL+HO+IR |
2265##
2266
2267#Param other Matrix on right side of multiply expression ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002268
2269#Example
Cary Clark154beea2017-10-26 07:58:48 -04002270#Image 3
2271#Description
2272setPolyToPoly creates perspective matrices, one the inverse of the other.
2273Multiplying the matrix by its inverse turns into an identity matrix.
2274##
2275SkMatrix matrix, matrix2;
2276SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2277SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2278matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2279matrix2.setPolyToPoly(perspect, bitmapBounds, 4);
2280matrix.preConcat(matrix2);
2281canvas->concat(matrix);
2282canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002283##
2284
Cary Clark154beea2017-10-26 07:58:48 -04002285#SeeAlso postConcat setConcat Concat
Cary Clarkbc5697d2017-10-04 14:31:33 -04002286
Cary Clark154beea2017-10-26 07:58:48 -04002287#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002288
2289# ------------------------------------------------------------------------------
2290
2291#Method void postTranslate(SkScalar dx, SkScalar dy)
Cary Clark4855f782018-02-06 09:41:53 -05002292#In Set
2293#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002294#Line # post-multiplies Matrix by translation ##
Cary Clark154beea2017-10-26 07:58:48 -04002295Sets Matrix to Matrix constructed from translation (dx, dy) multiplied by Matrix.
2296This can be thought of as moving the point to be mapped after applying Matrix.
2297
2298Given:
2299
2300#Code
2301#Literal
2302 | J K L | | 1 0 dx |
2303Matrix = | M N O |, T(dx, dy) = | 0 1 dy |
2304 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002305##
2306
Cary Clark154beea2017-10-26 07:58:48 -04002307sets Matrix to:
2308
2309#Code
2310#Literal
2311 | 1 0 dx | | J K L | | J+dx*P K+dx*Q L+dx*R |
2312T(dx, dy) * Matrix = | 0 1 dy | | M N O | = | M+dy*P N+dy*Q O+dy*R |
2313 | 0 0 1 | | P Q R | | P Q R |
2314##
2315
Cary Clark5538c132018-06-14 12:28:14 -04002316#Param dx x-axis translation after applying Matrix ##
2317#Param dy y-axis translation after applying Matrix ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002318
2319#Example
Cary Clark154beea2017-10-26 07:58:48 -04002320#Height 160
2321#Description
2322Compare with preTranslate example.
2323##
2324 SkPaint paint;
2325 paint.setAntiAlias(true);
2326 SkRect rect = {20, 20, 100, 100};
2327 for (int i = 0; i < 2; ++i ) {
2328 SkMatrix matrix;
2329 i == 0 ? matrix.reset(): matrix.setRotate(25, rect.centerX(), 320);
Cary Clark682c58d2018-05-16 07:07:07 -04002330 {
Cary Clark154beea2017-10-26 07:58:48 -04002331 SkAutoCanvasRestore acr(canvas, true);
2332 canvas->concat(matrix);
2333 paint.setColor(SK_ColorGRAY);
2334 canvas->drawRect(rect, paint);
2335 }
2336 paint.setColor(SK_ColorRED);
2337 for (int j = 0; j < 2; ++j ) {
2338 SkAutoCanvasRestore acr(canvas, true);
2339 matrix.postTranslate(40, 40);
2340 canvas->concat(matrix);
2341 canvas->drawCircle(0, 0, 3, paint);
2342 }
2343 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002344##
2345
Cary Clark682c58d2018-05-16 07:07:07 -04002346#SeeAlso preTranslate setTranslate MakeTrans
Cary Clarkbc5697d2017-10-04 14:31:33 -04002347
Cary Clark154beea2017-10-26 07:58:48 -04002348#Method ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002349
2350# ------------------------------------------------------------------------------
2351
2352#Method void postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05002353#In Set
2354#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002355#Line # post-multiplies Matrix by scale ##
Cary Clark154beea2017-10-26 07:58:48 -04002356Sets Matrix to Matrix constructed from scaling by (sx, sy) about pivot point
2357(px, py), multiplied by Matrix.
2358This can be thought of as scaling about a pivot point after applying Matrix.
2359
2360Given:
2361
2362#Code
2363#Literal
2364 | J K L | | sx 0 dx |
2365Matrix = | M N O |, S(sx, sy, px, py) = | 0 sy dy |
2366 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002367##
2368
Cary Clark682c58d2018-05-16 07:07:07 -04002369where
Cary Clark154beea2017-10-26 07:58:48 -04002370
2371#Code
2372#Literal
2373dx = px - sx * px
2374dy = py - sy * py
2375##
2376
2377sets Matrix to:
2378
2379#Code
2380#Literal
2381 | sx 0 dx | | J K L | | sx*J+dx*P sx*K+dx*Q sx*L+dx+R |
2382S(sx, sy, px, py) * Matrix = | 0 sy dy | | M N O | = | sy*M+dy*P sy*N+dy*Q sy*O+dy*R |
2383 | 0 0 1 | | P Q R | | P Q R |
2384##
2385
2386#Param sx horizontal scale factor ##
2387#Param sy vertical scale factor ##
2388#Param px pivot x ##
2389#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002390
2391#Example
Cary Clark154beea2017-10-26 07:58:48 -04002392#Image 3
2393SkMatrix matrix;
2394SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2395SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2396matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2397matrix.postScale(.75f, 1.5f, source.width() / 2, source.height() / 2);
2398canvas->concat(matrix);
2399canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002400##
2401
Cary Clark154beea2017-10-26 07:58:48 -04002402#SeeAlso preScale setScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04002403
2404##
2405
2406# ------------------------------------------------------------------------------
2407
2408#Method void postScale(SkScalar sx, SkScalar sy)
2409
Cary Clark154beea2017-10-26 07:58:48 -04002410Sets Matrix to Matrix constructed from scaling by (sx, sy) about pivot point
2411(0, 0), multiplied by Matrix.
2412This can be thought of as scaling about the origin after applying Matrix.
2413
2414Given:
2415
2416#Code
2417#Literal
2418 | J K L | | sx 0 0 |
2419Matrix = | M N O |, S(sx, sy) = | 0 sy 0 |
2420 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002421##
2422
Cary Clark154beea2017-10-26 07:58:48 -04002423sets Matrix to:
2424
2425#Code
2426#Literal
2427 | sx 0 0 | | J K L | | sx*J sx*K sx*L |
2428S(sx, sy) * Matrix = | 0 sy 0 | | M N O | = | sy*M sy*N sy*O |
2429 | 0 0 1 | | P Q R | | P Q R |
2430##
2431
2432#Param sx horizontal scale factor ##
2433#Param sy vertical scale factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002434
2435#Example
Cary Clark154beea2017-10-26 07:58:48 -04002436#Image 3
2437SkMatrix matrix;
2438SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2439SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2440matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2441matrix.postScale(.75f, 1.5f);
2442canvas->concat(matrix);
2443canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002444##
2445
Cary Clark154beea2017-10-26 07:58:48 -04002446#SeeAlso preScale setScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04002447
2448##
2449
2450# ------------------------------------------------------------------------------
2451
2452#Method bool postIDiv(int divx, int divy)
Cary Clark4855f782018-02-06 09:41:53 -05002453#In Set
2454#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002455#Line # post-multiplies Matrix by inverse scale ##
Cary Clark154beea2017-10-26 07:58:48 -04002456Sets Matrix to Matrix constructed from scaling by
Cary Clarkbc5697d2017-10-04 14:31:33 -04002457#Formula
Cary Clark154beea2017-10-26 07:58:48 -04002458(1/divx, 1/divy)
2459##
2460about pivot point (px, py), multiplied by Matrix.
2461
2462Returns false if either divx or divy is zero.
2463
2464Given:
2465
2466#Code
2467#Literal
2468 | J K L | | sx 0 0 |
2469Matrix = | M N O |, I(divx, divy) = | 0 sy 0 |
2470 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002471##
2472
Cary Clark682c58d2018-05-16 07:07:07 -04002473where
Cary Clarkbc5697d2017-10-04 14:31:33 -04002474
Cary Clark154beea2017-10-26 07:58:48 -04002475#Code
2476#Literal
2477sx = 1 / divx
2478sy = 1 / divy
2479##
2480
2481sets Matrix to:
2482
2483#Code
2484#Literal
2485 | sx 0 0 | | J K L | | sx*J sx*K sx*L |
2486I(divx, divy) * Matrix = | 0 sy 0 | | M N O | = | sy*M sy*N sy*O |
2487 | 0 0 1 | | P Q R | | P Q R |
2488##
2489
2490#Param divx integer divisor for inverse scale in x ##
2491#Param divy integer divisor for inverse scale in y ##
2492
2493#Return true on successful scale ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002494
2495#Example
Cary Clark154beea2017-10-26 07:58:48 -04002496#Image 3
Cary Clark681287e2018-03-16 11:34:15 -04002497SkMatrix matrix;
Cary Clark154beea2017-10-26 07:58:48 -04002498SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2499SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2500matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2501matrix.postIDiv(1, 2);
2502canvas->concat(matrix);
2503canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002504##
2505
Cary Clark154beea2017-10-26 07:58:48 -04002506#SeeAlso postScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04002507
2508##
2509
2510# ------------------------------------------------------------------------------
2511
2512#Method void postRotate(SkScalar degrees, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05002513#In Set
2514#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002515#Line # post-multiplies Matrix by rotation ##
Cary Clark154beea2017-10-26 07:58:48 -04002516Sets Matrix to Matrix constructed from rotating by degrees about pivot point
2517(px, py), multiplied by Matrix.
2518This can be thought of as rotating about a pivot point after applying Matrix.
2519
2520Positive degrees rotates clockwise.
2521
2522Given:
2523
2524#Code
2525#Literal
2526 | J K L | | c -s dx |
2527Matrix = | M N O |, R(degrees, px, py) = | s c dy |
2528 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002529##
2530
Cary Clark682c58d2018-05-16 07:07:07 -04002531where
Cary Clark154beea2017-10-26 07:58:48 -04002532
2533#Code
2534#Literal
2535c = cos(degrees)
2536s = sin(degrees)
2537dx = s * py + (1 - c) * px
2538dy = -s * px + (1 - c) * py
2539##
2540
2541sets Matrix to:
2542
2543#Code
2544#Literal
2545 |c -s dx| |J K L| |cJ-sM+dx*P cK-sN+dx*Q cL-sO+dx+R|
2546R(degrees, px, py) * Matrix = |s c dy| |M N O| = |sJ+cM+dy*P sK+cN+dy*Q sL+cO+dy*R|
2547 |0 0 1| |P Q R| | P Q R|
2548##
2549
2550#Param degrees angle of axes relative to upright axes ##
2551#Param px pivot x ##
2552#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002553
2554#Example
Cary Clark154beea2017-10-26 07:58:48 -04002555#Image 3
2556SkMatrix matrix;
2557SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2558SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2559matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2560matrix.postRotate(45, source.width() / 2, source.height() / 2);
2561canvas->concat(matrix);
2562canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002563##
2564
Cary Clark154beea2017-10-26 07:58:48 -04002565#SeeAlso preRotate setRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002566
2567##
2568
2569# ------------------------------------------------------------------------------
2570
2571#Method void postRotate(SkScalar degrees)
2572
Cary Clark154beea2017-10-26 07:58:48 -04002573Sets Matrix to Matrix constructed from rotating by degrees about pivot point
2574(0, 0), multiplied by Matrix.
2575This can be thought of as rotating about the origin after applying Matrix.
2576
2577Positive degrees rotates clockwise.
2578
2579Given:
2580
2581#Code
2582#Literal
2583 | J K L | | c -s 0 |
2584Matrix = | M N O |, R(degrees, px, py) = | s c 0 |
2585 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002586##
2587
Cary Clark682c58d2018-05-16 07:07:07 -04002588where
Cary Clark154beea2017-10-26 07:58:48 -04002589
2590#Code
2591#Literal
2592c = cos(degrees)
2593s = sin(degrees)
2594##
2595
2596sets Matrix to:
2597
2598#Code
2599#Literal
2600 | c -s dx | | J K L | | cJ-sM cK-sN cL-sO |
2601R(degrees, px, py) * Matrix = | s c dy | | M N O | = | sJ+cM sK+cN sL+cO |
2602 | 0 0 1 | | P Q R | | P Q R |
2603##
2604
2605#Param degrees angle of axes relative to upright axes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002606
2607#Example
Cary Clark154beea2017-10-26 07:58:48 -04002608#Image 3
2609SkMatrix matrix;
2610SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2611SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2612matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2613matrix.postRotate(45);
2614canvas->concat(matrix);
2615canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002616##
2617
Cary Clark154beea2017-10-26 07:58:48 -04002618#SeeAlso preRotate setRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002619
2620##
2621
2622# ------------------------------------------------------------------------------
2623
2624#Method void postSkew(SkScalar kx, SkScalar ky, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05002625#In Set
2626#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002627#Line # post-multiplies Matrix by skew ##
Cary Clark154beea2017-10-26 07:58:48 -04002628Sets Matrix to Matrix constructed from skewing by (kx, ky) about pivot point
2629(px, py), multiplied by Matrix.
2630This can be thought of as skewing about a pivot point after applying Matrix.
2631
2632Given:
2633
2634#Code
2635#Literal
2636 | J K L | | 1 kx dx |
2637Matrix = | M N O |, K(kx, ky, px, py) = | ky 1 dy |
2638 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002639##
2640
Cary Clark682c58d2018-05-16 07:07:07 -04002641where
Cary Clark154beea2017-10-26 07:58:48 -04002642
2643#Code
2644#Literal
2645dx = -kx * py
2646dy = -ky * px
2647##
2648
2649sets Matrix to:
2650
2651#Code
2652#Literal
2653 | 1 kx dx| |J K L| |J+kx*M+dx*P K+kx*N+dx*Q L+kx*O+dx+R|
2654K(kx, ky, px, py) * Matrix = |ky 1 dy| |M N O| = |ky*J+M+dy*P ky*K+N+dy*Q ky*L+O+dy*R|
2655 | 0 0 1| |P Q R| | P Q R|
2656##
2657
2658#Param kx horizontal skew factor ##
2659#Param ky vertical skew factor ##
2660#Param px pivot x ##
2661#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002662
2663#Example
Cary Clark154beea2017-10-26 07:58:48 -04002664#Image 3
2665SkMatrix matrix;
2666SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2667SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2668matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2669matrix.postSkew(.5f, 0, source.width() / 2, source.height() / 2);
2670canvas->concat(matrix);
2671canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002672##
2673
Cary Clark154beea2017-10-26 07:58:48 -04002674#SeeAlso preSkew setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04002675
2676##
2677
2678# ------------------------------------------------------------------------------
2679
2680#Method void postSkew(SkScalar kx, SkScalar ky)
2681
Cary Clark154beea2017-10-26 07:58:48 -04002682Sets Matrix to Matrix constructed from skewing by (kx, ky) about pivot point
2683(0, 0), multiplied by Matrix.
2684This can be thought of as skewing about the origin after applying Matrix.
2685
2686Given:
2687
2688#Code
2689#Literal
2690 | J K L | | 1 kx 0 |
2691Matrix = | M N O |, K(kx, ky) = | ky 1 0 |
2692 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002693##
2694
Cary Clark154beea2017-10-26 07:58:48 -04002695sets Matrix to:
2696
2697#Code
2698#Literal
2699 | 1 kx 0 | | J K L | | J+kx*M K+kx*N L+kx*O |
2700K(kx, ky) * Matrix = | ky 1 0 | | M N O | = | ky*J+M ky*K+N ky*L+O |
2701 | 0 0 1 | | P Q R | | P Q R |
2702##
2703
2704#Param kx horizontal skew factor ##
2705#Param ky vertical skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002706
2707#Example
Cary Clark154beea2017-10-26 07:58:48 -04002708#Image 3
2709SkMatrix matrix;
2710SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2711SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2712matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2713matrix.postSkew(.5f, 0);
2714canvas->concat(matrix);
2715canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002716##
2717
Cary Clark154beea2017-10-26 07:58:48 -04002718#SeeAlso preSkew setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04002719
2720##
2721
2722# ------------------------------------------------------------------------------
2723
2724#Method void postConcat(const SkMatrix& other)
Cary Clark4855f782018-02-06 09:41:53 -05002725#In Set
2726#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002727#Line # post-multiplies Matrix by Matrix parameter ##
Cary Clark154beea2017-10-26 07:58:48 -04002728Sets Matrix to Matrix other multiplied by Matrix.
2729This can be thought of mapping by other after applying Matrix.
2730
2731Given:
2732
2733#Code
2734#Literal
2735 | J K L | | A B C |
2736Matrix = | M N O |, other = | D E F |
2737 | P Q R | | G H I |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002738##
2739
Cary Clark154beea2017-10-26 07:58:48 -04002740sets Matrix to:
2741
2742#Code
2743#Literal
2744 | A B C | | J K L | | AJ+BM+CP AK+BN+CQ AL+BO+CR |
2745other * Matrix = | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR |
2746 | G H I | | P Q R | | GJ+HM+IP GK+HN+IQ GL+HO+IR |
2747##
2748
2749#Param other Matrix on left side of multiply expression ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002750
2751#Example
Cary Clark154beea2017-10-26 07:58:48 -04002752#Image 3
2753#Height 64
Cary Clark681287e2018-03-16 11:34:15 -04002754SkMatrix matrix;
Cary Clark154beea2017-10-26 07:58:48 -04002755SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2756SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2757matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2758matrix.postConcat(matrix);
2759canvas->concat(matrix);
2760canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002761##
2762
Cary Clark154beea2017-10-26 07:58:48 -04002763#SeeAlso preConcat setConcat Concat
Cary Clarkbc5697d2017-10-04 14:31:33 -04002764
2765##
2766
2767# ------------------------------------------------------------------------------
2768
2769#Enum ScaleToFit
Cary Clark682c58d2018-05-16 07:07:07 -04002770#Line # options to map Rects ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002771#Code
2772 enum ScaleToFit {
2773 kFill_ScaleToFit,
2774 kStart_ScaleToFit,
2775 kCenter_ScaleToFit,
2776 kEnd_ScaleToFit,
2777 };
2778##
2779
Cary Clark154beea2017-10-26 07:58:48 -04002780ScaleToFit describes how Matrix is constructed to map one Rect to another.
2781ScaleToFit may allow Matrix to have unequal horizontal and vertical scaling,
2782or may restrict Matrix to square scaling. If restricted, ScaleToFit specifies
2783how Matrix maps to the side or center of the destination Rect.
2784
2785#Const kFill_ScaleToFit 0
Cary Clark682c58d2018-05-16 07:07:07 -04002786#Line # scales in x and y to fill destination Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04002787 Computes Matrix that scales in x and y independently, so that source Rect is
2788 mapped to completely fill destination Rect. The aspect ratio of source Rect
2789 may change.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002790##
Cary Clark154beea2017-10-26 07:58:48 -04002791#Const kStart_ScaleToFit 1
Cary Clark682c58d2018-05-16 07:07:07 -04002792#Line # scales and aligns to left and top ##
Cary Clark154beea2017-10-26 07:58:48 -04002793 Computes Matrix that maintains source Rect aspect ratio, mapping source Rect
2794 width or height to destination Rect. Aligns mapping to left and top edges
2795 of destination Rect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002796##
Cary Clark154beea2017-10-26 07:58:48 -04002797#Const kCenter_ScaleToFit 2
Cary Clark682c58d2018-05-16 07:07:07 -04002798#Line # scales and aligns to center ##
Cary Clark154beea2017-10-26 07:58:48 -04002799 Computes Matrix that maintains source Rect aspect ratio, mapping source Rect
2800 width or height to destination Rect. Aligns mapping to center of destination
2801 Rect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002802##
Cary Clark154beea2017-10-26 07:58:48 -04002803#Const kEnd_ScaleToFit 3
Cary Clark682c58d2018-05-16 07:07:07 -04002804#Line # scales and aligns to right and bottom ##
Cary Clark154beea2017-10-26 07:58:48 -04002805 Computes Matrix that maintains source Rect aspect ratio, mapping source Rect
2806 width or height to destination Rect. Aligns mapping to right and bottom
2807 edges of destination Rect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002808##
2809
2810#Example
Cary Clark154beea2017-10-26 07:58:48 -04002811 const char* labels[] = { "Fill", "Start", "Center", "End" };
2812 SkRect rects[] = {{5, 5, 59, 59}, {5, 74, 59, 108}, {10, 123, 44, 172}, {10, 187, 54, 231}};
2813 SkRect bounds;
2814 source.getBounds(&bounds);
2815 SkPaint paint;
2816 paint.setAntiAlias(true);
2817 for (auto fit : { SkMatrix::kFill_ScaleToFit, SkMatrix::kStart_ScaleToFit,
2818 SkMatrix::kCenter_ScaleToFit, SkMatrix::kEnd_ScaleToFit } ) {
2819 for (auto rect : rects ) {
2820 canvas->drawRect(rect, paint);
2821 SkMatrix matrix;
2822 if (!matrix.setRectToRect(bounds, rect, fit)) {
2823 continue;
2824 }
2825 SkAutoCanvasRestore acr(canvas, true);
2826 canvas->concat(matrix);
2827 canvas->drawBitmap(source, 0, 0);
2828 }
2829 canvas->drawString(labels[fit], 10, 255, paint);
2830 canvas->translate(64, 0);
2831 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002832##
2833
Cary Clark154beea2017-10-26 07:58:48 -04002834#SeeAlso setRectToRect MakeRectToRect setPolyToPoly
Cary Clarkbc5697d2017-10-04 14:31:33 -04002835
2836##
2837
2838# ------------------------------------------------------------------------------
2839
2840#Method bool setRectToRect(const SkRect& src, const SkRect& dst, ScaleToFit stf)
Cary Clark4855f782018-02-06 09:41:53 -05002841#In Set
Cary Clark08895c42018-02-01 09:37:32 -05002842#Line # sets to map one Rect to another ##
Cary Clark154beea2017-10-26 07:58:48 -04002843Sets Matrix to scale and translate src Rect to dst Rect. stf selects whether
2844mapping completely fills dst or preserves the aspect ratio, and how to align
2845src within dst. Returns false if src is empty, and sets Matrix to identity.
2846Returns true if dst is empty, and sets Matrix to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002847
Cary Clark154beea2017-10-26 07:58:48 -04002848#Code
2849#Literal
2850| 0 0 0 |
2851| 0 0 0 |
2852| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002853##
2854
Cary Clark154beea2017-10-26 07:58:48 -04002855#Param src Rect to map from ##
2856#Param dst Rect to map to ##
2857#Param stf one of: kFill_ScaleToFit, kStart_ScaleToFit,
2858 kCenter_ScaleToFit, kEnd_ScaleToFit
Cary Clarkbc5697d2017-10-04 14:31:33 -04002859##
2860
Cary Clark154beea2017-10-26 07:58:48 -04002861#Return true if Matrix can represent Rect mapping ##
2862
Cary Clarkbc5697d2017-10-04 14:31:33 -04002863#Example
Cary Clark154beea2017-10-26 07:58:48 -04002864 const SkRect srcs[] = { {0, 0, 0, 0}, {1, 2, 3, 4} };
2865 const SkRect dsts[] = { {0, 0, 0, 0}, {5, 6, 8, 9} };
2866 for (auto src : srcs) {
2867 for (auto dst : dsts) {
2868 SkMatrix matrix;
2869 matrix.setAll(-1, -1, -1, -1, -1, -1, -1, -1, -1);
2870 bool success = matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
2871 SkDebugf("src: %g, %g, %g, %g dst: %g, %g, %g, %g success: %s\n",
2872 src.fLeft, src.fTop, src.fRight, src.fBottom,
2873 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, success ? "true" : "false");
2874 matrix.dump();
2875 }
2876 }
2877#StdOut
2878src: 0, 0, 0, 0 dst: 0, 0, 0, 0 success: false
2879[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2880src: 0, 0, 0, 0 dst: 5, 6, 8, 9 success: false
2881[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2882src: 1, 2, 3, 4 dst: 0, 0, 0, 0 success: true
2883[ 0.0000 0.0000 0.0000][ 0.0000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
2884src: 1, 2, 3, 4 dst: 5, 6, 8, 9 success: true
2885[ 1.5000 0.0000 3.5000][ 0.0000 1.5000 3.0000][ 0.0000 0.0000 1.0000]
2886##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002887##
2888
Cary Clark682c58d2018-05-16 07:07:07 -04002889#SeeAlso MakeRectToRect ScaleToFit setPolyToPoly SkRect::isEmpty
Cary Clarkbc5697d2017-10-04 14:31:33 -04002890
2891##
2892
2893# ------------------------------------------------------------------------------
2894
2895#Method static SkMatrix MakeRectToRect(const SkRect& src, const SkRect& dst, ScaleToFit stf)
Cary Clark4855f782018-02-06 09:41:53 -05002896#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05002897#Line # constructs from source Rect to destination Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04002898Returns Matrix set to scale and translate src Rect to dst Rect. stf selects
2899whether mapping completely fills dst or preserves the aspect ratio, and how to
2900align src within dst. Returns the identity Matrix if src is empty. If dst is
2901empty, returns Matrix set to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002902
Cary Clark154beea2017-10-26 07:58:48 -04002903#Code
2904#Literal
2905| 0 0 0 |
2906| 0 0 0 |
2907| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002908##
2909
Cary Clark154beea2017-10-26 07:58:48 -04002910#Param src Rect to map from ##
2911#Param dst Rect to map to ##
2912#Param stf one of: kFill_ScaleToFit, kStart_ScaleToFit,
2913 kCenter_ScaleToFit, kEnd_ScaleToFit
2914##
2915
2916#Return Matrix mapping src to dst ##
2917
2918#Example
2919 const SkRect srcs[] = { {0, 0, 0, 0}, {1, 2, 3, 4} };
2920 const SkRect dsts[] = { {0, 0, 0, 0}, {5, 6, 8, 9} };
2921 for (auto src : srcs) {
2922 for (auto dst : dsts) {
2923 SkMatrix matrix = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
2924 SkDebugf("src: %g, %g, %g, %g dst: %g, %g, %g, %g\n",
2925 src.fLeft, src.fTop, src.fRight, src.fBottom,
2926 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom);
2927 matrix.dump();
2928 }
2929 }
2930#StdOut
2931src: 0, 0, 0, 0 dst: 0, 0, 0, 0
2932[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2933src: 0, 0, 0, 0 dst: 5, 6, 8, 9
2934[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2935src: 1, 2, 3, 4 dst: 0, 0, 0, 0
2936[ 0.0000 0.0000 0.0000][ 0.0000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
2937src: 1, 2, 3, 4 dst: 5, 6, 8, 9
2938[ 1.5000 0.0000 3.5000][ 0.0000 1.5000 3.0000][ 0.0000 0.0000 1.0000]
2939##
2940##
2941
2942#SeeAlso setRectToRect ScaleToFit setPolyToPoly SkRect::isEmpty
Cary Clarkbc5697d2017-10-04 14:31:33 -04002943
2944##
2945
2946# ------------------------------------------------------------------------------
2947
2948#Method bool setPolyToPoly(const SkPoint src[], const SkPoint dst[], int count)
Cary Clark4855f782018-02-06 09:41:53 -05002949#In Set
Cary Clark08895c42018-02-01 09:37:32 -05002950#Line # sets to map one to four points to an equal array of points ##
Cary Clark154beea2017-10-26 07:58:48 -04002951Sets Matrix to map src to dst. count must be zero or greater, and four or less.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002952
Cary Clark154beea2017-10-26 07:58:48 -04002953If count is zero, sets Matrix to identity and returns true.
2954If count is one, sets Matrix to translate and returns true.
2955If count is two or more, sets Matrix to map Points if possible; returns false
2956if Matrix cannot be constructed. If count is four, Matrix may include
Cary Clark682c58d2018-05-16 07:07:07 -04002957perspective.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002958
Cary Clark154beea2017-10-26 07:58:48 -04002959#Param src Points to map from ##
2960#Param dst Points to map to ##
2961#Param count number of Points in src and dst ##
2962
2963#Return true if Matrix was constructed successfully
Cary Clarkbc5697d2017-10-04 14:31:33 -04002964##
2965
2966#Example
Cary Clark154beea2017-10-26 07:58:48 -04002967 const SkPoint src[] = { { 0, 0}, {30, 0}, {30, -30}, { 0, -30} };
2968 const SkPoint dst[] = { {50, 0}, {80, -10}, {90, -30}, {60, -40} };
2969 SkPaint blackPaint;
2970 blackPaint.setAntiAlias(true);
2971 blackPaint.setTextSize(42);
2972 SkPaint redPaint = blackPaint;
2973 redPaint.setColor(SK_ColorRED);
2974 for (int count : { 1, 2, 3, 4 } ) {
2975 canvas->translate(35, 55);
2976 for (int index = 0; index < count; ++index) {
2977 canvas->drawCircle(src[index], 3, blackPaint);
2978 canvas->drawCircle(dst[index], 3, blackPaint);
2979 if (index > 0) {
2980 canvas->drawLine(src[index], src[index - 1], blackPaint);
2981 canvas->drawLine(dst[index], dst[index - 1], blackPaint);
2982 }
2983 }
2984 SkMatrix matrix;
2985 matrix.setPolyToPoly(src, dst, count);
2986 canvas->drawString("A", src[0].fX, src[0].fY, redPaint);
2987 SkAutoCanvasRestore acr(canvas, true);
2988 canvas->concat(matrix);
2989 canvas->drawString("A", src[0].fX, src[0].fY, redPaint);
2990 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002991##
2992
Cary Clark154beea2017-10-26 07:58:48 -04002993#SeeAlso setRectToRect MakeRectToRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04002994
2995##
2996
2997# ------------------------------------------------------------------------------
2998
2999#Method bool SK_WARN_UNUSED_RESULT invert(SkMatrix* inverse) const
Cary Clark4855f782018-02-06 09:41:53 -05003000#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05003001#Line # returns inverse, if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04003002Sets inverse to reciprocal matrix, returning true if Matrix can be inverted.
3003Geometrically, if Matrix maps from source to destination, inverse Matrix
3004maps from destination to source. If Matrix can not be inverted, inverse is
3005unchanged.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003006
Cary Clark154beea2017-10-26 07:58:48 -04003007#Param inverse storage for inverted Matrix; may be nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003008
Cary Clark154beea2017-10-26 07:58:48 -04003009#Return true if Matrix can be inverted ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003010
3011#Example
Cary Clark154beea2017-10-26 07:58:48 -04003012#Height 128
3013 const SkPoint src[] = { { 10, 120}, {120, 120}, {120, 10}, { 10, 10} };
3014 const SkPoint dst[] = { {150, 120}, {200, 100}, {240, 30}, { 130, 40} };
3015 SkPaint paint;
3016 paint.setAntiAlias(true);
3017 SkMatrix matrix;
3018 matrix.setPolyToPoly(src, dst, 4);
3019 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, src, paint);
3020 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, dst, paint);
3021 paint.setColor(SK_ColorBLUE);
3022 paint.setStrokeWidth(3);
3023 paint.setStrokeCap(SkPaint::kRound_Cap);
3024 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, dst, paint);
Cary Clark681287e2018-03-16 11:34:15 -04003025 if (matrix.invert(&matrix)) {
3026 canvas->concat(matrix);
3027 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, dst, paint);
3028 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003029##
3030
Cary Clark154beea2017-10-26 07:58:48 -04003031#SeeAlso Concat
Cary Clarkbc5697d2017-10-04 14:31:33 -04003032
3033##
3034
3035# ------------------------------------------------------------------------------
3036
3037#Method static void SetAffineIdentity(SkScalar affine[6])
Cary Clark4855f782018-02-06 09:41:53 -05003038#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05003039#Line # sets 3x2 array to identity ##
Cary Clark154beea2017-10-26 07:58:48 -04003040Fills affine with identity values in column major order.
3041Sets affine to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003042
Cary Clark154beea2017-10-26 07:58:48 -04003043#Code
3044#Literal
3045| 1 0 0 |
3046| 0 1 0 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003047##
3048
Cary Clark154beea2017-10-26 07:58:48 -04003049Affine 3x2 matrices in column major order are used by OpenGL and XPS.
3050
3051#Param affine storage for 3x2 affine matrix ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003052
3053#Example
Cary Clark154beea2017-10-26 07:58:48 -04003054 SkScalar affine[6];
3055 SkMatrix::SetAffineIdentity(affine);
3056 const char* names[] = { "ScaleX", "SkewY", "SkewX", "ScaleY", "TransX", "TransY" };
3057 for (int i = 0; i < 6; ++i) {
3058 SkDebugf("%s: %g ", names[i], affine[i]);
3059 }
3060 SkDebugf("\n");
3061#StdOut
3062ScaleX: 1 SkewY: 0 SkewX: 0 ScaleY: 1 TransX: 0 TransY: 0
3063##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003064##
3065
Cary Clark154beea2017-10-26 07:58:48 -04003066#SeeAlso setAffine asAffine
Cary Clarkbc5697d2017-10-04 14:31:33 -04003067
3068##
3069
3070# ------------------------------------------------------------------------------
3071
3072#Method bool SK_WARN_UNUSED_RESULT asAffine(SkScalar affine[6]) const
Cary Clark4855f782018-02-06 09:41:53 -05003073#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05003074#Line # copies to 3x2 array ##
Cary Clark154beea2017-10-26 07:58:48 -04003075Fills affine in column major order. Sets affine to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003076
Cary Clark154beea2017-10-26 07:58:48 -04003077#Code
3078#Literal
3079| scale-x skew-x translate-x |
3080| skew-y scale-y translate-y |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003081##
3082
Cary Clark154beea2017-10-26 07:58:48 -04003083If Matrix contains perspective, returns false and leaves affine unchanged.
3084
3085#Param affine storage for 3x2 affine matrix; may be nullptr ##
3086
3087#Return true if Matrix does not contain perspective ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003088
3089#Example
Cary Clark154beea2017-10-26 07:58:48 -04003090SkMatrix matrix;
3091matrix.setAll(2, 3, 4, 5, 6, 7, 0, 0, 1);
3092SkScalar affine[6];
Cary Clark681287e2018-03-16 11:34:15 -04003093if (matrix.asAffine(affine)) {
3094 const char* names[] = { "ScaleX", "SkewY", "SkewX", "ScaleY", "TransX", "TransY" };
3095 for (int i = 0; i < 6; ++i) {
3096 SkDebugf("%s: %g ", names[i], affine[i]);
3097 }
3098 SkDebugf("\n");
Cary Clark154beea2017-10-26 07:58:48 -04003099}
Cary Clark154beea2017-10-26 07:58:48 -04003100#StdOut
Cary Clark682c58d2018-05-16 07:07:07 -04003101ScaleX: 2 SkewY: 5 SkewX: 3 ScaleY: 6 TransX: 4 TransY: 7
Cary Clark154beea2017-10-26 07:58:48 -04003102##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003103##
3104
Cary Clark154beea2017-10-26 07:58:48 -04003105#SeeAlso setAffine SetAffineIdentity
Cary Clarkbc5697d2017-10-04 14:31:33 -04003106
3107##
3108
3109# ------------------------------------------------------------------------------
3110
3111#Method void setAffine(const SkScalar affine[6])
Cary Clark4855f782018-02-06 09:41:53 -05003112#In Constructor
3113#In Set
Cary Clark08895c42018-02-01 09:37:32 -05003114#Line # sets left two columns ##
Cary Clark154beea2017-10-26 07:58:48 -04003115Sets Matrix to affine values, passed in column major order. Given affine,
3116column, then row, as:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003117
Cary Clark154beea2017-10-26 07:58:48 -04003118#Code
3119#Literal
3120| scale-x skew-x translate-x |
3121| skew-y scale-y translate-y |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003122##
3123
Cary Clark154beea2017-10-26 07:58:48 -04003124Matrix is set, row, then column, to:
3125
3126#Code
3127#Literal
3128| scale-x skew-x translate-x |
3129| skew-y scale-y translate-y |
3130| 0 0 1 |
3131##
3132
3133#Param affine 3x2 affine matrix ##
3134
3135#Example
3136SkMatrix matrix;
3137matrix.setAll(2, 3, 4, 5, 6, 7, 0, 0, 1);
3138SkScalar affine[6];
Cary Clark681287e2018-03-16 11:34:15 -04003139if (matrix.asAffine(affine)) {
3140 const char* names[] = { "ScaleX", "SkewY", "SkewX", "ScaleY", "TransX", "TransY" };
3141 for (int i = 0; i < 6; ++i) {
3142 SkDebugf("%s: %g ", names[i], affine[i]);
3143 }
3144 SkDebugf("\n");
3145 matrix.reset();
3146 matrix.setAffine(affine);
3147 matrix.dump();
Cary Clark154beea2017-10-26 07:58:48 -04003148}
Cary Clark154beea2017-10-26 07:58:48 -04003149#StdOut
Cary Clark682c58d2018-05-16 07:07:07 -04003150ScaleX: 2 SkewY: 5 SkewX: 3 ScaleY: 6 TransX: 4 TransY: 7
Cary Clark154beea2017-10-26 07:58:48 -04003151[ 2.0000 3.0000 4.0000][ 5.0000 6.0000 7.0000][ 0.0000 0.0000 1.0000]
3152##
3153##
3154
3155#SeeAlso asAffine SetAffineIdentity
Cary Clarkbc5697d2017-10-04 14:31:33 -04003156
3157##
3158
3159# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05003160#Subtopic Transform
3161#Populate
3162#Line # map points with Matrix ##
3163##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003164
3165#Method void mapPoints(SkPoint dst[], const SkPoint src[], int count) const
Cary Clark4855f782018-02-06 09:41:53 -05003166#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003167#Line # maps Point array ##
Cary Clark154beea2017-10-26 07:58:48 -04003168Maps src Point array of length count to dst Point array of equal or greater
3169length. Points are mapped by multiplying each Point by Matrix. Given:
3170
3171#Code
3172#Literal
3173 | A B C | | x |
3174Matrix = | D E F |, pt = | y |
3175 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003176##
3177
Cary Clark682c58d2018-05-16 07:07:07 -04003178where
Cary Clark154beea2017-10-26 07:58:48 -04003179
3180#Code
3181#Literal
3182for (i = 0; i < count; ++i) {
3183 x = src[i].fX
3184 y = src[i].fY
3185}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003186##
Cary Clark154beea2017-10-26 07:58:48 -04003187
3188each dst Point is computed as:
3189
3190#Code
3191#Literal
3192 |A B C| |x| Ax+By+C Dx+Ey+F
3193Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3194 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003195##
Cary Clark154beea2017-10-26 07:58:48 -04003196
3197src and dst may point to the same storage.
3198
3199#Param dst storage for mapped Points ##
3200#Param src Points to transform ##
3201#Param count number of Points to transform ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003202
3203#Example
Cary Clark154beea2017-10-26 07:58:48 -04003204 SkMatrix matrix;
3205 matrix.reset();
3206 const int count = 4;
3207 SkPoint src[count];
3208 matrix.mapRectToQuad(src, {40, 70, 180, 220} );
3209 SkPaint paint;
3210 paint.setARGB(77, 23, 99, 154);
3211 for (int i = 0; i < 5; ++i) {
3212 SkPoint dst[count];
3213 matrix.mapPoints(dst, src, count);
3214 canvas->drawPoints(SkCanvas::kPolygon_PointMode, count, dst, paint);
3215 matrix.preRotate(35, 128, 128);
3216 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003217##
3218
Brian Salomonfa3783f2018-01-05 13:49:07 -05003219#SeeAlso mapXY mapHomogeneousPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003220
3221##
3222
3223# ------------------------------------------------------------------------------
3224
3225#Method void mapPoints(SkPoint pts[], int count) const
3226
Cary Clark154beea2017-10-26 07:58:48 -04003227Maps pts Point array of length count in place. Points are mapped by multiplying
3228each Point by Matrix. Given:
3229
3230#Code
3231#Literal
3232 | A B C | | x |
3233Matrix = | D E F |, pt = | y |
3234 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003235##
3236
Cary Clark682c58d2018-05-16 07:07:07 -04003237where
Cary Clark154beea2017-10-26 07:58:48 -04003238
3239#Code
3240#Literal
3241for (i = 0; i < count; ++i) {
3242 x = pts[i].fX
3243 y = pts[i].fY
3244}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003245##
Cary Clark154beea2017-10-26 07:58:48 -04003246
3247each resulting pts Point is computed as:
3248
3249#Code
3250#Literal
3251 |A B C| |x| Ax+By+C Dx+Ey+F
3252Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3253 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003254##
3255
Cary Clark154beea2017-10-26 07:58:48 -04003256#Param pts storage for mapped Points ##
3257#Param count number of Points to transform ##
3258
Cary Clarkbc5697d2017-10-04 14:31:33 -04003259#Example
Cary Clark154beea2017-10-26 07:58:48 -04003260 SkMatrix matrix;
3261 matrix.setRotate(35, 128, 128);
3262 const int count = 4;
3263 SkPoint pts[count];
3264 matrix.mapRectToQuad(pts, {40, 70, 180, 220} );
3265 SkPaint paint;
3266 paint.setARGB(77, 23, 99, 154);
3267 for (int i = 0; i < 5; ++i) {
3268 canvas->drawPoints(SkCanvas::kPolygon_PointMode, count, pts, paint);
3269 matrix.mapPoints(pts, count);
3270 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003271##
3272
Brian Salomonfa3783f2018-01-05 13:49:07 -05003273#SeeAlso mapXY mapHomogeneousPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003274
3275##
3276
3277# ------------------------------------------------------------------------------
3278
Cary Clark154beea2017-10-26 07:58:48 -04003279#Method void mapHomogeneousPoints(SkPoint3 dst[], const SkPoint3 src[], int count) const
Cary Clark4855f782018-02-06 09:41:53 -05003280#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003281#Line # maps Point3 array ##
Cary Clark154beea2017-10-26 07:58:48 -04003282Maps src Point3 array of length count to dst Point3 array, which must of length count or
3283greater. Point3 array is mapped by multiplying each Point3 by Matrix. Given:
3284
3285#Code
3286#Literal
3287 | A B C | | x |
3288Matrix = | D E F |, src = | y |
3289 | G H I | | z |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003290##
3291
Cary Clark154beea2017-10-26 07:58:48 -04003292each resulting dst Point is computed as:
3293
3294#Code
3295#Literal
3296 |A B C| |x|
3297Matrix * src = |D E F| |y| = |Ax+By+Cz Dx+Ey+Fz Gx+Hy+Iz|
3298 |G H I| |z|
Cary Clarkbc5697d2017-10-04 14:31:33 -04003299##
Cary Clark154beea2017-10-26 07:58:48 -04003300
3301#Param dst storage for mapped Point3 array ##
3302#Param src Point3 array to transform ##
3303#Param count items in Point3 array to transform ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003304
3305#Example
Cary Clark154beea2017-10-26 07:58:48 -04003306 SkPoint3 src[] = {{3, 3, 1}, {8, 2, 2}, {5, 0, 4}, {0, 1, 3},
3307 {3, 7, 1}, {8, 6, 2}, {5, 4, 4}, {0, 5, 3}};
3308 int lines[] = { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 };
3309 constexpr int count = SK_ARRAY_COUNT(src);
3310 auto debugster = [=](SkPoint3 src[]) -> void {
3311 for (size_t i = 0; i < SK_ARRAY_COUNT(lines); i += 2) {
3312 const SkPoint3& s = src[lines[i]];
3313 const SkPoint3& e = src[lines[i + 1]];
3314 SkPaint paint;
3315 paint.setARGB(77, 23, 99, 154);
3316 canvas->drawLine(s.fX / s.fZ, s.fY / s.fZ, e.fX / e.fZ, e.fY / e.fZ, paint);
3317 }
3318 };
3319 canvas->save();
3320 canvas->translate(5, 5);
3321 canvas->scale(15, 15);
3322 debugster(src);
3323 canvas->restore();
3324 canvas->translate(128, 128);
3325 SkMatrix matrix;
3326 matrix.setAll(15, 0, 0, 0, 15, 0, -0.08, 0.04, 1);
Cary Clarka560c472017-11-27 10:44:06 -05003327 matrix.mapHomogeneousPoints(src, src, count);
Cary Clark154beea2017-10-26 07:58:48 -04003328 debugster(src);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003329##
3330
Brian Salomonfa3783f2018-01-05 13:49:07 -05003331#SeeAlso mapPoints mapXY mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003332
3333##
3334
3335# ------------------------------------------------------------------------------
3336
3337#Method void mapXY(SkScalar x, SkScalar y, SkPoint* result) const
Cary Clark4855f782018-02-06 09:41:53 -05003338#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003339#Line # maps Point ##
Cary Clark154beea2017-10-26 07:58:48 -04003340Maps Point (x, y) to result. Point is mapped by multiplying by Matrix. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003341
Cary Clark154beea2017-10-26 07:58:48 -04003342#Code
3343#Literal
3344 | A B C | | x |
3345Matrix = | D E F |, pt = | y |
3346 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003347##
3348
Cary Clark154beea2017-10-26 07:58:48 -04003349result is computed as:
3350
3351#Code
3352#Literal
3353 |A B C| |x| Ax+By+C Dx+Ey+F
3354Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3355 |G H I| |1| Gx+Hy+I Gx+Hy+I
3356##
3357
Cary Clark5538c132018-06-14 12:28:14 -04003358#Param x x-axis value of Point to map ##
3359#Param y y-axis value of Point to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003360#Param result storage for mapped Point ##
3361
3362#Example
3363 SkPaint paint;
3364 paint.setAntiAlias(true);
3365 SkMatrix matrix;
3366 matrix.setRotate(60, 128, 128);
3367 SkPoint lines[] = {{50, 50}, {150, 50}, {150, 150}};
3368 for (size_t i = 0; i < SK_ARRAY_COUNT(lines); ++i) {
3369 SkPoint pt;
3370 matrix.mapXY(lines[i].fX, lines[i].fY, &pt);
3371 canvas->drawCircle(pt.fX, pt.fY, 3, paint);
3372 }
3373 canvas->concat(matrix);
3374 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(lines), lines, paint);
3375##
3376
Brian Salomonfa3783f2018-01-05 13:49:07 -05003377#SeeAlso mapPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003378
3379##
3380
3381# ------------------------------------------------------------------------------
3382
3383#Method SkPoint mapXY(SkScalar x, SkScalar y) const
3384
Cary Clark154beea2017-10-26 07:58:48 -04003385Returns Point (x, y) multiplied by Matrix. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003386
Cary Clark154beea2017-10-26 07:58:48 -04003387#Code
3388#Literal
3389 | A B C | | x |
3390Matrix = | D E F |, pt = | y |
3391 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003392##
3393
Cary Clark154beea2017-10-26 07:58:48 -04003394result is computed as:
3395
3396#Code
3397#Literal
3398 |A B C| |x| Ax+By+C Dx+Ey+F
3399Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3400 |G H I| |1| Gx+Hy+I Gx+Hy+I
3401##
3402
Cary Clark5538c132018-06-14 12:28:14 -04003403#Param x x-axis value of Point to map ##
3404#Param y y-axis value of Point to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003405
3406#Return mapped Point ##
3407
3408#Example
3409#Image 4
3410SkMatrix matrix;
3411SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {30, 206}};
3412SkRect::Make(source.bounds()).toQuad(bitmapBounds);
3413matrix.setPolyToPoly(bitmapBounds, perspect, 4);
3414SkPaint paint;
3415paint.setAntiAlias(true);
3416paint.setStrokeWidth(3);
3417for (int x : { 0, source.width() } ) {
3418 for (int y : { 0, source.height() } ) {
3419 canvas->drawPoint(matrix.mapXY(x, y), paint);
3420 }
3421}
3422canvas->concat(matrix);
3423canvas->drawBitmap(source, 0, 0);
3424##
3425
Brian Salomonfa3783f2018-01-05 13:49:07 -05003426#SeeAlso mapPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003427
3428##
3429
3430# ------------------------------------------------------------------------------
3431
3432#Method void mapVectors(SkVector dst[], const SkVector src[], int count) const
Cary Clark4855f782018-02-06 09:41:53 -05003433#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003434#Line # maps Vector array ##
Cary Clark154beea2017-10-26 07:58:48 -04003435Maps src Vector array of length count to Vector Point array of equal or greater
3436length. Vectors are mapped by multiplying each Vector by Matrix, treating
3437Matrix translation as zero. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003438
Cary Clark154beea2017-10-26 07:58:48 -04003439#Code
3440#Literal
3441 | A B 0 | | x |
3442Matrix = | D E 0 |, src = | y |
3443 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003444##
Cary Clark154beea2017-10-26 07:58:48 -04003445
Cary Clark682c58d2018-05-16 07:07:07 -04003446where
Cary Clark154beea2017-10-26 07:58:48 -04003447
3448#Code
3449#Literal
3450for (i = 0; i < count; ++i) {
3451 x = src[i].fX
3452 y = src[i].fY
3453}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003454##
Cary Clark154beea2017-10-26 07:58:48 -04003455
3456each dst Vector is computed as:
3457
3458#Code
3459#Literal
3460 |A B 0| |x| Ax+By Dx+Ey
3461Matrix * src = |D E 0| |y| = |Ax+By Dx+Ey Gx+Hy+I| = ------- , -------
3462 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003463##
3464
Cary Clark154beea2017-10-26 07:58:48 -04003465src and dst may point to the same storage.
3466
3467#Param dst storage for mapped Vectors ##
3468#Param src Vectors to transform ##
3469#Param count number of Vectors to transform ##
3470
Cary Clarkbc5697d2017-10-04 14:31:33 -04003471#Example
Cary Clark154beea2017-10-26 07:58:48 -04003472 SkPaint paint;
3473 paint.setAntiAlias(true);
3474 paint.setStyle(SkPaint::kStroke_Style);
3475 SkMatrix matrix;
3476 matrix.reset();
3477 const SkVector radii[] = {{8, 4}, {9, 1}, {6, 2}, {7, 3}};
3478 for (int i = 0; i < 4; ++i) {
3479 SkVector rScaled[4];
3480 matrix.preScale(1.5f, 2.f);
3481 matrix.mapVectors(rScaled, radii, SK_ARRAY_COUNT(radii));
3482 SkRRect rrect;
3483 rrect.setRectRadii({20, 20, 180, 70}, rScaled);
3484 canvas->drawRRect(rrect, paint);
3485 canvas->translate(0, 60);
3486 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003487##
3488
Brian Salomonfa3783f2018-01-05 13:49:07 -05003489#SeeAlso mapVector mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003490
3491##
3492
3493# ------------------------------------------------------------------------------
3494
3495#Method void mapVectors(SkVector vecs[], int count) const
3496
Cary Clark154beea2017-10-26 07:58:48 -04003497Maps vecs Vector array of length count in place, multiplying each Vector by
3498Matrix, treating Matrix translation as zero. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003499
Cary Clark154beea2017-10-26 07:58:48 -04003500#Code
3501#Literal
3502 | A B 0 | | x |
3503Matrix = | D E 0 |, vec = | y |
3504 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003505##
Cary Clark154beea2017-10-26 07:58:48 -04003506
Cary Clark682c58d2018-05-16 07:07:07 -04003507where
Cary Clark154beea2017-10-26 07:58:48 -04003508
3509#Code
3510#Literal
3511for (i = 0; i < count; ++i) {
3512 x = vecs[i].fX
3513 y = vecs[i].fY
3514}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003515##
3516
Cary Clark154beea2017-10-26 07:58:48 -04003517each result Vector is computed as:
3518
3519#Code
3520#Literal
3521 |A B 0| |x| Ax+By Dx+Ey
3522Matrix * vec = |D E 0| |y| = |Ax+By Dx+Ey Gx+Hy+I| = ------- , -------
3523 |G H I| |1| Gx+Hy+I Gx+Hy+I
3524##
3525
3526#Param vecs Vectors to transform, and storage for mapped Vectors ##
3527#Param count number of Vectors to transform ##
3528
Cary Clarkbc5697d2017-10-04 14:31:33 -04003529#Example
Cary Clark154beea2017-10-26 07:58:48 -04003530 SkPaint paint;
3531 paint.setAntiAlias(true);
3532 paint.setStyle(SkPaint::kStroke_Style);
3533 SkMatrix matrix;
3534 matrix.setScale(2, 3);
3535 SkVector radii[] = {{7, 7}, {3, 3}, {2, 2}, {4, 0}};
3536 for (int i = 0; i < 4; ++i) {
3537 SkRRect rrect;
3538 rrect.setRectRadii({20, 20, 180, 70}, radii);
3539 canvas->drawRRect(rrect, paint);
3540 canvas->translate(0, 60);
3541 matrix.mapVectors(radii, SK_ARRAY_COUNT(radii));
3542 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003543##
3544
Brian Salomonfa3783f2018-01-05 13:49:07 -05003545#SeeAlso mapVector mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003546
3547##
3548
3549# ------------------------------------------------------------------------------
3550
3551#Method void mapVector(SkScalar dx, SkScalar dy, SkVector* result) const
Cary Clark4855f782018-02-06 09:41:53 -05003552#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003553#Line # maps Vector ##
Cary Clark154beea2017-10-26 07:58:48 -04003554Maps Vector (x, y) to result. Vector is mapped by multiplying by Matrix,
3555treating Matrix translation as zero. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003556
Cary Clark154beea2017-10-26 07:58:48 -04003557#Code
3558#Literal
3559 | A B 0 | | dx |
3560Matrix = | D E 0 |, vec = | dy |
3561 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003562##
3563
Cary Clark154beea2017-10-26 07:58:48 -04003564each result Vector is computed as:
3565
3566#Code
3567#Literal
3568#Outdent
3569 |A B 0| |dx| A*dx+B*dy D*dx+E*dy
3570Matrix * vec = |D E 0| |dy| = |A*dx+B*dy D*dx+E*dy G*dx+H*dy+I| = ----------- , -----------
3571 |G H I| | 1| G*dx+H*dy+I G*dx+*dHy+I
3572##
3573
Cary Clark5538c132018-06-14 12:28:14 -04003574#Param dx x-axis value of Vector to map ##
3575#Param dy y-axis value of Vector to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003576#Param result storage for mapped Vector ##
3577
3578#Example
3579 SkPaint paint;
3580 paint.setColor(SK_ColorGREEN);
3581 paint.setAntiAlias(true);
3582 paint.setTextSize(48);
3583 SkMatrix matrix;
3584 matrix.setRotate(90);
3585 SkVector offset = { 7, 7 };
3586 for (int i = 0; i < 4; ++i) {
3587 paint.setImageFilter(SkDropShadowImageFilter::Make(offset.fX, offset.fY, 3, 3,
3588 SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr));
3589 matrix.mapVector(offset.fX, offset.fY, &offset);
3590 canvas->translate(0, 60);
3591 canvas->drawString("Text", 50, 0, paint);
3592 }
3593##
3594
Brian Salomonfa3783f2018-01-05 13:49:07 -05003595#SeeAlso mapVectors mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003596
3597##
3598
3599# ------------------------------------------------------------------------------
3600
3601#Method SkVector mapVector(SkScalar dx, SkScalar dy) const
3602
Cary Clark154beea2017-10-26 07:58:48 -04003603Returns Vector (x, y) multiplied by Matrix, treating Matrix translation as zero.
3604Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003605
Cary Clark154beea2017-10-26 07:58:48 -04003606#Code
3607#Literal
3608 | A B 0 | | dx |
3609Matrix = | D E 0 |, vec = | dy |
3610 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003611##
3612
Cary Clark154beea2017-10-26 07:58:48 -04003613each result Vector is computed as:
3614
3615#Code
3616#Literal
3617#Outdent
3618 |A B 0| |dx| A*dx+B*dy D*dx+E*dy
3619Matrix * vec = |D E 0| |dy| = |A*dx+B*dy D*dx+E*dy G*dx+H*dy+I| = ----------- , -----------
3620 |G H I| | 1| G*dx+H*dy+I G*dx+*dHy+I
3621##
3622
Cary Clark5538c132018-06-14 12:28:14 -04003623#Param dx x-axis value of Vector to map ##
3624#Param dy y-axis value of Vector to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003625
3626#Return mapped Vector ##
3627
3628#Example
3629 SkPaint paint;
3630 paint.setColor(SK_ColorGREEN);
3631 paint.setAntiAlias(true);
3632 paint.setTextSize(48);
3633 SkMatrix matrix;
3634 matrix.setRotate(90);
3635 SkVector offset = { 7, 7 };
3636 for (int i = 0; i < 4; ++i) {
3637 paint.setImageFilter(SkDropShadowImageFilter::Make(offset.fX, offset.fY, 3, 3,
3638 SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr));
3639 offset = matrix.mapVector(offset.fX, offset.fY);
3640 canvas->translate(0, 60);
3641 canvas->drawString("Text", 50, 0, paint);
3642 }
3643##
3644
Brian Salomonfa3783f2018-01-05 13:49:07 -05003645#SeeAlso mapVectors mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003646
3647##
3648
3649# ------------------------------------------------------------------------------
3650
3651#Method bool mapRect(SkRect* dst, const SkRect& src) const
Cary Clark4855f782018-02-06 09:41:53 -05003652#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003653#Line # returns bounds of mapped Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04003654Sets dst to bounds of src corners mapped by Matrix.
3655Returns true if mapped corners are dst corners.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003656
Cary Clark154beea2017-10-26 07:58:48 -04003657Returned value is the same as calling rectStaysRect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003658
Cary Clark154beea2017-10-26 07:58:48 -04003659#Param dst storage for bounds of mapped Points ##
3660#Param src Rect to map ##
3661
3662#Return true if dst is equivalent to mapped src ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003663
3664#Example
Cary Clark154beea2017-10-26 07:58:48 -04003665 SkPaint paint;
3666 paint.setAntiAlias(true);
3667 SkMatrix matrix;
3668 matrix.setRotate(45, 128, 128);
3669 SkRect rotatedBounds, bounds = {40, 50, 190, 200};
3670 matrix.mapRect(&rotatedBounds, bounds );
3671 paint.setColor(SK_ColorGRAY);
3672 canvas->drawRect(rotatedBounds, paint);
3673 canvas->concat(matrix);
3674 paint.setColor(SK_ColorRED);
3675 canvas->drawRect(bounds, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003676##
3677
Cary Clark154beea2017-10-26 07:58:48 -04003678#SeeAlso mapPoints rectStaysRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04003679
3680##
3681
3682# ------------------------------------------------------------------------------
3683
3684#Method bool mapRect(SkRect* rect) const
3685
Cary Clark154beea2017-10-26 07:58:48 -04003686Sets rect to bounds of rect corners mapped by Matrix.
3687Returns true if mapped corners are computed rect corners.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003688
Cary Clark154beea2017-10-26 07:58:48 -04003689Returned value is the same as calling rectStaysRect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003690
Cary Clark154beea2017-10-26 07:58:48 -04003691#Param rect rectangle to map, and storage for bounds of mapped corners ##
3692
3693#Return true if result is equivalent to mapped src ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003694
3695#Example
Cary Clark154beea2017-10-26 07:58:48 -04003696 SkPaint paint;
3697 paint.setAntiAlias(true);
3698 SkMatrix matrix;
3699 matrix.setRotate(45, 128, 128);
3700 SkRect bounds = {40, 50, 190, 200};
3701 matrix.mapRect(&bounds);
3702 paint.setColor(SK_ColorGRAY);
3703 canvas->drawRect(bounds, paint);
3704 canvas->concat(matrix);
3705 paint.setColor(SK_ColorRED);
3706 canvas->drawRect({40, 50, 190, 200}, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003707##
3708
Cary Clark154beea2017-10-26 07:58:48 -04003709#SeeAlso mapRectScaleTranslate mapPoints rectStaysRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04003710
3711##
3712
3713# ------------------------------------------------------------------------------
3714
Cary Clarkc06754b2018-05-16 21:28:55 -04003715#Method SkRect mapRect(const SkRect& src) const
3716
3717Returns bounds of src corners mapped by Matrix.
3718
3719#Param src rectangle to map ##
3720#Return mapped bounds ##
3721
3722#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04003723 SkRect rect{110, 50, 180, 100};
3724 SkMatrix matrix;
3725 matrix.setRotate(50, 28, 28);
3726 SkRect mapped = matrix.mapRect(rect);
3727 SkPaint paint;
3728 paint.setAntiAlias(true);
3729 paint.setStyle(SkPaint::kStroke_Style);
3730 canvas->drawRect(rect, paint);
3731 canvas->drawRect(mapped, paint);
3732 canvas->concat(matrix);
3733 canvas->drawRect(rect, paint);
Cary Clarkc06754b2018-05-16 21:28:55 -04003734##
3735
Cary Clarkffb3d682018-05-17 12:17:28 -04003736#SeeAlso mapRectToQuad mapRectScaleTranslate
Cary Clarkc06754b2018-05-16 21:28:55 -04003737#Method ##
3738
3739# ------------------------------------------------------------------------------
3740
Cary Clarkbc5697d2017-10-04 14:31:33 -04003741#Method void mapRectToQuad(SkPoint dst[4], const SkRect& rect) const
Cary Clark4855f782018-02-06 09:41:53 -05003742#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003743#Line # maps Rect to Point array ##
Cary Clark154beea2017-10-26 07:58:48 -04003744Maps four corners of rect to dst. Points are mapped by multiplying each
3745rect corner by Matrix. rect corner is processed in this order:
3746(rect.fLeft, rect.fTop), (rect.fRight, rect.fTop), (rect.fRight, rect.fBottom),
Cary Clark682c58d2018-05-16 07:07:07 -04003747(rect.fLeft, rect.fBottom).
Cary Clarkbc5697d2017-10-04 14:31:33 -04003748
Cary Clark154beea2017-10-26 07:58:48 -04003749rect may be empty: rect.fLeft may be greater than or equal to rect.fRight;
3750rect.fTop may be greater than or equal to rect.fBottom.
3751
3752Given:
3753
3754#Code
3755#Literal
3756 | A B C | | x |
3757Matrix = | D E F |, pt = | y |
3758 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003759##
Cary Clark154beea2017-10-26 07:58:48 -04003760
3761where pt is initialized from each of (rect.fLeft, rect.fTop),
3762(rect.fRight, rect.fTop), (rect.fRight, rect.fBottom), (rect.fLeft, rect.fBottom),
3763each dst Point is computed as:
3764
3765#Code
3766#Literal
3767 |A B C| |x| Ax+By+C Dx+Ey+F
3768Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3769 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003770##
3771
Cary Clark154beea2017-10-26 07:58:48 -04003772#Param dst storage for mapped corner Points ##
3773#Param rect Rect to map ##
3774
Cary Clarkbc5697d2017-10-04 14:31:33 -04003775#Example
Cary Clark2ade9972017-11-02 17:49:34 -04003776#Height 192
Cary Clark154beea2017-10-26 07:58:48 -04003777 SkPaint paint;
3778 paint.setAntiAlias(true);
3779 SkMatrix matrix;
3780 matrix.setRotate(60, 128, 128);
3781 SkRect rect = {50, 50, 150, 150};
3782 SkPoint pts[4];
3783 matrix.mapRectToQuad(pts, rect);
3784 for (int i = 0; i < 4; ++i) {
3785 canvas->drawCircle(pts[i].fX, pts[i].fY, 3, paint);
3786 }
3787 canvas->concat(matrix);
3788 paint.setStyle(SkPaint::kStroke_Style);
3789 canvas->drawRect(rect, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003790##
3791
Cary Clark154beea2017-10-26 07:58:48 -04003792#SeeAlso mapRect mapRectScaleTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -04003793
3794##
3795
3796# ------------------------------------------------------------------------------
3797
3798#Method void mapRectScaleTranslate(SkRect* dst, const SkRect& src) const
Cary Clark4855f782018-02-06 09:41:53 -05003799#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003800#Line # returns bounds of mapped Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04003801Sets dst to bounds of src corners mapped by Matrix. If matrix contains
3802elements other than scale or translate: asserts if SK_DEBUG is defined;
3803otherwise, results are undefined.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003804
Cary Clark154beea2017-10-26 07:58:48 -04003805#Param dst storage for bounds of mapped Points ##
3806#Param src Rect to map ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003807
3808#Example
Cary Clark154beea2017-10-26 07:58:48 -04003809 SkPaint paint;
3810 SkMatrix matrix;
3811 SkRect rect = {100, 50, 150, 180};
3812 matrix.setScale(2, .5f, rect.centerX(), rect.centerY());
3813 SkRect rotated;
3814 matrix.mapRectScaleTranslate(&rotated, rect);
3815 paint.setStyle(SkPaint::kStroke_Style);
3816 canvas->drawRect(rect, paint);
3817 paint.setColor(SK_ColorRED);
3818 canvas->drawRect(rotated, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003819##
3820
Cary Clark154beea2017-10-26 07:58:48 -04003821#SeeAlso mapRect mapRectToQuad isScaleTranslate rectStaysRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04003822
3823##
3824
3825# ------------------------------------------------------------------------------
3826
3827#Method SkScalar mapRadius(SkScalar radius) const
Cary Clark4855f782018-02-06 09:41:53 -05003828#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003829#Line # returns mean radius of mapped Circle ##
Cary Clark154beea2017-10-26 07:58:48 -04003830Returns geometric mean radius of ellipse formed by constructing Circle of
3831size radius, and mapping constructed Circle with Matrix. The result squared is
3832equal to the major axis length times the minor axis length.
3833Result is not meaningful if Matrix contains perspective elements.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003834
Cary Clark154beea2017-10-26 07:58:48 -04003835#Param radius Circle size to map ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003836
Cary Clark154beea2017-10-26 07:58:48 -04003837#Return average mapped radius ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003838
3839#Example
Cary Clark154beea2017-10-26 07:58:48 -04003840#Description
3841The area enclosed by a square with sides equal to mappedRadius is the same as
3842the area enclosed by the ellipse major and minor axes.
3843##
3844 SkPaint paint;
3845 paint.setAntiAlias(true);
3846 SkMatrix matrix;
3847 const SkPoint center = {108, 93};
3848 matrix.setScale(2, .5f, center.fX, center.fY);
3849 matrix.postRotate(45, center.fX, center.fY);
3850 const SkScalar circleRadius = 50;
3851 SkScalar mappedRadius = matrix.mapRadius(circleRadius);
3852 SkVector minorAxis, majorAxis;
3853 matrix.mapVector(0, circleRadius, &minorAxis);
3854 matrix.mapVector(circleRadius, 0, &majorAxis);
3855 SkString mappedArea;
3856 mappedArea.printf("area = %g", mappedRadius * mappedRadius);
3857 canvas->drawString(mappedArea, 145, 250, paint);
3858 canvas->drawString("mappedRadius", center.fX + mappedRadius + 3, center.fY, paint);
3859 paint.setColor(SK_ColorRED);
3860 SkString axArea;
3861 axArea.printf("area = %g", majorAxis.length() * minorAxis.length());
3862 paint.setStyle(SkPaint::kFill_Style);
3863 canvas->drawString(axArea, 15, 250, paint);
3864 paint.setStyle(SkPaint::kStroke_Style);
3865 canvas->drawRect({10, 200, 10 + majorAxis.length(), 200 + minorAxis.length()}, paint);
3866 paint.setColor(SK_ColorBLACK);
3867 canvas->drawLine(center.fX, center.fY, center.fX + mappedRadius, center.fY, paint);
3868 canvas->drawLine(center.fX, center.fY, center.fX, center.fY + mappedRadius, paint);
3869 canvas->drawRect({140, 180, 140 + mappedRadius, 180 + mappedRadius}, paint);
3870 canvas->concat(matrix);
3871 canvas->drawCircle(center.fX, center.fY, circleRadius, paint);
3872 paint.setColor(SK_ColorRED);
3873 canvas->drawLine(center.fX, center.fY, center.fX + circleRadius, center.fY, paint);
3874 canvas->drawLine(center.fX, center.fY, center.fX, center.fY + circleRadius, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003875##
3876
Cary Clark154beea2017-10-26 07:58:48 -04003877#SeeAlso mapVector
Cary Clarkbc5697d2017-10-04 14:31:33 -04003878
3879##
3880
3881# ------------------------------------------------------------------------------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003882#Method bool isFixedStepInX() const
Cary Clark4855f782018-02-06 09:41:53 -05003883#In Property
Cary Clark08895c42018-02-01 09:37:32 -05003884#Line # returns if transformation supports fixed step in x ##
Cary Clark5538c132018-06-14 12:28:14 -04003885Returns true if a unit step on x-axis at some y-axis value mapped through Matrix
3886can be represented by a constant Vector. Returns true if getType returns
3887kIdentity_Mask, or combinations of: kTranslate_Mask, kScale_Mask, and kAffine_Mask.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003888
Cary Clark154beea2017-10-26 07:58:48 -04003889May return true if getType returns kPerspective_Mask, but only when Matrix
3890does not include rotation or skewing along the y-axis.
3891
3892#Return true if Matrix does not have complex perspective ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003893
3894#Example
Cary Clark154beea2017-10-26 07:58:48 -04003895 SkMatrix matrix;
3896 for (SkScalar px : { 0.0f, 0.1f } ) {
3897 for (SkScalar py : { 0.0f, 0.1f } ) {
3898 for (SkScalar sy : { 1, 2 } ) {
3899 matrix.setAll(1, 0, 0, 0, sy, 0, px, py, 1);
3900 matrix.dump();
3901 SkDebugf("isFixedStepInX: %s\n", matrix.isFixedStepInX() ? "true" : "false");
3902 }
3903 }
3904 }
3905#StdOut
3906[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
3907isFixedStepInX: true
3908[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.0000 0.0000 1.0000]
3909isFixedStepInX: true
3910[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.1000 1.0000]
3911isFixedStepInX: true
3912[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.0000 0.1000 1.0000]
3913isFixedStepInX: true
3914[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.1000 0.0000 1.0000]
3915isFixedStepInX: false
3916[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.1000 0.0000 1.0000]
3917isFixedStepInX: false
3918[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.1000 0.1000 1.0000]
3919isFixedStepInX: false
3920[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.1000 0.1000 1.0000]
3921isFixedStepInX: false
3922##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003923##
3924
Cary Clark154beea2017-10-26 07:58:48 -04003925#SeeAlso fixedStepInX getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04003926
3927##
3928
3929# ------------------------------------------------------------------------------
3930
3931#Method SkVector fixedStepInX(SkScalar y) const
Cary Clark4855f782018-02-06 09:41:53 -05003932#In Property
Cary Clark08895c42018-02-01 09:37:32 -05003933#Line # returns step in x for a position in y ##
Cary Clark5538c132018-06-14 12:28:14 -04003934Returns Vector representing a unit step on x-axis at y mapped through Matrix.
Cary Clark154beea2017-10-26 07:58:48 -04003935If isFixedStepInX is false, returned value is undefined.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003936
Cary Clark154beea2017-10-26 07:58:48 -04003937#Param y position of line parallel to x-axis ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003938
Cary Clark5538c132018-06-14 12:28:14 -04003939#Return Vector advance of mapped unit step on x-axis ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003940
3941#Example
Cary Clark154beea2017-10-26 07:58:48 -04003942#Image 3
3943 SkMatrix matrix;
3944 const SkPoint center = { 128, 128 };
3945 matrix.setScale(20, 25, center.fX, center.fY);
3946 matrix.postRotate(75, center.fX, center.fY);
3947 {
3948 SkAutoCanvasRestore acr(canvas, true);
3949 canvas->concat(matrix);
3950 canvas->drawBitmap(source, 0, 0);
3951 }
3952 if (matrix.isFixedStepInX()) {
3953 SkPaint paint;
3954 paint.setAntiAlias(true);
3955 SkVector step = matrix.fixedStepInX(128);
3956 SkVector end = center + step;
3957 canvas->drawLine(center, end, paint);
3958 SkVector arrow = { step.fX + step.fY, step.fY - step.fX};
3959 arrow = arrow * .25f;
3960 canvas->drawLine(end, end - arrow, paint);
3961 canvas->drawLine(end, {end.fX + arrow.fY, end.fY - arrow.fX}, paint);
3962 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003963##
3964
Cary Clark154beea2017-10-26 07:58:48 -04003965#SeeAlso isFixedStepInX getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04003966
3967##
3968
3969# ------------------------------------------------------------------------------
3970
3971#Method bool cheapEqualTo(const SkMatrix& m) const
Cary Clark4855f782018-02-06 09:41:53 -05003972#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05003973#Line # compares Matrix pair using memcmp() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003974Returns true if Matrix equals m, using an efficient comparison.
3975
Cary Clark154beea2017-10-26 07:58:48 -04003976Returns false when the sign of zero values is the different; when one
Cary Clarkbc5697d2017-10-04 14:31:33 -04003977matrix has positive zero value and the other has negative zero value.
3978
Cary Clark154beea2017-10-26 07:58:48 -04003979Returns true even when both Matrices contain NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003980
Cary Clark154beea2017-10-26 07:58:48 -04003981NaN never equals any value, including itself. To improve performance, NaN values
3982are treated as bit patterns that are equal if their bit patterns are equal.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003983
Cary Clark154beea2017-10-26 07:58:48 -04003984#Param m Matrix to compare ##
3985
3986#Return true if m and Matrix are represented by identical bit patterns ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003987
3988#Example
Cary Clark154beea2017-10-26 07:58:48 -04003989 auto debugster = [](const char* prefix, const SkMatrix& a, const SkMatrix& b) -> void {
3990 SkDebugf("%s: a %c= b a.cheapEqualTo(b): %s\n", prefix,
3991 a == b ? '=' : '!', a.cheapEqualTo(b) ? "true" : "false");
Cary Clarkbc5697d2017-10-04 14:31:33 -04003992 };
Cary Clark154beea2017-10-26 07:58:48 -04003993 SkMatrix a, b;
3994 a.setAll(1, 0, 0, 0, 1, 0, 0, 0, 1);
3995 b.setIdentity();
3996 debugster("identity", a, b);
3997 a.setAll(1, -0.0f, 0, 0, 1, 0, 0, 0, 1);
3998 debugster("neg zero", a, b);
3999 a.setAll(1, SK_ScalarNaN, 0, 0, 1, 0, 0, 0, 1);
4000 debugster(" one NaN", a, b);
4001 b.setAll(1, SK_ScalarNaN, 0, 0, 1, 0, 0, 0, 1);
4002 debugster("both NaN", a, b);
4003#StdOut
4004identity: a == b a.cheapEqualTo(b): true
4005neg zero: a == b a.cheapEqualTo(b): false
4006 one NaN: a != b a.cheapEqualTo(b): false
4007both NaN: a != b a.cheapEqualTo(b): true
4008##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004009##
4010
Cary Clark154beea2017-10-26 07:58:48 -04004011#SeeAlso operator==(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004012
4013##
4014
4015# ------------------------------------------------------------------------------
4016
Cary Clark154beea2017-10-26 07:58:48 -04004017#Method bool operator==(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004018
Cary Clark08895c42018-02-01 09:37:32 -05004019#Line # returns true if members are equal ##
Cary Clark154beea2017-10-26 07:58:48 -04004020Compares a and b; returns true if a and b are numerically equal. Returns true
4021even if sign of zero values are different. Returns false if either Matrix
4022contains NaN, even if the other Matrix also contains NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004023
Cary Clark154beea2017-10-26 07:58:48 -04004024#Param a Matrix to compare ##
4025#Param b Matrix to compare ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004026
Cary Clark4855f782018-02-06 09:41:53 -05004027#Return true if Matrix a and Matrix b are numerically equal ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004028
4029#Example
Cary Clark154beea2017-10-26 07:58:48 -04004030 auto debugster = [](const char* prefix, const SkMatrix& a, const SkMatrix& b) -> void {
4031 SkDebugf("%s: a %c= b a.cheapEqualTo(b): %s\n", prefix,
4032 a == b ? '=' : '!', a.cheapEqualTo(b) ? "true" : "false");
4033 };
4034 SkMatrix a, b;
4035 a.setAll(1, 0, 0, 0, 1, 0, 0, 0, 1);
4036 b.setScale(2, 4);
4037 b.postScale(0.5f, 0.25f);
4038 debugster("identity", a, b);
4039#StdOut
4040identity: a == b a.cheapEqualTo(b): true
4041##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004042##
4043
Cary Clark154beea2017-10-26 07:58:48 -04004044#SeeAlso cheapEqualTo operator!=(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004045
4046##
4047
4048# ------------------------------------------------------------------------------
4049
Cary Clark154beea2017-10-26 07:58:48 -04004050#Method bool operator!=(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004051
Cary Clark08895c42018-02-01 09:37:32 -05004052#Line # returns true if members are unequal ##
Cary Clark154beea2017-10-26 07:58:48 -04004053Compares a and b; returns true if a and b are not numerically equal. Returns false
4054even if sign of zero values are different. Returns true if either Matrix
4055contains NaN, even if the other Matrix also contains NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004056
Cary Clark154beea2017-10-26 07:58:48 -04004057#Param a Matrix to compare ##
4058#Param b Matrix to compare ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004059
Cary Clark4855f782018-02-06 09:41:53 -05004060#Return true if Matrix a and Matrix b are numerically not equal ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004061
4062#Example
Cary Clark154beea2017-10-26 07:58:48 -04004063 auto debugster = [](const char* prefix, const SkMatrix& a, const SkMatrix& b) -> void {
4064 SkDebugf("%s: a %c= b a.cheapEqualTo(b): %s\n", prefix,
4065 a != b ? '!' : '=', a.cheapEqualTo(b) ? "true" : "false");
4066 };
4067 SkMatrix a, b;
4068 a.setAll(1, 0, 0, 0, 1, 0, 1, 0, 1);
Cary Clark681287e2018-03-16 11:34:15 -04004069 if (a.invert(&b)) {
4070 debugster("identity", a, b);
4071 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04004072##
4073
Cary Clark154beea2017-10-26 07:58:48 -04004074#SeeAlso cheapEqualTo operator==(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004075
4076##
4077
4078# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05004079#Subtopic Utility
4080#Populate
4081#Line # rarely called management functions ##
4082##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004083
4084#Method void dump() const
Cary Clark4855f782018-02-06 09:41:53 -05004085#In Utility
Cary Clark08895c42018-02-01 09:37:32 -05004086#Line # sends text representation using floats to standard output ##
Cary Clark154beea2017-10-26 07:58:48 -04004087Writes text representation of Matrix to standard output. Floating point values
4088are written with limited precision; it may not be possible to reconstruct
4089original Matrix from output.
4090
Cary Clarkbc5697d2017-10-04 14:31:33 -04004091#Example
Cary Clark154beea2017-10-26 07:58:48 -04004092 SkMatrix matrix;
4093 matrix.setRotate(45);
4094 matrix.dump();
4095 SkMatrix nearlyEqual;
4096 nearlyEqual.setAll(0.7071f, -0.7071f, 0, 0.7071f, 0.7071f, 0, 0, 0, 1);
4097 nearlyEqual.dump();
4098 SkDebugf("matrix %c= nearlyEqual\n", matrix == nearlyEqual ? '=' : '!');
4099#StdOut
4100[ 0.7071 -0.7071 0.0000][ 0.7071 0.7071 0.0000][ 0.0000 0.0000 1.0000]
4101[ 0.7071 -0.7071 0.0000][ 0.7071 0.7071 0.0000][ 0.0000 0.0000 1.0000]
4102matrix != nearlyEqual
4103##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004104##
4105
Cary Clark27dddae2018-06-08 15:57:37 -04004106#SeeAlso SkPath::dump
Cary Clarkbc5697d2017-10-04 14:31:33 -04004107
4108##
4109
4110# ------------------------------------------------------------------------------
4111
4112#Method SkScalar getMinScale() const
Cary Clark4855f782018-02-06 09:41:53 -05004113#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004114#Line # returns minimum scaling, if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04004115Returns the minimum scaling factor of Matrix by decomposing the scaling and
4116skewing elements.
4117Returns -1 if scale factor overflows or Matrix contains perspective.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004118
4119#Return minimum scale factor
4120##
4121
4122#Example
Cary Clark154beea2017-10-26 07:58:48 -04004123 SkMatrix matrix;
4124 matrix.setScale(42, 24);
4125 SkDebugf("matrix.getMinScale() %g\n", matrix.getMinScale());
4126#StdOut
4127matrix.getMinScale() 24
4128##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004129##
4130
Cary Clark154beea2017-10-26 07:58:48 -04004131#SeeAlso getMaxScale getMinMaxScales
Cary Clarkbc5697d2017-10-04 14:31:33 -04004132
4133##
4134
4135# ------------------------------------------------------------------------------
4136
4137#Method SkScalar getMaxScale() const
Cary Clark4855f782018-02-06 09:41:53 -05004138#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004139#Line # returns maximum scaling, if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04004140Returns the maximum scaling factor of Matrix by decomposing the scaling and
4141skewing elements.
4142Returns -1 if scale factor overflows or Matrix contains perspective.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004143
4144#Return maximum scale factor
4145##
4146
4147#Example
Cary Clark154beea2017-10-26 07:58:48 -04004148 SkMatrix matrix;
4149 matrix.setScale(42, 24);
4150 SkDebugf("matrix.getMaxScale() %g\n", matrix.getMaxScale());
4151#StdOut
4152matrix.getMaxScale() 42
4153##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004154##
4155
Cary Clark154beea2017-10-26 07:58:48 -04004156#SeeAlso getMinScale getMinMaxScales
Cary Clarkbc5697d2017-10-04 14:31:33 -04004157
4158##
4159
4160# ------------------------------------------------------------------------------
4161
4162#Method bool SK_WARN_UNUSED_RESULT getMinMaxScales(SkScalar scaleFactors[2]) const
Cary Clark4855f782018-02-06 09:41:53 -05004163#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004164#Line # returns minimum and maximum scaling, if possible ##
Cary Clark682c58d2018-05-16 07:07:07 -04004165Sets scaleFactors[0] to the minimum scaling factor, and scaleFactors[1] to the
Cary Clark154beea2017-10-26 07:58:48 -04004166maximum scaling factor. Scaling factors are computed by decomposing
4167the Matrix scaling and skewing elements.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004168
Cary Clark154beea2017-10-26 07:58:48 -04004169Returns true if scaleFactors are found; otherwise, returns false and sets
4170scaleFactors to undefined values.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004171
Cary Clark154beea2017-10-26 07:58:48 -04004172#Param scaleFactors storage for minimum and maximum scale factors ##
4173
4174#Return true if scale factors were computed correctly ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004175
4176#Example
Cary Clark154beea2017-10-26 07:58:48 -04004177 SkMatrix matrix;
4178 matrix.setAll(1, 0, 0, 0, 1, 0, 0, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04004179 if (matrix.invert(&matrix)) {
4180 SkScalar factor[2] = {2, 2};
4181 bool result = matrix.getMinMaxScales(factor);
4182 SkDebugf("matrix.getMinMaxScales() %s %g %g\n",
4183 result ? "true" : "false", factor[0], factor[1]);
4184 }
Cary Clark154beea2017-10-26 07:58:48 -04004185#StdOut
4186matrix.getMinMaxScales() false 2 2
4187##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004188##
4189
Cary Clark154beea2017-10-26 07:58:48 -04004190#SeeAlso getMinScale getMaxScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04004191
4192##
4193
4194# ------------------------------------------------------------------------------
4195
4196#Method bool decomposeScale(SkSize* scale, SkMatrix* remaining = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05004197#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004198#Line # separates scale if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04004199Decomposes Matrix into scale components and whatever remains. Returns false if
4200Matrix could not be decomposed.
4201
Cary Clark5538c132018-06-14 12:28:14 -04004202Sets scale to portion of Matrix that scale axes. Sets remaining to Matrix
4203with scaling factored out. remaining may be passed as nullptr
Cary Clark154beea2017-10-26 07:58:48 -04004204to determine if Matrix can be decomposed without computing remainder.
4205
4206Returns true if scale components are found. scale and remaining are
4207unchanged if Matrix contains perspective; scale factors are not finite, or
Cary Clark682c58d2018-05-16 07:07:07 -04004208are nearly zero.
Cary Clark154beea2017-10-26 07:58:48 -04004209
4210On success
4211
Cary Clarkbc5697d2017-10-04 14:31:33 -04004212#Formula
Cary Clark154beea2017-10-26 07:58:48 -04004213Matrix = scale * Remaining
Cary Clarkbc5697d2017-10-04 14:31:33 -04004214##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004215
Cary Clark5538c132018-06-14 12:28:14 -04004216#Param scale axes scaling factors; may be nullptr ##
Cary Clark154beea2017-10-26 07:58:48 -04004217#Param remaining Matrix without scaling; may be nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004218
Cary Clark154beea2017-10-26 07:58:48 -04004219#Return true if scale can be computed ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004220
4221#Example
Cary Clark154beea2017-10-26 07:58:48 -04004222 SkMatrix matrix;
4223 matrix.setRotate(90 * SK_Scalar1);
4224 matrix.postScale(1.f / 4, 1.f / 2);
4225 matrix.dump();
4226 SkSize scale = {SK_ScalarNaN, SK_ScalarNaN};
4227 SkMatrix remaining;
4228 remaining.reset();
4229 bool success = matrix.decomposeScale(&scale, &remaining);
4230 SkDebugf("success: %s ", success ? "true" : "false");
4231 SkDebugf("scale: %g, %g\n", scale.width(), scale.height());
4232 remaining.dump();
4233 SkMatrix scaleMatrix = SkMatrix::MakeScale(scale.width(), scale.height());
4234 SkMatrix combined = SkMatrix::Concat(scaleMatrix, remaining);
4235 combined.dump();
4236#StdOut
4237[ 0.0000 -0.2500 0.0000][ 0.5000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
4238success: true scale: 0.5, 0.25
4239[ 0.0000 -0.5000 0.0000][ 2.0000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
4240[ 0.0000 -0.2500 0.0000][ 0.5000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
4241##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004242##
4243
Cary Clark154beea2017-10-26 07:58:48 -04004244#SeeAlso setScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04004245
4246##
4247
4248# ------------------------------------------------------------------------------
4249
4250#Method static const SkMatrix& I()
Cary Clark4855f782018-02-06 09:41:53 -05004251#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05004252#Line # returns a reference to a const identity Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -04004253Returns reference to const identity Matrix. Returned Matrix is set to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04004254
Cary Clark154beea2017-10-26 07:58:48 -04004255#Code
4256#Literal
4257| 1 0 0 |
4258| 0 1 0 |
4259| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004260##
4261
Cary Clark154beea2017-10-26 07:58:48 -04004262#Return const identity Matrix ##
4263
4264#Example
4265 SkMatrix m1, m2, m3;
4266 m1.reset();
4267 m2.setIdentity();
4268 m3 = SkMatrix::I();
4269 SkDebugf("m1 %c= m2\n", m1 == m2 ? '=' : '!');
4270 SkDebugf("m2 %c= m3\n", m1 == m2 ? '=' : '!');
4271#StdOut
4272m1 == m2
4273m2 == m3
4274##
4275##
4276
4277#SeeAlso reset() setIdentity
Cary Clarkbc5697d2017-10-04 14:31:33 -04004278
4279##
4280
4281# ------------------------------------------------------------------------------
4282
4283#Method static const SkMatrix& InvalidMatrix()
Cary Clark4855f782018-02-06 09:41:53 -05004284#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05004285#Line # returns a reference to a const invalid Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -04004286Returns reference to a const Matrix with invalid values. Returned Matrix is set
4287to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04004288
Cary Clark154beea2017-10-26 07:58:48 -04004289#Code
4290#Literal
4291| SK_ScalarMax SK_ScalarMax SK_ScalarMax |
4292| SK_ScalarMax SK_ScalarMax SK_ScalarMax |
4293| SK_ScalarMax SK_ScalarMax SK_ScalarMax |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004294##
4295
Cary Clark154beea2017-10-26 07:58:48 -04004296#Return const invalid Matrix ##
4297
4298#Example
4299 SkDebugf("scaleX %g\n", SkMatrix::InvalidMatrix().getScaleX());
4300#StdOut
4301scaleX 3.40282e+38
4302##
4303##
4304
4305#SeeAlso SeeAlso getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04004306
4307##
4308
4309# ------------------------------------------------------------------------------
4310
4311#Method static SkMatrix Concat(const SkMatrix& a, const SkMatrix& b)
Cary Clark4855f782018-02-06 09:41:53 -05004312#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05004313#Line # returns the concatenation of Matrix pair ##
Cary Clark154beea2017-10-26 07:58:48 -04004314Returns Matrix a multiplied by Matrix b.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004315
Cary Clark154beea2017-10-26 07:58:48 -04004316Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04004317
Cary Clark154beea2017-10-26 07:58:48 -04004318#Code
4319#Literal
4320 | A B C | | J K L |
4321a = | D E F |, b = | M N O |
4322 | G H I | | P Q R |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004323##
4324
Cary Clark154beea2017-10-26 07:58:48 -04004325sets Matrix to:
4326
4327#Code
4328#Literal
4329 | A B C | | J K L | | AJ+BM+CP AK+BN+CQ AL+BO+CR |
4330a * b = | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR |
4331 | G H I | | P Q R | | GJ+HM+IP GK+HN+IQ GL+HO+IR |
4332##
4333
4334#Param a Matrix on left side of multiply expression ##
4335#Param b Matrix on right side of multiply expression ##
4336
4337#Return Matrix computed from a times b ##
4338
4339#Example
4340#Height 64
4341#Image 4
4342#Description
4343setPolyToPoly creates perspective matrices, one the inverse of the other.
4344Multiplying the matrix by its inverse turns into an identity matrix.
4345##
4346SkMatrix matrix, matrix2;
4347SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
4348SkRect::Make(source.bounds()).toQuad(bitmapBounds);
4349matrix.setPolyToPoly(bitmapBounds, perspect, 4);
4350matrix2.setPolyToPoly(perspect, bitmapBounds, 4);
4351SkMatrix concat = SkMatrix::Concat(matrix, matrix2);
4352canvas->concat(concat);
4353canvas->drawBitmap(source, 0, 0);
4354##
4355
4356#SeeAlso preConcat postConcat
Cary Clarkbc5697d2017-10-04 14:31:33 -04004357
4358##
4359
4360# ------------------------------------------------------------------------------
4361
4362#Method void dirtyMatrixTypeCache()
Cary Clark4855f782018-02-06 09:41:53 -05004363#In Utility
Cary Clark08895c42018-02-01 09:37:32 -05004364#Line # sets internal cache to unknown state ##
Cary Clark682c58d2018-05-16 07:07:07 -04004365Sets internal cache to unknown state. Use to force update after repeated
Cary Clark154beea2017-10-26 07:58:48 -04004366modifications to Matrix element reference returned by operator[](int index).
Cary Clarkbc5697d2017-10-04 14:31:33 -04004367
4368#Example
Cary Clark154beea2017-10-26 07:58:48 -04004369SkMatrix matrix;
4370matrix.setIdentity();
4371SkDebugf("with identity matrix: x = %g\n", matrix.mapXY(24, 42).fX);
4372SkScalar& skewRef = matrix[SkMatrix::kMSkewX];
4373skewRef = 0;
4374SkDebugf("after skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
4375skewRef = 1;
4376SkDebugf("after 2nd skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
4377matrix.dirtyMatrixTypeCache();
4378SkDebugf("after dirty cache: x = %g\n", matrix.mapXY(24, 42).fX);
4379#StdOut
4380with identity matrix: x = 24
4381after skew x mod: x = 24
4382after 2nd skew x mod: x = 24
4383after dirty cache: x = 66
4384##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004385##
4386
Cary Clark154beea2017-10-26 07:58:48 -04004387#SeeAlso operator[](int index) getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04004388
4389##
4390
4391# ------------------------------------------------------------------------------
4392
4393#Method void setScaleTranslate(SkScalar sx, SkScalar sy, SkScalar tx, SkScalar ty)
Cary Clark4855f782018-02-06 09:41:53 -05004394#In Constructor
4395#In Set
Cary Clark08895c42018-02-01 09:37:32 -05004396#Line # sets to scale and translate ##
Cary Clark154beea2017-10-26 07:58:48 -04004397Initializes Matrix with scale and translate elements.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004398
Cary Clark154beea2017-10-26 07:58:48 -04004399#Code
4400#Literal
4401| sx 0 tx |
4402| 0 sy ty |
4403| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004404##
4405
Cary Clark154beea2017-10-26 07:58:48 -04004406#Param sx horizontal scale factor to store ##
4407#Param sy vertical scale factor to store ##
4408#Param tx horizontal translation to store ##
4409#Param ty vertical translation to store ##
4410
4411#Example
4412SkMatrix matrix;
4413matrix.setScaleTranslate(1, 2, 3, 4);
4414matrix.dump();
4415#StdOut
4416[ 1.0000 0.0000 3.0000][ 0.0000 2.0000 4.0000][ 0.0000 0.0000 1.0000]
4417##
4418##
4419
4420#SeeAlso setScale preTranslate postTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -04004421
4422##
4423
4424# ------------------------------------------------------------------------------
4425
4426#Method bool isFinite() const
Cary Clark4855f782018-02-06 09:41:53 -05004427#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004428#Line # returns if all Matrix values are not infinity, NaN ##
Cary Clark154beea2017-10-26 07:58:48 -04004429Returns true if all elements of the matrix are finite. Returns false if any
4430element is infinity, or NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004431
Cary Clark154beea2017-10-26 07:58:48 -04004432#Return true if matrix has only finite elements ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004433
4434#Example
Cary Clark154beea2017-10-26 07:58:48 -04004435SkMatrix matrix = SkMatrix::MakeTrans(SK_ScalarNaN, 0);
4436matrix.dump();
4437SkDebugf("matrix is finite: %s\n", matrix.isFinite() ? "true" : "false");
4438SkDebugf("matrix %c= matrix\n", matrix == matrix ? '=' : '!');
4439#StdOut
4440[ 1.0000 0.0000 nan][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
4441matrix is finite: false
4442matrix != matrix
4443##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004444##
4445
Cary Clark154beea2017-10-26 07:58:48 -04004446#SeeAlso operator==
Cary Clarkbc5697d2017-10-04 14:31:33 -04004447
4448##
4449
4450#Class SkMatrix ##
4451
4452#Topic Matrix ##
Cary Clark4855f782018-02-06 09:41:53 -05004453