blob: 046d10441a2e661d7c21e6a7e5849334eb22fd4d [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 Clark2be81cf2018-09-13 12:04:30 -04002456Sets Matrix to Matrix constructed from scaling by #Formula # (1/divx, 1/divy) ##,
Cary Clark154beea2017-10-26 07:58:48 -04002457about pivot point (px, py), multiplied by Matrix.
2458
2459Returns false if either divx or divy is zero.
2460
2461Given:
2462
2463#Code
2464#Literal
2465 | J K L | | sx 0 0 |
2466Matrix = | M N O |, I(divx, divy) = | 0 sy 0 |
2467 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002468##
2469
Cary Clark682c58d2018-05-16 07:07:07 -04002470where
Cary Clarkbc5697d2017-10-04 14:31:33 -04002471
Cary Clark154beea2017-10-26 07:58:48 -04002472#Code
2473#Literal
2474sx = 1 / divx
2475sy = 1 / divy
2476##
2477
2478sets Matrix to:
2479
2480#Code
2481#Literal
2482 | sx 0 0 | | J K L | | sx*J sx*K sx*L |
2483I(divx, divy) * Matrix = | 0 sy 0 | | M N O | = | sy*M sy*N sy*O |
2484 | 0 0 1 | | P Q R | | P Q R |
2485##
2486
2487#Param divx integer divisor for inverse scale in x ##
2488#Param divy integer divisor for inverse scale in y ##
2489
2490#Return true on successful scale ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002491
2492#Example
Cary Clark154beea2017-10-26 07:58:48 -04002493#Image 3
Cary Clark681287e2018-03-16 11:34:15 -04002494SkMatrix matrix;
Cary Clark154beea2017-10-26 07:58:48 -04002495SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2496SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2497matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2498matrix.postIDiv(1, 2);
2499canvas->concat(matrix);
2500canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002501##
2502
Cary Clark154beea2017-10-26 07:58:48 -04002503#SeeAlso postScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04002504
2505##
2506
2507# ------------------------------------------------------------------------------
2508
2509#Method void postRotate(SkScalar degrees, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05002510#In Set
2511#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002512#Line # post-multiplies Matrix by rotation ##
Cary Clark154beea2017-10-26 07:58:48 -04002513Sets Matrix to Matrix constructed from rotating by degrees about pivot point
2514(px, py), multiplied by Matrix.
2515This can be thought of as rotating about a pivot point after applying Matrix.
2516
2517Positive degrees rotates clockwise.
2518
2519Given:
2520
2521#Code
2522#Literal
2523 | J K L | | c -s dx |
2524Matrix = | M N O |, R(degrees, px, py) = | s c dy |
2525 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002526##
2527
Cary Clark682c58d2018-05-16 07:07:07 -04002528where
Cary Clark154beea2017-10-26 07:58:48 -04002529
2530#Code
2531#Literal
2532c = cos(degrees)
2533s = sin(degrees)
2534dx = s * py + (1 - c) * px
2535dy = -s * px + (1 - c) * py
2536##
2537
2538sets Matrix to:
2539
2540#Code
2541#Literal
2542 |c -s dx| |J K L| |cJ-sM+dx*P cK-sN+dx*Q cL-sO+dx+R|
2543R(degrees, px, py) * Matrix = |s c dy| |M N O| = |sJ+cM+dy*P sK+cN+dy*Q sL+cO+dy*R|
2544 |0 0 1| |P Q R| | P Q R|
2545##
2546
2547#Param degrees angle of axes relative to upright axes ##
2548#Param px pivot x ##
2549#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002550
2551#Example
Cary Clark154beea2017-10-26 07:58:48 -04002552#Image 3
2553SkMatrix matrix;
2554SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2555SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2556matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2557matrix.postRotate(45, source.width() / 2, source.height() / 2);
2558canvas->concat(matrix);
2559canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002560##
2561
Cary Clark154beea2017-10-26 07:58:48 -04002562#SeeAlso preRotate setRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002563
2564##
2565
2566# ------------------------------------------------------------------------------
2567
2568#Method void postRotate(SkScalar degrees)
2569
Cary Clark154beea2017-10-26 07:58:48 -04002570Sets Matrix to Matrix constructed from rotating by degrees about pivot point
2571(0, 0), multiplied by Matrix.
2572This can be thought of as rotating about the origin after applying Matrix.
2573
2574Positive degrees rotates clockwise.
2575
2576Given:
2577
2578#Code
2579#Literal
2580 | J K L | | c -s 0 |
2581Matrix = | M N O |, R(degrees, px, py) = | s c 0 |
2582 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002583##
2584
Cary Clark682c58d2018-05-16 07:07:07 -04002585where
Cary Clark154beea2017-10-26 07:58:48 -04002586
2587#Code
2588#Literal
2589c = cos(degrees)
2590s = sin(degrees)
2591##
2592
2593sets Matrix to:
2594
2595#Code
2596#Literal
2597 | c -s dx | | J K L | | cJ-sM cK-sN cL-sO |
2598R(degrees, px, py) * Matrix = | s c dy | | M N O | = | sJ+cM sK+cN sL+cO |
2599 | 0 0 1 | | P Q R | | P Q R |
2600##
2601
2602#Param degrees angle of axes relative to upright axes ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002603
2604#Example
Cary Clark154beea2017-10-26 07:58:48 -04002605#Image 3
2606SkMatrix matrix;
2607SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2608SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2609matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2610matrix.postRotate(45);
2611canvas->concat(matrix);
2612canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002613##
2614
Cary Clark154beea2017-10-26 07:58:48 -04002615#SeeAlso preRotate setRotate
Cary Clarkbc5697d2017-10-04 14:31:33 -04002616
2617##
2618
2619# ------------------------------------------------------------------------------
2620
2621#Method void postSkew(SkScalar kx, SkScalar ky, SkScalar px, SkScalar py)
Cary Clark4855f782018-02-06 09:41:53 -05002622#In Set
2623#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002624#Line # post-multiplies Matrix by skew ##
Cary Clark154beea2017-10-26 07:58:48 -04002625Sets Matrix to Matrix constructed from skewing by (kx, ky) about pivot point
2626(px, py), multiplied by Matrix.
2627This can be thought of as skewing about a pivot point after applying Matrix.
2628
2629Given:
2630
2631#Code
2632#Literal
2633 | J K L | | 1 kx dx |
2634Matrix = | M N O |, K(kx, ky, px, py) = | ky 1 dy |
2635 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002636##
2637
Cary Clark682c58d2018-05-16 07:07:07 -04002638where
Cary Clark154beea2017-10-26 07:58:48 -04002639
2640#Code
2641#Literal
2642dx = -kx * py
2643dy = -ky * px
2644##
2645
2646sets Matrix to:
2647
2648#Code
2649#Literal
2650 | 1 kx dx| |J K L| |J+kx*M+dx*P K+kx*N+dx*Q L+kx*O+dx+R|
2651K(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|
2652 | 0 0 1| |P Q R| | P Q R|
2653##
2654
2655#Param kx horizontal skew factor ##
2656#Param ky vertical skew factor ##
2657#Param px pivot x ##
2658#Param py pivot y ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002659
2660#Example
Cary Clark154beea2017-10-26 07:58:48 -04002661#Image 3
2662SkMatrix matrix;
2663SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2664SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2665matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2666matrix.postSkew(.5f, 0, source.width() / 2, source.height() / 2);
2667canvas->concat(matrix);
2668canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002669##
2670
Cary Clark154beea2017-10-26 07:58:48 -04002671#SeeAlso preSkew setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04002672
2673##
2674
2675# ------------------------------------------------------------------------------
2676
2677#Method void postSkew(SkScalar kx, SkScalar ky)
2678
Cary Clark154beea2017-10-26 07:58:48 -04002679Sets Matrix to Matrix constructed from skewing by (kx, ky) about pivot point
2680(0, 0), multiplied by Matrix.
2681This can be thought of as skewing about the origin after applying Matrix.
2682
2683Given:
2684
2685#Code
2686#Literal
2687 | J K L | | 1 kx 0 |
2688Matrix = | M N O |, K(kx, ky) = | ky 1 0 |
2689 | P Q R | | 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002690##
2691
Cary Clark154beea2017-10-26 07:58:48 -04002692sets Matrix to:
2693
2694#Code
2695#Literal
2696 | 1 kx 0 | | J K L | | J+kx*M K+kx*N L+kx*O |
2697K(kx, ky) * Matrix = | ky 1 0 | | M N O | = | ky*J+M ky*K+N ky*L+O |
2698 | 0 0 1 | | P Q R | | P Q R |
2699##
2700
2701#Param kx horizontal skew factor ##
2702#Param ky vertical skew factor ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002703
2704#Example
Cary Clark154beea2017-10-26 07:58:48 -04002705#Image 3
2706SkMatrix matrix;
2707SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2708SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2709matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2710matrix.postSkew(.5f, 0);
2711canvas->concat(matrix);
2712canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002713##
2714
Cary Clark154beea2017-10-26 07:58:48 -04002715#SeeAlso preSkew setSkew
Cary Clarkbc5697d2017-10-04 14:31:33 -04002716
2717##
2718
2719# ------------------------------------------------------------------------------
2720
2721#Method void postConcat(const SkMatrix& other)
Cary Clark4855f782018-02-06 09:41:53 -05002722#In Set
2723#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002724#Line # post-multiplies Matrix by Matrix parameter ##
Cary Clark154beea2017-10-26 07:58:48 -04002725Sets Matrix to Matrix other multiplied by Matrix.
2726This can be thought of mapping by other after applying Matrix.
2727
2728Given:
2729
2730#Code
2731#Literal
2732 | J K L | | A B C |
2733Matrix = | M N O |, other = | D E F |
2734 | P Q R | | G H I |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002735##
2736
Cary Clark154beea2017-10-26 07:58:48 -04002737sets Matrix to:
2738
2739#Code
2740#Literal
2741 | A B C | | J K L | | AJ+BM+CP AK+BN+CQ AL+BO+CR |
2742other * Matrix = | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR |
2743 | G H I | | P Q R | | GJ+HM+IP GK+HN+IQ GL+HO+IR |
2744##
2745
2746#Param other Matrix on left side of multiply expression ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002747
2748#Example
Cary Clark154beea2017-10-26 07:58:48 -04002749#Image 3
2750#Height 64
Cary Clark681287e2018-03-16 11:34:15 -04002751SkMatrix matrix;
Cary Clark154beea2017-10-26 07:58:48 -04002752SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
2753SkRect::Make(source.bounds()).toQuad(bitmapBounds);
2754matrix.setPolyToPoly(bitmapBounds, perspect, 4);
2755matrix.postConcat(matrix);
2756canvas->concat(matrix);
2757canvas->drawBitmap(source, 0, 0);
Cary Clarkbc5697d2017-10-04 14:31:33 -04002758##
2759
Cary Clark154beea2017-10-26 07:58:48 -04002760#SeeAlso preConcat setConcat Concat
Cary Clarkbc5697d2017-10-04 14:31:33 -04002761
2762##
2763
2764# ------------------------------------------------------------------------------
2765
2766#Enum ScaleToFit
Cary Clark682c58d2018-05-16 07:07:07 -04002767#Line # options to map Rects ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002768#Code
2769 enum ScaleToFit {
2770 kFill_ScaleToFit,
2771 kStart_ScaleToFit,
2772 kCenter_ScaleToFit,
2773 kEnd_ScaleToFit,
2774 };
2775##
2776
Cary Clark154beea2017-10-26 07:58:48 -04002777ScaleToFit describes how Matrix is constructed to map one Rect to another.
2778ScaleToFit may allow Matrix to have unequal horizontal and vertical scaling,
2779or may restrict Matrix to square scaling. If restricted, ScaleToFit specifies
2780how Matrix maps to the side or center of the destination Rect.
2781
2782#Const kFill_ScaleToFit 0
Cary Clark682c58d2018-05-16 07:07:07 -04002783#Line # scales in x and y to fill destination Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04002784 Computes Matrix that scales in x and y independently, so that source Rect is
2785 mapped to completely fill destination Rect. The aspect ratio of source Rect
2786 may change.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002787##
Cary Clark154beea2017-10-26 07:58:48 -04002788#Const kStart_ScaleToFit 1
Cary Clark682c58d2018-05-16 07:07:07 -04002789#Line # scales and aligns to left and top ##
Cary Clark154beea2017-10-26 07:58:48 -04002790 Computes Matrix that maintains source Rect aspect ratio, mapping source Rect
2791 width or height to destination Rect. Aligns mapping to left and top edges
2792 of destination Rect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002793##
Cary Clark154beea2017-10-26 07:58:48 -04002794#Const kCenter_ScaleToFit 2
Cary Clark682c58d2018-05-16 07:07:07 -04002795#Line # scales and aligns to center ##
Cary Clark154beea2017-10-26 07:58:48 -04002796 Computes Matrix that maintains source Rect aspect ratio, mapping source Rect
2797 width or height to destination Rect. Aligns mapping to center of destination
2798 Rect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002799##
Cary Clark154beea2017-10-26 07:58:48 -04002800#Const kEnd_ScaleToFit 3
Cary Clark682c58d2018-05-16 07:07:07 -04002801#Line # scales and aligns to right and bottom ##
Cary Clark154beea2017-10-26 07:58:48 -04002802 Computes Matrix that maintains source Rect aspect ratio, mapping source Rect
2803 width or height to destination Rect. Aligns mapping to right and bottom
2804 edges of destination Rect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002805##
2806
2807#Example
Cary Clark154beea2017-10-26 07:58:48 -04002808 const char* labels[] = { "Fill", "Start", "Center", "End" };
2809 SkRect rects[] = {{5, 5, 59, 59}, {5, 74, 59, 108}, {10, 123, 44, 172}, {10, 187, 54, 231}};
2810 SkRect bounds;
2811 source.getBounds(&bounds);
2812 SkPaint paint;
2813 paint.setAntiAlias(true);
2814 for (auto fit : { SkMatrix::kFill_ScaleToFit, SkMatrix::kStart_ScaleToFit,
2815 SkMatrix::kCenter_ScaleToFit, SkMatrix::kEnd_ScaleToFit } ) {
2816 for (auto rect : rects ) {
2817 canvas->drawRect(rect, paint);
2818 SkMatrix matrix;
2819 if (!matrix.setRectToRect(bounds, rect, fit)) {
2820 continue;
2821 }
2822 SkAutoCanvasRestore acr(canvas, true);
2823 canvas->concat(matrix);
2824 canvas->drawBitmap(source, 0, 0);
2825 }
2826 canvas->drawString(labels[fit], 10, 255, paint);
2827 canvas->translate(64, 0);
2828 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002829##
2830
Cary Clark154beea2017-10-26 07:58:48 -04002831#SeeAlso setRectToRect MakeRectToRect setPolyToPoly
Cary Clarkbc5697d2017-10-04 14:31:33 -04002832
2833##
2834
2835# ------------------------------------------------------------------------------
2836
2837#Method bool setRectToRect(const SkRect& src, const SkRect& dst, ScaleToFit stf)
Cary Clark4855f782018-02-06 09:41:53 -05002838#In Set
Cary Clark08895c42018-02-01 09:37:32 -05002839#Line # sets to map one Rect to another ##
Cary Clark154beea2017-10-26 07:58:48 -04002840Sets Matrix to scale and translate src Rect to dst Rect. stf selects whether
2841mapping completely fills dst or preserves the aspect ratio, and how to align
2842src within dst. Returns false if src is empty, and sets Matrix to identity.
2843Returns true if dst is empty, and sets Matrix to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002844
Cary Clark154beea2017-10-26 07:58:48 -04002845#Code
2846#Literal
2847| 0 0 0 |
2848| 0 0 0 |
2849| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002850##
2851
Cary Clark154beea2017-10-26 07:58:48 -04002852#Param src Rect to map from ##
2853#Param dst Rect to map to ##
2854#Param stf one of: kFill_ScaleToFit, kStart_ScaleToFit,
2855 kCenter_ScaleToFit, kEnd_ScaleToFit
Cary Clarkbc5697d2017-10-04 14:31:33 -04002856##
2857
Cary Clark154beea2017-10-26 07:58:48 -04002858#Return true if Matrix can represent Rect mapping ##
2859
Cary Clarkbc5697d2017-10-04 14:31:33 -04002860#Example
Cary Clark154beea2017-10-26 07:58:48 -04002861 const SkRect srcs[] = { {0, 0, 0, 0}, {1, 2, 3, 4} };
2862 const SkRect dsts[] = { {0, 0, 0, 0}, {5, 6, 8, 9} };
2863 for (auto src : srcs) {
2864 for (auto dst : dsts) {
2865 SkMatrix matrix;
2866 matrix.setAll(-1, -1, -1, -1, -1, -1, -1, -1, -1);
2867 bool success = matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
2868 SkDebugf("src: %g, %g, %g, %g dst: %g, %g, %g, %g success: %s\n",
2869 src.fLeft, src.fTop, src.fRight, src.fBottom,
2870 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, success ? "true" : "false");
2871 matrix.dump();
2872 }
2873 }
2874#StdOut
2875src: 0, 0, 0, 0 dst: 0, 0, 0, 0 success: false
2876[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2877src: 0, 0, 0, 0 dst: 5, 6, 8, 9 success: false
2878[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2879src: 1, 2, 3, 4 dst: 0, 0, 0, 0 success: true
2880[ 0.0000 0.0000 0.0000][ 0.0000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
2881src: 1, 2, 3, 4 dst: 5, 6, 8, 9 success: true
2882[ 1.5000 0.0000 3.5000][ 0.0000 1.5000 3.0000][ 0.0000 0.0000 1.0000]
2883##
Cary Clarkbc5697d2017-10-04 14:31:33 -04002884##
2885
Cary Clark682c58d2018-05-16 07:07:07 -04002886#SeeAlso MakeRectToRect ScaleToFit setPolyToPoly SkRect::isEmpty
Cary Clarkbc5697d2017-10-04 14:31:33 -04002887
2888##
2889
2890# ------------------------------------------------------------------------------
2891
2892#Method static SkMatrix MakeRectToRect(const SkRect& src, const SkRect& dst, ScaleToFit stf)
Cary Clark4855f782018-02-06 09:41:53 -05002893#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05002894#Line # constructs from source Rect to destination Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04002895Returns Matrix set to scale and translate src Rect to dst Rect. stf selects
2896whether mapping completely fills dst or preserves the aspect ratio, and how to
2897align src within dst. Returns the identity Matrix if src is empty. If dst is
2898empty, returns Matrix set to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04002899
Cary Clark154beea2017-10-26 07:58:48 -04002900#Code
2901#Literal
2902| 0 0 0 |
2903| 0 0 0 |
2904| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04002905##
2906
Cary Clark154beea2017-10-26 07:58:48 -04002907#Param src Rect to map from ##
2908#Param dst Rect to map to ##
2909#Param stf one of: kFill_ScaleToFit, kStart_ScaleToFit,
2910 kCenter_ScaleToFit, kEnd_ScaleToFit
2911##
2912
2913#Return Matrix mapping src to dst ##
2914
2915#Example
2916 const SkRect srcs[] = { {0, 0, 0, 0}, {1, 2, 3, 4} };
2917 const SkRect dsts[] = { {0, 0, 0, 0}, {5, 6, 8, 9} };
2918 for (auto src : srcs) {
2919 for (auto dst : dsts) {
2920 SkMatrix matrix = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
2921 SkDebugf("src: %g, %g, %g, %g dst: %g, %g, %g, %g\n",
2922 src.fLeft, src.fTop, src.fRight, src.fBottom,
2923 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom);
2924 matrix.dump();
2925 }
2926 }
2927#StdOut
2928src: 0, 0, 0, 0 dst: 0, 0, 0, 0
2929[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2930src: 0, 0, 0, 0 dst: 5, 6, 8, 9
2931[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
2932src: 1, 2, 3, 4 dst: 0, 0, 0, 0
2933[ 0.0000 0.0000 0.0000][ 0.0000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
2934src: 1, 2, 3, 4 dst: 5, 6, 8, 9
2935[ 1.5000 0.0000 3.5000][ 0.0000 1.5000 3.0000][ 0.0000 0.0000 1.0000]
2936##
2937##
2938
2939#SeeAlso setRectToRect ScaleToFit setPolyToPoly SkRect::isEmpty
Cary Clarkbc5697d2017-10-04 14:31:33 -04002940
2941##
2942
2943# ------------------------------------------------------------------------------
2944
2945#Method bool setPolyToPoly(const SkPoint src[], const SkPoint dst[], int count)
Cary Clark4855f782018-02-06 09:41:53 -05002946#In Set
Cary Clark08895c42018-02-01 09:37:32 -05002947#Line # sets to map one to four points to an equal array of points ##
Cary Clark154beea2017-10-26 07:58:48 -04002948Sets Matrix to map src to dst. count must be zero or greater, and four or less.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002949
Cary Clark154beea2017-10-26 07:58:48 -04002950If count is zero, sets Matrix to identity and returns true.
2951If count is one, sets Matrix to translate and returns true.
2952If count is two or more, sets Matrix to map Points if possible; returns false
2953if Matrix cannot be constructed. If count is four, Matrix may include
Cary Clark682c58d2018-05-16 07:07:07 -04002954perspective.
Cary Clarkbc5697d2017-10-04 14:31:33 -04002955
Cary Clark154beea2017-10-26 07:58:48 -04002956#Param src Points to map from ##
2957#Param dst Points to map to ##
2958#Param count number of Points in src and dst ##
2959
2960#Return true if Matrix was constructed successfully
Cary Clarkbc5697d2017-10-04 14:31:33 -04002961##
2962
2963#Example
Cary Clark154beea2017-10-26 07:58:48 -04002964 const SkPoint src[] = { { 0, 0}, {30, 0}, {30, -30}, { 0, -30} };
2965 const SkPoint dst[] = { {50, 0}, {80, -10}, {90, -30}, {60, -40} };
2966 SkPaint blackPaint;
2967 blackPaint.setAntiAlias(true);
2968 blackPaint.setTextSize(42);
2969 SkPaint redPaint = blackPaint;
2970 redPaint.setColor(SK_ColorRED);
2971 for (int count : { 1, 2, 3, 4 } ) {
2972 canvas->translate(35, 55);
2973 for (int index = 0; index < count; ++index) {
2974 canvas->drawCircle(src[index], 3, blackPaint);
2975 canvas->drawCircle(dst[index], 3, blackPaint);
2976 if (index > 0) {
2977 canvas->drawLine(src[index], src[index - 1], blackPaint);
2978 canvas->drawLine(dst[index], dst[index - 1], blackPaint);
2979 }
2980 }
2981 SkMatrix matrix;
2982 matrix.setPolyToPoly(src, dst, count);
2983 canvas->drawString("A", src[0].fX, src[0].fY, redPaint);
2984 SkAutoCanvasRestore acr(canvas, true);
2985 canvas->concat(matrix);
2986 canvas->drawString("A", src[0].fX, src[0].fY, redPaint);
2987 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04002988##
2989
Cary Clark154beea2017-10-26 07:58:48 -04002990#SeeAlso setRectToRect MakeRectToRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04002991
2992##
2993
2994# ------------------------------------------------------------------------------
2995
2996#Method bool SK_WARN_UNUSED_RESULT invert(SkMatrix* inverse) const
Cary Clark4855f782018-02-06 09:41:53 -05002997#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05002998#Line # returns inverse, if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04002999Sets inverse to reciprocal matrix, returning true if Matrix can be inverted.
3000Geometrically, if Matrix maps from source to destination, inverse Matrix
3001maps from destination to source. If Matrix can not be inverted, inverse is
3002unchanged.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003003
Cary Clark154beea2017-10-26 07:58:48 -04003004#Param inverse storage for inverted Matrix; may be nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003005
Cary Clark154beea2017-10-26 07:58:48 -04003006#Return true if Matrix can be inverted ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003007
3008#Example
Cary Clark154beea2017-10-26 07:58:48 -04003009#Height 128
3010 const SkPoint src[] = { { 10, 120}, {120, 120}, {120, 10}, { 10, 10} };
3011 const SkPoint dst[] = { {150, 120}, {200, 100}, {240, 30}, { 130, 40} };
3012 SkPaint paint;
3013 paint.setAntiAlias(true);
3014 SkMatrix matrix;
3015 matrix.setPolyToPoly(src, dst, 4);
3016 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, src, paint);
3017 canvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, dst, paint);
3018 paint.setColor(SK_ColorBLUE);
3019 paint.setStrokeWidth(3);
3020 paint.setStrokeCap(SkPaint::kRound_Cap);
3021 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, dst, paint);
Cary Clark681287e2018-03-16 11:34:15 -04003022 if (matrix.invert(&matrix)) {
3023 canvas->concat(matrix);
3024 canvas->drawPoints(SkCanvas::kPoints_PointMode, 4, dst, paint);
3025 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003026##
3027
Cary Clark154beea2017-10-26 07:58:48 -04003028#SeeAlso Concat
Cary Clarkbc5697d2017-10-04 14:31:33 -04003029
3030##
3031
3032# ------------------------------------------------------------------------------
3033
3034#Method static void SetAffineIdentity(SkScalar affine[6])
Cary Clark4855f782018-02-06 09:41:53 -05003035#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05003036#Line # sets 3x2 array to identity ##
Cary Clark154beea2017-10-26 07:58:48 -04003037Fills affine with identity values in column major order.
3038Sets affine to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003039
Cary Clark154beea2017-10-26 07:58:48 -04003040#Code
3041#Literal
3042| 1 0 0 |
3043| 0 1 0 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003044##
3045
Cary Clark154beea2017-10-26 07:58:48 -04003046Affine 3x2 matrices in column major order are used by OpenGL and XPS.
3047
3048#Param affine storage for 3x2 affine matrix ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003049
3050#Example
Cary Clark154beea2017-10-26 07:58:48 -04003051 SkScalar affine[6];
3052 SkMatrix::SetAffineIdentity(affine);
3053 const char* names[] = { "ScaleX", "SkewY", "SkewX", "ScaleY", "TransX", "TransY" };
3054 for (int i = 0; i < 6; ++i) {
3055 SkDebugf("%s: %g ", names[i], affine[i]);
3056 }
3057 SkDebugf("\n");
3058#StdOut
3059ScaleX: 1 SkewY: 0 SkewX: 0 ScaleY: 1 TransX: 0 TransY: 0
3060##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003061##
3062
Cary Clark154beea2017-10-26 07:58:48 -04003063#SeeAlso setAffine asAffine
Cary Clarkbc5697d2017-10-04 14:31:33 -04003064
3065##
3066
3067# ------------------------------------------------------------------------------
3068
3069#Method bool SK_WARN_UNUSED_RESULT asAffine(SkScalar affine[6]) const
Cary Clark4855f782018-02-06 09:41:53 -05003070#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05003071#Line # copies to 3x2 array ##
Cary Clark154beea2017-10-26 07:58:48 -04003072Fills affine in column major order. Sets affine to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003073
Cary Clark154beea2017-10-26 07:58:48 -04003074#Code
3075#Literal
3076| scale-x skew-x translate-x |
3077| skew-y scale-y translate-y |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003078##
3079
Cary Clark154beea2017-10-26 07:58:48 -04003080If Matrix contains perspective, returns false and leaves affine unchanged.
3081
3082#Param affine storage for 3x2 affine matrix; may be nullptr ##
3083
3084#Return true if Matrix does not contain perspective ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003085
3086#Example
Cary Clark154beea2017-10-26 07:58:48 -04003087SkMatrix matrix;
3088matrix.setAll(2, 3, 4, 5, 6, 7, 0, 0, 1);
3089SkScalar affine[6];
Cary Clark681287e2018-03-16 11:34:15 -04003090if (matrix.asAffine(affine)) {
3091 const char* names[] = { "ScaleX", "SkewY", "SkewX", "ScaleY", "TransX", "TransY" };
3092 for (int i = 0; i < 6; ++i) {
3093 SkDebugf("%s: %g ", names[i], affine[i]);
3094 }
3095 SkDebugf("\n");
Cary Clark154beea2017-10-26 07:58:48 -04003096}
Cary Clark154beea2017-10-26 07:58:48 -04003097#StdOut
Cary Clark682c58d2018-05-16 07:07:07 -04003098ScaleX: 2 SkewY: 5 SkewX: 3 ScaleY: 6 TransX: 4 TransY: 7
Cary Clark154beea2017-10-26 07:58:48 -04003099##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003100##
3101
Cary Clark154beea2017-10-26 07:58:48 -04003102#SeeAlso setAffine SetAffineIdentity
Cary Clarkbc5697d2017-10-04 14:31:33 -04003103
3104##
3105
3106# ------------------------------------------------------------------------------
3107
3108#Method void setAffine(const SkScalar affine[6])
Cary Clark4855f782018-02-06 09:41:53 -05003109#In Constructor
3110#In Set
Cary Clark08895c42018-02-01 09:37:32 -05003111#Line # sets left two columns ##
Cary Clark154beea2017-10-26 07:58:48 -04003112Sets Matrix to affine values, passed in column major order. Given affine,
3113column, then row, as:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003114
Cary Clark154beea2017-10-26 07:58:48 -04003115#Code
3116#Literal
3117| scale-x skew-x translate-x |
3118| skew-y scale-y translate-y |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003119##
3120
Cary Clark154beea2017-10-26 07:58:48 -04003121Matrix is set, row, then column, to:
3122
3123#Code
3124#Literal
3125| scale-x skew-x translate-x |
3126| skew-y scale-y translate-y |
3127| 0 0 1 |
3128##
3129
3130#Param affine 3x2 affine matrix ##
3131
3132#Example
3133SkMatrix matrix;
3134matrix.setAll(2, 3, 4, 5, 6, 7, 0, 0, 1);
3135SkScalar affine[6];
Cary Clark681287e2018-03-16 11:34:15 -04003136if (matrix.asAffine(affine)) {
3137 const char* names[] = { "ScaleX", "SkewY", "SkewX", "ScaleY", "TransX", "TransY" };
3138 for (int i = 0; i < 6; ++i) {
3139 SkDebugf("%s: %g ", names[i], affine[i]);
3140 }
3141 SkDebugf("\n");
3142 matrix.reset();
3143 matrix.setAffine(affine);
3144 matrix.dump();
Cary Clark154beea2017-10-26 07:58:48 -04003145}
Cary Clark154beea2017-10-26 07:58:48 -04003146#StdOut
Cary Clark682c58d2018-05-16 07:07:07 -04003147ScaleX: 2 SkewY: 5 SkewX: 3 ScaleY: 6 TransX: 4 TransY: 7
Cary Clark154beea2017-10-26 07:58:48 -04003148[ 2.0000 3.0000 4.0000][ 5.0000 6.0000 7.0000][ 0.0000 0.0000 1.0000]
3149##
3150##
3151
3152#SeeAlso asAffine SetAffineIdentity
Cary Clarkbc5697d2017-10-04 14:31:33 -04003153
3154##
3155
3156# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05003157#Subtopic Transform
3158#Populate
3159#Line # map points with Matrix ##
3160##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003161
3162#Method void mapPoints(SkPoint dst[], const SkPoint src[], int count) const
Cary Clark4855f782018-02-06 09:41:53 -05003163#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003164#Line # maps Point array ##
Cary Clark154beea2017-10-26 07:58:48 -04003165Maps src Point array of length count to dst Point array of equal or greater
3166length. Points are mapped by multiplying each Point by Matrix. Given:
3167
3168#Code
3169#Literal
3170 | A B C | | x |
3171Matrix = | D E F |, pt = | y |
3172 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003173##
3174
Cary Clark682c58d2018-05-16 07:07:07 -04003175where
Cary Clark154beea2017-10-26 07:58:48 -04003176
3177#Code
3178#Literal
3179for (i = 0; i < count; ++i) {
3180 x = src[i].fX
3181 y = src[i].fY
3182}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003183##
Cary Clark154beea2017-10-26 07:58:48 -04003184
3185each dst Point is computed as:
3186
3187#Code
3188#Literal
3189 |A B C| |x| Ax+By+C Dx+Ey+F
3190Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3191 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003192##
Cary Clark154beea2017-10-26 07:58:48 -04003193
3194src and dst may point to the same storage.
3195
3196#Param dst storage for mapped Points ##
3197#Param src Points to transform ##
3198#Param count number of Points to transform ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003199
3200#Example
Cary Clark154beea2017-10-26 07:58:48 -04003201 SkMatrix matrix;
3202 matrix.reset();
3203 const int count = 4;
3204 SkPoint src[count];
3205 matrix.mapRectToQuad(src, {40, 70, 180, 220} );
3206 SkPaint paint;
3207 paint.setARGB(77, 23, 99, 154);
3208 for (int i = 0; i < 5; ++i) {
3209 SkPoint dst[count];
3210 matrix.mapPoints(dst, src, count);
3211 canvas->drawPoints(SkCanvas::kPolygon_PointMode, count, dst, paint);
3212 matrix.preRotate(35, 128, 128);
3213 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003214##
3215
Brian Salomonfa3783f2018-01-05 13:49:07 -05003216#SeeAlso mapXY mapHomogeneousPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003217
3218##
3219
3220# ------------------------------------------------------------------------------
3221
3222#Method void mapPoints(SkPoint pts[], int count) const
3223
Cary Clark154beea2017-10-26 07:58:48 -04003224Maps pts Point array of length count in place. Points are mapped by multiplying
3225each Point by Matrix. Given:
3226
3227#Code
3228#Literal
3229 | A B C | | x |
3230Matrix = | D E F |, pt = | y |
3231 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003232##
3233
Cary Clark682c58d2018-05-16 07:07:07 -04003234where
Cary Clark154beea2017-10-26 07:58:48 -04003235
3236#Code
3237#Literal
3238for (i = 0; i < count; ++i) {
3239 x = pts[i].fX
3240 y = pts[i].fY
3241}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003242##
Cary Clark154beea2017-10-26 07:58:48 -04003243
3244each resulting pts Point is computed as:
3245
3246#Code
3247#Literal
3248 |A B C| |x| Ax+By+C Dx+Ey+F
3249Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3250 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003251##
3252
Cary Clark154beea2017-10-26 07:58:48 -04003253#Param pts storage for mapped Points ##
3254#Param count number of Points to transform ##
3255
Cary Clarkbc5697d2017-10-04 14:31:33 -04003256#Example
Cary Clark154beea2017-10-26 07:58:48 -04003257 SkMatrix matrix;
3258 matrix.setRotate(35, 128, 128);
3259 const int count = 4;
3260 SkPoint pts[count];
3261 matrix.mapRectToQuad(pts, {40, 70, 180, 220} );
3262 SkPaint paint;
3263 paint.setARGB(77, 23, 99, 154);
3264 for (int i = 0; i < 5; ++i) {
3265 canvas->drawPoints(SkCanvas::kPolygon_PointMode, count, pts, paint);
3266 matrix.mapPoints(pts, count);
3267 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003268##
3269
Brian Salomonfa3783f2018-01-05 13:49:07 -05003270#SeeAlso mapXY mapHomogeneousPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003271
3272##
3273
3274# ------------------------------------------------------------------------------
3275
Cary Clark154beea2017-10-26 07:58:48 -04003276#Method void mapHomogeneousPoints(SkPoint3 dst[], const SkPoint3 src[], int count) const
Cary Clark4855f782018-02-06 09:41:53 -05003277#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003278#Line # maps Point3 array ##
Cary Clark154beea2017-10-26 07:58:48 -04003279Maps src Point3 array of length count to dst Point3 array, which must of length count or
3280greater. Point3 array is mapped by multiplying each Point3 by Matrix. Given:
3281
3282#Code
3283#Literal
3284 | A B C | | x |
3285Matrix = | D E F |, src = | y |
3286 | G H I | | z |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003287##
3288
Cary Clark154beea2017-10-26 07:58:48 -04003289each resulting dst Point is computed as:
3290
3291#Code
3292#Literal
3293 |A B C| |x|
3294Matrix * src = |D E F| |y| = |Ax+By+Cz Dx+Ey+Fz Gx+Hy+Iz|
3295 |G H I| |z|
Cary Clarkbc5697d2017-10-04 14:31:33 -04003296##
Cary Clark154beea2017-10-26 07:58:48 -04003297
3298#Param dst storage for mapped Point3 array ##
3299#Param src Point3 array to transform ##
3300#Param count items in Point3 array to transform ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003301
3302#Example
Cary Clark154beea2017-10-26 07:58:48 -04003303 SkPoint3 src[] = {{3, 3, 1}, {8, 2, 2}, {5, 0, 4}, {0, 1, 3},
3304 {3, 7, 1}, {8, 6, 2}, {5, 4, 4}, {0, 5, 3}};
3305 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 };
3306 constexpr int count = SK_ARRAY_COUNT(src);
3307 auto debugster = [=](SkPoint3 src[]) -> void {
3308 for (size_t i = 0; i < SK_ARRAY_COUNT(lines); i += 2) {
3309 const SkPoint3& s = src[lines[i]];
3310 const SkPoint3& e = src[lines[i + 1]];
3311 SkPaint paint;
3312 paint.setARGB(77, 23, 99, 154);
3313 canvas->drawLine(s.fX / s.fZ, s.fY / s.fZ, e.fX / e.fZ, e.fY / e.fZ, paint);
3314 }
3315 };
3316 canvas->save();
3317 canvas->translate(5, 5);
3318 canvas->scale(15, 15);
3319 debugster(src);
3320 canvas->restore();
3321 canvas->translate(128, 128);
3322 SkMatrix matrix;
3323 matrix.setAll(15, 0, 0, 0, 15, 0, -0.08, 0.04, 1);
Cary Clarka560c472017-11-27 10:44:06 -05003324 matrix.mapHomogeneousPoints(src, src, count);
Cary Clark154beea2017-10-26 07:58:48 -04003325 debugster(src);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003326##
3327
Brian Salomonfa3783f2018-01-05 13:49:07 -05003328#SeeAlso mapPoints mapXY mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003329
3330##
3331
3332# ------------------------------------------------------------------------------
3333
3334#Method void mapXY(SkScalar x, SkScalar y, SkPoint* result) const
Cary Clark4855f782018-02-06 09:41:53 -05003335#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003336#Line # maps Point ##
Cary Clark154beea2017-10-26 07:58:48 -04003337Maps Point (x, y) to result. Point is mapped by multiplying by Matrix. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003338
Cary Clark154beea2017-10-26 07:58:48 -04003339#Code
3340#Literal
3341 | A B C | | x |
3342Matrix = | D E F |, pt = | y |
3343 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003344##
3345
Cary Clark154beea2017-10-26 07:58:48 -04003346result is computed as:
3347
3348#Code
3349#Literal
3350 |A B C| |x| Ax+By+C Dx+Ey+F
3351Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3352 |G H I| |1| Gx+Hy+I Gx+Hy+I
3353##
3354
Cary Clark5538c132018-06-14 12:28:14 -04003355#Param x x-axis value of Point to map ##
3356#Param y y-axis value of Point to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003357#Param result storage for mapped Point ##
3358
3359#Example
3360 SkPaint paint;
3361 paint.setAntiAlias(true);
3362 SkMatrix matrix;
3363 matrix.setRotate(60, 128, 128);
3364 SkPoint lines[] = {{50, 50}, {150, 50}, {150, 150}};
3365 for (size_t i = 0; i < SK_ARRAY_COUNT(lines); ++i) {
3366 SkPoint pt;
3367 matrix.mapXY(lines[i].fX, lines[i].fY, &pt);
3368 canvas->drawCircle(pt.fX, pt.fY, 3, paint);
3369 }
3370 canvas->concat(matrix);
3371 canvas->drawPoints(SkCanvas::kPolygon_PointMode, SK_ARRAY_COUNT(lines), lines, paint);
3372##
3373
Brian Salomonfa3783f2018-01-05 13:49:07 -05003374#SeeAlso mapPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003375
3376##
3377
3378# ------------------------------------------------------------------------------
3379
3380#Method SkPoint mapXY(SkScalar x, SkScalar y) const
3381
Cary Clark154beea2017-10-26 07:58:48 -04003382Returns Point (x, y) multiplied by Matrix. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003383
Cary Clark154beea2017-10-26 07:58:48 -04003384#Code
3385#Literal
3386 | A B C | | x |
3387Matrix = | D E F |, pt = | y |
3388 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003389##
3390
Cary Clark154beea2017-10-26 07:58:48 -04003391result is computed as:
3392
3393#Code
3394#Literal
3395 |A B C| |x| Ax+By+C Dx+Ey+F
3396Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3397 |G H I| |1| Gx+Hy+I Gx+Hy+I
3398##
3399
Cary Clark5538c132018-06-14 12:28:14 -04003400#Param x x-axis value of Point to map ##
3401#Param y y-axis value of Point to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003402
3403#Return mapped Point ##
3404
3405#Example
3406#Image 4
3407SkMatrix matrix;
3408SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {30, 206}};
3409SkRect::Make(source.bounds()).toQuad(bitmapBounds);
3410matrix.setPolyToPoly(bitmapBounds, perspect, 4);
3411SkPaint paint;
3412paint.setAntiAlias(true);
3413paint.setStrokeWidth(3);
3414for (int x : { 0, source.width() } ) {
3415 for (int y : { 0, source.height() } ) {
3416 canvas->drawPoint(matrix.mapXY(x, y), paint);
3417 }
3418}
3419canvas->concat(matrix);
3420canvas->drawBitmap(source, 0, 0);
3421##
3422
Brian Salomonfa3783f2018-01-05 13:49:07 -05003423#SeeAlso mapPoints mapVectors
Cary Clarkbc5697d2017-10-04 14:31:33 -04003424
3425##
3426
3427# ------------------------------------------------------------------------------
3428
3429#Method void mapVectors(SkVector dst[], const SkVector src[], int count) const
Cary Clark4855f782018-02-06 09:41:53 -05003430#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003431#Line # maps Vector array ##
Cary Clark154beea2017-10-26 07:58:48 -04003432Maps src Vector array of length count to Vector Point array of equal or greater
3433length. Vectors are mapped by multiplying each Vector by Matrix, treating
3434Matrix translation as zero. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003435
Cary Clark154beea2017-10-26 07:58:48 -04003436#Code
3437#Literal
3438 | A B 0 | | x |
3439Matrix = | D E 0 |, src = | y |
3440 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003441##
Cary Clark154beea2017-10-26 07:58:48 -04003442
Cary Clark682c58d2018-05-16 07:07:07 -04003443where
Cary Clark154beea2017-10-26 07:58:48 -04003444
3445#Code
3446#Literal
3447for (i = 0; i < count; ++i) {
3448 x = src[i].fX
3449 y = src[i].fY
3450}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003451##
Cary Clark154beea2017-10-26 07:58:48 -04003452
3453each dst Vector is computed as:
3454
3455#Code
3456#Literal
3457 |A B 0| |x| Ax+By Dx+Ey
3458Matrix * src = |D E 0| |y| = |Ax+By Dx+Ey Gx+Hy+I| = ------- , -------
3459 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003460##
3461
Cary Clark154beea2017-10-26 07:58:48 -04003462src and dst may point to the same storage.
3463
3464#Param dst storage for mapped Vectors ##
3465#Param src Vectors to transform ##
3466#Param count number of Vectors to transform ##
3467
Cary Clarkbc5697d2017-10-04 14:31:33 -04003468#Example
Cary Clark154beea2017-10-26 07:58:48 -04003469 SkPaint paint;
3470 paint.setAntiAlias(true);
3471 paint.setStyle(SkPaint::kStroke_Style);
3472 SkMatrix matrix;
3473 matrix.reset();
3474 const SkVector radii[] = {{8, 4}, {9, 1}, {6, 2}, {7, 3}};
3475 for (int i = 0; i < 4; ++i) {
3476 SkVector rScaled[4];
3477 matrix.preScale(1.5f, 2.f);
3478 matrix.mapVectors(rScaled, radii, SK_ARRAY_COUNT(radii));
3479 SkRRect rrect;
3480 rrect.setRectRadii({20, 20, 180, 70}, rScaled);
3481 canvas->drawRRect(rrect, paint);
3482 canvas->translate(0, 60);
3483 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003484##
3485
Brian Salomonfa3783f2018-01-05 13:49:07 -05003486#SeeAlso mapVector mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003487
3488##
3489
3490# ------------------------------------------------------------------------------
3491
3492#Method void mapVectors(SkVector vecs[], int count) const
3493
Cary Clark154beea2017-10-26 07:58:48 -04003494Maps vecs Vector array of length count in place, multiplying each Vector by
3495Matrix, treating Matrix translation as zero. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003496
Cary Clark154beea2017-10-26 07:58:48 -04003497#Code
3498#Literal
3499 | A B 0 | | x |
3500Matrix = | D E 0 |, vec = | y |
3501 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003502##
Cary Clark154beea2017-10-26 07:58:48 -04003503
Cary Clark682c58d2018-05-16 07:07:07 -04003504where
Cary Clark154beea2017-10-26 07:58:48 -04003505
3506#Code
3507#Literal
3508for (i = 0; i < count; ++i) {
3509 x = vecs[i].fX
3510 y = vecs[i].fY
3511}
Cary Clarkbc5697d2017-10-04 14:31:33 -04003512##
3513
Cary Clark154beea2017-10-26 07:58:48 -04003514each result Vector is computed as:
3515
3516#Code
3517#Literal
3518 |A B 0| |x| Ax+By Dx+Ey
3519Matrix * vec = |D E 0| |y| = |Ax+By Dx+Ey Gx+Hy+I| = ------- , -------
3520 |G H I| |1| Gx+Hy+I Gx+Hy+I
3521##
3522
3523#Param vecs Vectors to transform, and storage for mapped Vectors ##
3524#Param count number of Vectors to transform ##
3525
Cary Clarkbc5697d2017-10-04 14:31:33 -04003526#Example
Cary Clark154beea2017-10-26 07:58:48 -04003527 SkPaint paint;
3528 paint.setAntiAlias(true);
3529 paint.setStyle(SkPaint::kStroke_Style);
3530 SkMatrix matrix;
3531 matrix.setScale(2, 3);
3532 SkVector radii[] = {{7, 7}, {3, 3}, {2, 2}, {4, 0}};
3533 for (int i = 0; i < 4; ++i) {
3534 SkRRect rrect;
3535 rrect.setRectRadii({20, 20, 180, 70}, radii);
3536 canvas->drawRRect(rrect, paint);
3537 canvas->translate(0, 60);
3538 matrix.mapVectors(radii, SK_ARRAY_COUNT(radii));
3539 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003540##
3541
Brian Salomonfa3783f2018-01-05 13:49:07 -05003542#SeeAlso mapVector mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003543
3544##
3545
3546# ------------------------------------------------------------------------------
3547
3548#Method void mapVector(SkScalar dx, SkScalar dy, SkVector* result) const
Cary Clark4855f782018-02-06 09:41:53 -05003549#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003550#Line # maps Vector ##
Cary Clark154beea2017-10-26 07:58:48 -04003551Maps Vector (x, y) to result. Vector is mapped by multiplying by Matrix,
3552treating Matrix translation as zero. Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003553
Cary Clark154beea2017-10-26 07:58:48 -04003554#Code
3555#Literal
3556 | A B 0 | | dx |
3557Matrix = | D E 0 |, vec = | dy |
3558 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003559##
3560
Cary Clark154beea2017-10-26 07:58:48 -04003561each result Vector is computed as:
3562
3563#Code
3564#Literal
3565#Outdent
3566 |A B 0| |dx| A*dx+B*dy D*dx+E*dy
3567Matrix * vec = |D E 0| |dy| = |A*dx+B*dy D*dx+E*dy G*dx+H*dy+I| = ----------- , -----------
3568 |G H I| | 1| G*dx+H*dy+I G*dx+*dHy+I
3569##
3570
Cary Clark5538c132018-06-14 12:28:14 -04003571#Param dx x-axis value of Vector to map ##
3572#Param dy y-axis value of Vector to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003573#Param result storage for mapped Vector ##
3574
3575#Example
3576 SkPaint paint;
3577 paint.setColor(SK_ColorGREEN);
3578 paint.setAntiAlias(true);
3579 paint.setTextSize(48);
3580 SkMatrix matrix;
3581 matrix.setRotate(90);
3582 SkVector offset = { 7, 7 };
3583 for (int i = 0; i < 4; ++i) {
3584 paint.setImageFilter(SkDropShadowImageFilter::Make(offset.fX, offset.fY, 3, 3,
3585 SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr));
3586 matrix.mapVector(offset.fX, offset.fY, &offset);
3587 canvas->translate(0, 60);
3588 canvas->drawString("Text", 50, 0, paint);
3589 }
3590##
3591
Brian Salomonfa3783f2018-01-05 13:49:07 -05003592#SeeAlso mapVectors mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003593
3594##
3595
3596# ------------------------------------------------------------------------------
3597
3598#Method SkVector mapVector(SkScalar dx, SkScalar dy) const
3599
Cary Clark154beea2017-10-26 07:58:48 -04003600Returns Vector (x, y) multiplied by Matrix, treating Matrix translation as zero.
3601Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04003602
Cary Clark154beea2017-10-26 07:58:48 -04003603#Code
3604#Literal
3605 | A B 0 | | dx |
3606Matrix = | D E 0 |, vec = | dy |
3607 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003608##
3609
Cary Clark154beea2017-10-26 07:58:48 -04003610each result Vector is computed as:
3611
3612#Code
3613#Literal
3614#Outdent
3615 |A B 0| |dx| A*dx+B*dy D*dx+E*dy
3616Matrix * vec = |D E 0| |dy| = |A*dx+B*dy D*dx+E*dy G*dx+H*dy+I| = ----------- , -----------
3617 |G H I| | 1| G*dx+H*dy+I G*dx+*dHy+I
3618##
3619
Cary Clark5538c132018-06-14 12:28:14 -04003620#Param dx x-axis value of Vector to map ##
3621#Param dy y-axis value of Vector to map ##
Cary Clark154beea2017-10-26 07:58:48 -04003622
3623#Return mapped Vector ##
3624
3625#Example
3626 SkPaint paint;
3627 paint.setColor(SK_ColorGREEN);
3628 paint.setAntiAlias(true);
3629 paint.setTextSize(48);
3630 SkMatrix matrix;
3631 matrix.setRotate(90);
3632 SkVector offset = { 7, 7 };
3633 for (int i = 0; i < 4; ++i) {
3634 paint.setImageFilter(SkDropShadowImageFilter::Make(offset.fX, offset.fY, 3, 3,
3635 SK_ColorBLUE, SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr));
3636 offset = matrix.mapVector(offset.fX, offset.fY);
3637 canvas->translate(0, 60);
3638 canvas->drawString("Text", 50, 0, paint);
3639 }
3640##
3641
Brian Salomonfa3783f2018-01-05 13:49:07 -05003642#SeeAlso mapVectors mapPoints mapXY
Cary Clarkbc5697d2017-10-04 14:31:33 -04003643
3644##
3645
3646# ------------------------------------------------------------------------------
3647
3648#Method bool mapRect(SkRect* dst, const SkRect& src) const
Cary Clark4855f782018-02-06 09:41:53 -05003649#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003650#Line # returns bounds of mapped Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04003651Sets dst to bounds of src corners mapped by Matrix.
3652Returns true if mapped corners are dst corners.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003653
Cary Clark154beea2017-10-26 07:58:48 -04003654Returned value is the same as calling rectStaysRect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003655
Cary Clark154beea2017-10-26 07:58:48 -04003656#Param dst storage for bounds of mapped Points ##
3657#Param src Rect to map ##
3658
3659#Return true if dst is equivalent to mapped src ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003660
3661#Example
Cary Clark154beea2017-10-26 07:58:48 -04003662 SkPaint paint;
3663 paint.setAntiAlias(true);
3664 SkMatrix matrix;
3665 matrix.setRotate(45, 128, 128);
3666 SkRect rotatedBounds, bounds = {40, 50, 190, 200};
3667 matrix.mapRect(&rotatedBounds, bounds );
3668 paint.setColor(SK_ColorGRAY);
3669 canvas->drawRect(rotatedBounds, paint);
3670 canvas->concat(matrix);
3671 paint.setColor(SK_ColorRED);
3672 canvas->drawRect(bounds, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003673##
3674
Cary Clark154beea2017-10-26 07:58:48 -04003675#SeeAlso mapPoints rectStaysRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04003676
3677##
3678
3679# ------------------------------------------------------------------------------
3680
3681#Method bool mapRect(SkRect* rect) const
3682
Cary Clark154beea2017-10-26 07:58:48 -04003683Sets rect to bounds of rect corners mapped by Matrix.
3684Returns true if mapped corners are computed rect corners.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003685
Cary Clark154beea2017-10-26 07:58:48 -04003686Returned value is the same as calling rectStaysRect.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003687
Cary Clark154beea2017-10-26 07:58:48 -04003688#Param rect rectangle to map, and storage for bounds of mapped corners ##
3689
3690#Return true if result is equivalent to mapped src ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003691
3692#Example
Cary Clark154beea2017-10-26 07:58:48 -04003693 SkPaint paint;
3694 paint.setAntiAlias(true);
3695 SkMatrix matrix;
3696 matrix.setRotate(45, 128, 128);
3697 SkRect bounds = {40, 50, 190, 200};
3698 matrix.mapRect(&bounds);
3699 paint.setColor(SK_ColorGRAY);
3700 canvas->drawRect(bounds, paint);
3701 canvas->concat(matrix);
3702 paint.setColor(SK_ColorRED);
3703 canvas->drawRect({40, 50, 190, 200}, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003704##
3705
Cary Clark154beea2017-10-26 07:58:48 -04003706#SeeAlso mapRectScaleTranslate mapPoints rectStaysRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04003707
3708##
3709
3710# ------------------------------------------------------------------------------
3711
Cary Clarkc06754b2018-05-16 21:28:55 -04003712#Method SkRect mapRect(const SkRect& src) const
3713
3714Returns bounds of src corners mapped by Matrix.
3715
3716#Param src rectangle to map ##
3717#Return mapped bounds ##
3718
3719#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04003720 SkRect rect{110, 50, 180, 100};
3721 SkMatrix matrix;
3722 matrix.setRotate(50, 28, 28);
3723 SkRect mapped = matrix.mapRect(rect);
3724 SkPaint paint;
3725 paint.setAntiAlias(true);
3726 paint.setStyle(SkPaint::kStroke_Style);
3727 canvas->drawRect(rect, paint);
3728 canvas->drawRect(mapped, paint);
3729 canvas->concat(matrix);
3730 canvas->drawRect(rect, paint);
Cary Clarkc06754b2018-05-16 21:28:55 -04003731##
3732
Cary Clarkffb3d682018-05-17 12:17:28 -04003733#SeeAlso mapRectToQuad mapRectScaleTranslate
Cary Clarkc06754b2018-05-16 21:28:55 -04003734#Method ##
3735
3736# ------------------------------------------------------------------------------
3737
Cary Clarkbc5697d2017-10-04 14:31:33 -04003738#Method void mapRectToQuad(SkPoint dst[4], const SkRect& rect) const
Cary Clark4855f782018-02-06 09:41:53 -05003739#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003740#Line # maps Rect to Point array ##
Cary Clark154beea2017-10-26 07:58:48 -04003741Maps four corners of rect to dst. Points are mapped by multiplying each
3742rect corner by Matrix. rect corner is processed in this order:
3743(rect.fLeft, rect.fTop), (rect.fRight, rect.fTop), (rect.fRight, rect.fBottom),
Cary Clark682c58d2018-05-16 07:07:07 -04003744(rect.fLeft, rect.fBottom).
Cary Clarkbc5697d2017-10-04 14:31:33 -04003745
Cary Clark154beea2017-10-26 07:58:48 -04003746rect may be empty: rect.fLeft may be greater than or equal to rect.fRight;
3747rect.fTop may be greater than or equal to rect.fBottom.
3748
3749Given:
3750
3751#Code
3752#Literal
3753 | A B C | | x |
3754Matrix = | D E F |, pt = | y |
3755 | G H I | | 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04003756##
Cary Clark154beea2017-10-26 07:58:48 -04003757
3758where pt is initialized from each of (rect.fLeft, rect.fTop),
3759(rect.fRight, rect.fTop), (rect.fRight, rect.fBottom), (rect.fLeft, rect.fBottom),
3760each dst Point is computed as:
3761
3762#Code
3763#Literal
3764 |A B C| |x| Ax+By+C Dx+Ey+F
3765Matrix * pt = |D E F| |y| = |Ax+By+C Dx+Ey+F Gx+Hy+I| = ------- , -------
3766 |G H I| |1| Gx+Hy+I Gx+Hy+I
Cary Clarkbc5697d2017-10-04 14:31:33 -04003767##
3768
Cary Clark154beea2017-10-26 07:58:48 -04003769#Param dst storage for mapped corner Points ##
3770#Param rect Rect to map ##
3771
Cary Clarkbc5697d2017-10-04 14:31:33 -04003772#Example
Cary Clark2ade9972017-11-02 17:49:34 -04003773#Height 192
Cary Clark154beea2017-10-26 07:58:48 -04003774 SkPaint paint;
3775 paint.setAntiAlias(true);
3776 SkMatrix matrix;
3777 matrix.setRotate(60, 128, 128);
3778 SkRect rect = {50, 50, 150, 150};
3779 SkPoint pts[4];
3780 matrix.mapRectToQuad(pts, rect);
3781 for (int i = 0; i < 4; ++i) {
3782 canvas->drawCircle(pts[i].fX, pts[i].fY, 3, paint);
3783 }
3784 canvas->concat(matrix);
3785 paint.setStyle(SkPaint::kStroke_Style);
3786 canvas->drawRect(rect, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003787##
3788
Cary Clark154beea2017-10-26 07:58:48 -04003789#SeeAlso mapRect mapRectScaleTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -04003790
3791##
3792
3793# ------------------------------------------------------------------------------
3794
3795#Method void mapRectScaleTranslate(SkRect* dst, const SkRect& src) const
Cary Clark4855f782018-02-06 09:41:53 -05003796#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003797#Line # returns bounds of mapped Rect ##
Cary Clark154beea2017-10-26 07:58:48 -04003798Sets dst to bounds of src corners mapped by Matrix. If matrix contains
3799elements other than scale or translate: asserts if SK_DEBUG is defined;
3800otherwise, results are undefined.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003801
Cary Clark154beea2017-10-26 07:58:48 -04003802#Param dst storage for bounds of mapped Points ##
3803#Param src Rect to map ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003804
3805#Example
Cary Clark154beea2017-10-26 07:58:48 -04003806 SkPaint paint;
3807 SkMatrix matrix;
3808 SkRect rect = {100, 50, 150, 180};
3809 matrix.setScale(2, .5f, rect.centerX(), rect.centerY());
3810 SkRect rotated;
3811 matrix.mapRectScaleTranslate(&rotated, rect);
3812 paint.setStyle(SkPaint::kStroke_Style);
3813 canvas->drawRect(rect, paint);
3814 paint.setColor(SK_ColorRED);
3815 canvas->drawRect(rotated, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003816##
3817
Cary Clark154beea2017-10-26 07:58:48 -04003818#SeeAlso mapRect mapRectToQuad isScaleTranslate rectStaysRect
Cary Clarkbc5697d2017-10-04 14:31:33 -04003819
3820##
3821
3822# ------------------------------------------------------------------------------
3823
3824#Method SkScalar mapRadius(SkScalar radius) const
Cary Clark4855f782018-02-06 09:41:53 -05003825#In Transform
Cary Clark08895c42018-02-01 09:37:32 -05003826#Line # returns mean radius of mapped Circle ##
Cary Clark154beea2017-10-26 07:58:48 -04003827Returns geometric mean radius of ellipse formed by constructing Circle of
3828size radius, and mapping constructed Circle with Matrix. The result squared is
3829equal to the major axis length times the minor axis length.
3830Result is not meaningful if Matrix contains perspective elements.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003831
Cary Clark154beea2017-10-26 07:58:48 -04003832#Param radius Circle size to map ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003833
Cary Clark154beea2017-10-26 07:58:48 -04003834#Return average mapped radius ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003835
3836#Example
Cary Clark154beea2017-10-26 07:58:48 -04003837#Description
3838The area enclosed by a square with sides equal to mappedRadius is the same as
3839the area enclosed by the ellipse major and minor axes.
3840##
3841 SkPaint paint;
3842 paint.setAntiAlias(true);
3843 SkMatrix matrix;
3844 const SkPoint center = {108, 93};
3845 matrix.setScale(2, .5f, center.fX, center.fY);
3846 matrix.postRotate(45, center.fX, center.fY);
3847 const SkScalar circleRadius = 50;
3848 SkScalar mappedRadius = matrix.mapRadius(circleRadius);
3849 SkVector minorAxis, majorAxis;
3850 matrix.mapVector(0, circleRadius, &minorAxis);
3851 matrix.mapVector(circleRadius, 0, &majorAxis);
3852 SkString mappedArea;
3853 mappedArea.printf("area = %g", mappedRadius * mappedRadius);
3854 canvas->drawString(mappedArea, 145, 250, paint);
3855 canvas->drawString("mappedRadius", center.fX + mappedRadius + 3, center.fY, paint);
3856 paint.setColor(SK_ColorRED);
3857 SkString axArea;
3858 axArea.printf("area = %g", majorAxis.length() * minorAxis.length());
3859 paint.setStyle(SkPaint::kFill_Style);
3860 canvas->drawString(axArea, 15, 250, paint);
3861 paint.setStyle(SkPaint::kStroke_Style);
3862 canvas->drawRect({10, 200, 10 + majorAxis.length(), 200 + minorAxis.length()}, paint);
3863 paint.setColor(SK_ColorBLACK);
3864 canvas->drawLine(center.fX, center.fY, center.fX + mappedRadius, center.fY, paint);
3865 canvas->drawLine(center.fX, center.fY, center.fX, center.fY + mappedRadius, paint);
3866 canvas->drawRect({140, 180, 140 + mappedRadius, 180 + mappedRadius}, paint);
3867 canvas->concat(matrix);
3868 canvas->drawCircle(center.fX, center.fY, circleRadius, paint);
3869 paint.setColor(SK_ColorRED);
3870 canvas->drawLine(center.fX, center.fY, center.fX + circleRadius, center.fY, paint);
3871 canvas->drawLine(center.fX, center.fY, center.fX, center.fY + circleRadius, paint);
Cary Clarkbc5697d2017-10-04 14:31:33 -04003872##
3873
Cary Clark154beea2017-10-26 07:58:48 -04003874#SeeAlso mapVector
Cary Clarkbc5697d2017-10-04 14:31:33 -04003875
3876##
3877
3878# ------------------------------------------------------------------------------
Cary Clarkbc5697d2017-10-04 14:31:33 -04003879#Method bool isFixedStepInX() const
Cary Clark4855f782018-02-06 09:41:53 -05003880#In Property
Cary Clark08895c42018-02-01 09:37:32 -05003881#Line # returns if transformation supports fixed step in x ##
Cary Clark5538c132018-06-14 12:28:14 -04003882Returns true if a unit step on x-axis at some y-axis value mapped through Matrix
3883can be represented by a constant Vector. Returns true if getType returns
3884kIdentity_Mask, or combinations of: kTranslate_Mask, kScale_Mask, and kAffine_Mask.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003885
Cary Clark154beea2017-10-26 07:58:48 -04003886May return true if getType returns kPerspective_Mask, but only when Matrix
3887does not include rotation or skewing along the y-axis.
3888
3889#Return true if Matrix does not have complex perspective ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003890
3891#Example
Cary Clark154beea2017-10-26 07:58:48 -04003892 SkMatrix matrix;
3893 for (SkScalar px : { 0.0f, 0.1f } ) {
3894 for (SkScalar py : { 0.0f, 0.1f } ) {
3895 for (SkScalar sy : { 1, 2 } ) {
3896 matrix.setAll(1, 0, 0, 0, sy, 0, px, py, 1);
3897 matrix.dump();
3898 SkDebugf("isFixedStepInX: %s\n", matrix.isFixedStepInX() ? "true" : "false");
3899 }
3900 }
3901 }
3902#StdOut
3903[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
3904isFixedStepInX: true
3905[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.0000 0.0000 1.0000]
3906isFixedStepInX: true
3907[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.0000 0.1000 1.0000]
3908isFixedStepInX: true
3909[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.0000 0.1000 1.0000]
3910isFixedStepInX: true
3911[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.1000 0.0000 1.0000]
3912isFixedStepInX: false
3913[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.1000 0.0000 1.0000]
3914isFixedStepInX: false
3915[ 1.0000 0.0000 0.0000][ 0.0000 1.0000 0.0000][ 0.1000 0.1000 1.0000]
3916isFixedStepInX: false
3917[ 1.0000 0.0000 0.0000][ 0.0000 2.0000 0.0000][ 0.1000 0.1000 1.0000]
3918isFixedStepInX: false
3919##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003920##
3921
Cary Clark154beea2017-10-26 07:58:48 -04003922#SeeAlso fixedStepInX getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04003923
3924##
3925
3926# ------------------------------------------------------------------------------
3927
3928#Method SkVector fixedStepInX(SkScalar y) const
Cary Clark4855f782018-02-06 09:41:53 -05003929#In Property
Cary Clark08895c42018-02-01 09:37:32 -05003930#Line # returns step in x for a position in y ##
Cary Clark5538c132018-06-14 12:28:14 -04003931Returns Vector representing a unit step on x-axis at y mapped through Matrix.
Cary Clark154beea2017-10-26 07:58:48 -04003932If isFixedStepInX is false, returned value is undefined.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003933
Cary Clark154beea2017-10-26 07:58:48 -04003934#Param y position of line parallel to x-axis ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003935
Cary Clark5538c132018-06-14 12:28:14 -04003936#Return Vector advance of mapped unit step on x-axis ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003937
3938#Example
Cary Clark154beea2017-10-26 07:58:48 -04003939#Image 3
3940 SkMatrix matrix;
3941 const SkPoint center = { 128, 128 };
3942 matrix.setScale(20, 25, center.fX, center.fY);
3943 matrix.postRotate(75, center.fX, center.fY);
3944 {
3945 SkAutoCanvasRestore acr(canvas, true);
3946 canvas->concat(matrix);
3947 canvas->drawBitmap(source, 0, 0);
3948 }
3949 if (matrix.isFixedStepInX()) {
3950 SkPaint paint;
3951 paint.setAntiAlias(true);
3952 SkVector step = matrix.fixedStepInX(128);
3953 SkVector end = center + step;
3954 canvas->drawLine(center, end, paint);
3955 SkVector arrow = { step.fX + step.fY, step.fY - step.fX};
3956 arrow = arrow * .25f;
3957 canvas->drawLine(end, end - arrow, paint);
3958 canvas->drawLine(end, {end.fX + arrow.fY, end.fY - arrow.fX}, paint);
3959 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04003960##
3961
Cary Clark154beea2017-10-26 07:58:48 -04003962#SeeAlso isFixedStepInX getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04003963
3964##
3965
3966# ------------------------------------------------------------------------------
3967
3968#Method bool cheapEqualTo(const SkMatrix& m) const
Cary Clark4855f782018-02-06 09:41:53 -05003969#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05003970#Line # compares Matrix pair using memcmp() ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003971Returns true if Matrix equals m, using an efficient comparison.
3972
Cary Clark154beea2017-10-26 07:58:48 -04003973Returns false when the sign of zero values is the different; when one
Cary Clarkbc5697d2017-10-04 14:31:33 -04003974matrix has positive zero value and the other has negative zero value.
3975
Cary Clark154beea2017-10-26 07:58:48 -04003976Returns true even when both Matrices contain NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003977
Cary Clark154beea2017-10-26 07:58:48 -04003978NaN never equals any value, including itself. To improve performance, NaN values
3979are treated as bit patterns that are equal if their bit patterns are equal.
Cary Clarkbc5697d2017-10-04 14:31:33 -04003980
Cary Clark154beea2017-10-26 07:58:48 -04003981#Param m Matrix to compare ##
3982
3983#Return true if m and Matrix are represented by identical bit patterns ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04003984
3985#Example
Cary Clark154beea2017-10-26 07:58:48 -04003986 auto debugster = [](const char* prefix, const SkMatrix& a, const SkMatrix& b) -> void {
3987 SkDebugf("%s: a %c= b a.cheapEqualTo(b): %s\n", prefix,
3988 a == b ? '=' : '!', a.cheapEqualTo(b) ? "true" : "false");
Cary Clarkbc5697d2017-10-04 14:31:33 -04003989 };
Cary Clark154beea2017-10-26 07:58:48 -04003990 SkMatrix a, b;
3991 a.setAll(1, 0, 0, 0, 1, 0, 0, 0, 1);
3992 b.setIdentity();
3993 debugster("identity", a, b);
3994 a.setAll(1, -0.0f, 0, 0, 1, 0, 0, 0, 1);
3995 debugster("neg zero", a, b);
3996 a.setAll(1, SK_ScalarNaN, 0, 0, 1, 0, 0, 0, 1);
3997 debugster(" one NaN", a, b);
3998 b.setAll(1, SK_ScalarNaN, 0, 0, 1, 0, 0, 0, 1);
3999 debugster("both NaN", a, b);
4000#StdOut
4001identity: a == b a.cheapEqualTo(b): true
4002neg zero: a == b a.cheapEqualTo(b): false
4003 one NaN: a != b a.cheapEqualTo(b): false
4004both NaN: a != b a.cheapEqualTo(b): true
4005##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004006##
4007
Cary Clark154beea2017-10-26 07:58:48 -04004008#SeeAlso operator==(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004009
4010##
4011
4012# ------------------------------------------------------------------------------
4013
Cary Clark154beea2017-10-26 07:58:48 -04004014#Method bool operator==(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004015
Cary Clark08895c42018-02-01 09:37:32 -05004016#Line # returns true if members are equal ##
Cary Clark154beea2017-10-26 07:58:48 -04004017Compares a and b; returns true if a and b are numerically equal. Returns true
4018even if sign of zero values are different. Returns false if either Matrix
4019contains NaN, even if the other Matrix also contains NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004020
Cary Clark154beea2017-10-26 07:58:48 -04004021#Param a Matrix to compare ##
4022#Param b Matrix to compare ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004023
Cary Clark4855f782018-02-06 09:41:53 -05004024#Return true if Matrix a and Matrix b are numerically equal ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004025
4026#Example
Cary Clark154beea2017-10-26 07:58:48 -04004027 auto debugster = [](const char* prefix, const SkMatrix& a, const SkMatrix& b) -> void {
4028 SkDebugf("%s: a %c= b a.cheapEqualTo(b): %s\n", prefix,
4029 a == b ? '=' : '!', a.cheapEqualTo(b) ? "true" : "false");
4030 };
4031 SkMatrix a, b;
4032 a.setAll(1, 0, 0, 0, 1, 0, 0, 0, 1);
4033 b.setScale(2, 4);
4034 b.postScale(0.5f, 0.25f);
4035 debugster("identity", a, b);
4036#StdOut
4037identity: a == b a.cheapEqualTo(b): true
4038##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004039##
4040
Cary Clark154beea2017-10-26 07:58:48 -04004041#SeeAlso cheapEqualTo operator!=(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004042
4043##
4044
4045# ------------------------------------------------------------------------------
4046
Cary Clark154beea2017-10-26 07:58:48 -04004047#Method bool operator!=(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004048
Cary Clark08895c42018-02-01 09:37:32 -05004049#Line # returns true if members are unequal ##
Cary Clark154beea2017-10-26 07:58:48 -04004050Compares a and b; returns true if a and b are not numerically equal. Returns false
4051even if sign of zero values are different. Returns true if either Matrix
4052contains NaN, even if the other Matrix also contains NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004053
Cary Clark154beea2017-10-26 07:58:48 -04004054#Param a Matrix to compare ##
4055#Param b Matrix to compare ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004056
Cary Clark4855f782018-02-06 09:41:53 -05004057#Return true if Matrix a and Matrix b are numerically not equal ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004058
4059#Example
Cary Clark154beea2017-10-26 07:58:48 -04004060 auto debugster = [](const char* prefix, const SkMatrix& a, const SkMatrix& b) -> void {
4061 SkDebugf("%s: a %c= b a.cheapEqualTo(b): %s\n", prefix,
4062 a != b ? '!' : '=', a.cheapEqualTo(b) ? "true" : "false");
4063 };
4064 SkMatrix a, b;
4065 a.setAll(1, 0, 0, 0, 1, 0, 1, 0, 1);
Cary Clark681287e2018-03-16 11:34:15 -04004066 if (a.invert(&b)) {
4067 debugster("identity", a, b);
4068 }
Cary Clarkbc5697d2017-10-04 14:31:33 -04004069##
4070
Cary Clark154beea2017-10-26 07:58:48 -04004071#SeeAlso cheapEqualTo operator==(const SkMatrix& a, const SkMatrix& b)
Cary Clarkbc5697d2017-10-04 14:31:33 -04004072
4073##
4074
4075# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05004076#Subtopic Utility
4077#Populate
4078#Line # rarely called management functions ##
4079##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004080
4081#Method void dump() const
Cary Clark4855f782018-02-06 09:41:53 -05004082#In Utility
Cary Clark08895c42018-02-01 09:37:32 -05004083#Line # sends text representation using floats to standard output ##
Cary Clark154beea2017-10-26 07:58:48 -04004084Writes text representation of Matrix to standard output. Floating point values
4085are written with limited precision; it may not be possible to reconstruct
4086original Matrix from output.
4087
Cary Clarkbc5697d2017-10-04 14:31:33 -04004088#Example
Cary Clark154beea2017-10-26 07:58:48 -04004089 SkMatrix matrix;
4090 matrix.setRotate(45);
4091 matrix.dump();
4092 SkMatrix nearlyEqual;
4093 nearlyEqual.setAll(0.7071f, -0.7071f, 0, 0.7071f, 0.7071f, 0, 0, 0, 1);
4094 nearlyEqual.dump();
4095 SkDebugf("matrix %c= nearlyEqual\n", matrix == nearlyEqual ? '=' : '!');
4096#StdOut
4097[ 0.7071 -0.7071 0.0000][ 0.7071 0.7071 0.0000][ 0.0000 0.0000 1.0000]
4098[ 0.7071 -0.7071 0.0000][ 0.7071 0.7071 0.0000][ 0.0000 0.0000 1.0000]
4099matrix != nearlyEqual
4100##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004101##
4102
Cary Clark27dddae2018-06-08 15:57:37 -04004103#SeeAlso SkPath::dump
Cary Clarkbc5697d2017-10-04 14:31:33 -04004104
4105##
4106
4107# ------------------------------------------------------------------------------
4108
4109#Method SkScalar getMinScale() const
Cary Clark4855f782018-02-06 09:41:53 -05004110#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004111#Line # returns minimum scaling, if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04004112Returns the minimum scaling factor of Matrix by decomposing the scaling and
4113skewing elements.
4114Returns -1 if scale factor overflows or Matrix contains perspective.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004115
4116#Return minimum scale factor
4117##
4118
4119#Example
Cary Clark154beea2017-10-26 07:58:48 -04004120 SkMatrix matrix;
4121 matrix.setScale(42, 24);
4122 SkDebugf("matrix.getMinScale() %g\n", matrix.getMinScale());
4123#StdOut
4124matrix.getMinScale() 24
4125##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004126##
4127
Cary Clark154beea2017-10-26 07:58:48 -04004128#SeeAlso getMaxScale getMinMaxScales
Cary Clarkbc5697d2017-10-04 14:31:33 -04004129
4130##
4131
4132# ------------------------------------------------------------------------------
4133
4134#Method SkScalar getMaxScale() const
Cary Clark4855f782018-02-06 09:41:53 -05004135#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004136#Line # returns maximum scaling, if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04004137Returns the maximum scaling factor of Matrix by decomposing the scaling and
4138skewing elements.
4139Returns -1 if scale factor overflows or Matrix contains perspective.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004140
4141#Return maximum scale factor
4142##
4143
4144#Example
Cary Clark154beea2017-10-26 07:58:48 -04004145 SkMatrix matrix;
4146 matrix.setScale(42, 24);
4147 SkDebugf("matrix.getMaxScale() %g\n", matrix.getMaxScale());
4148#StdOut
4149matrix.getMaxScale() 42
4150##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004151##
4152
Cary Clark154beea2017-10-26 07:58:48 -04004153#SeeAlso getMinScale getMinMaxScales
Cary Clarkbc5697d2017-10-04 14:31:33 -04004154
4155##
4156
4157# ------------------------------------------------------------------------------
4158
4159#Method bool SK_WARN_UNUSED_RESULT getMinMaxScales(SkScalar scaleFactors[2]) const
Cary Clark4855f782018-02-06 09:41:53 -05004160#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004161#Line # returns minimum and maximum scaling, if possible ##
Cary Clark682c58d2018-05-16 07:07:07 -04004162Sets scaleFactors[0] to the minimum scaling factor, and scaleFactors[1] to the
Cary Clark154beea2017-10-26 07:58:48 -04004163maximum scaling factor. Scaling factors are computed by decomposing
4164the Matrix scaling and skewing elements.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004165
Cary Clark154beea2017-10-26 07:58:48 -04004166Returns true if scaleFactors are found; otherwise, returns false and sets
4167scaleFactors to undefined values.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004168
Cary Clark154beea2017-10-26 07:58:48 -04004169#Param scaleFactors storage for minimum and maximum scale factors ##
4170
4171#Return true if scale factors were computed correctly ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004172
4173#Example
Cary Clark154beea2017-10-26 07:58:48 -04004174 SkMatrix matrix;
4175 matrix.setAll(1, 0, 0, 0, 1, 0, 0, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04004176 if (matrix.invert(&matrix)) {
4177 SkScalar factor[2] = {2, 2};
4178 bool result = matrix.getMinMaxScales(factor);
4179 SkDebugf("matrix.getMinMaxScales() %s %g %g\n",
4180 result ? "true" : "false", factor[0], factor[1]);
4181 }
Cary Clark154beea2017-10-26 07:58:48 -04004182#StdOut
4183matrix.getMinMaxScales() false 2 2
4184##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004185##
4186
Cary Clark154beea2017-10-26 07:58:48 -04004187#SeeAlso getMinScale getMaxScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04004188
4189##
4190
4191# ------------------------------------------------------------------------------
4192
4193#Method bool decomposeScale(SkSize* scale, SkMatrix* remaining = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05004194#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004195#Line # separates scale if possible ##
Cary Clark154beea2017-10-26 07:58:48 -04004196Decomposes Matrix into scale components and whatever remains. Returns false if
4197Matrix could not be decomposed.
4198
Cary Clark5538c132018-06-14 12:28:14 -04004199Sets scale to portion of Matrix that scale axes. Sets remaining to Matrix
4200with scaling factored out. remaining may be passed as nullptr
Cary Clark154beea2017-10-26 07:58:48 -04004201to determine if Matrix can be decomposed without computing remainder.
4202
4203Returns true if scale components are found. scale and remaining are
4204unchanged if Matrix contains perspective; scale factors are not finite, or
Cary Clark682c58d2018-05-16 07:07:07 -04004205are nearly zero.
Cary Clark154beea2017-10-26 07:58:48 -04004206
Cary Clark2be81cf2018-09-13 12:04:30 -04004207On success: #Formula # Matrix = scale * Remaining ##.
Cary Clark154beea2017-10-26 07:58:48 -04004208
Cary Clark2be81cf2018-09-13 12:04:30 -04004209#Param scale axes scaling factors; may be nullptr ##
Cary Clark154beea2017-10-26 07:58:48 -04004210#Param remaining Matrix without scaling; may be nullptr ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004211
Cary Clark154beea2017-10-26 07:58:48 -04004212#Return true if scale can be computed ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004213
4214#Example
Cary Clark154beea2017-10-26 07:58:48 -04004215 SkMatrix matrix;
4216 matrix.setRotate(90 * SK_Scalar1);
4217 matrix.postScale(1.f / 4, 1.f / 2);
4218 matrix.dump();
4219 SkSize scale = {SK_ScalarNaN, SK_ScalarNaN};
4220 SkMatrix remaining;
4221 remaining.reset();
4222 bool success = matrix.decomposeScale(&scale, &remaining);
4223 SkDebugf("success: %s ", success ? "true" : "false");
4224 SkDebugf("scale: %g, %g\n", scale.width(), scale.height());
4225 remaining.dump();
4226 SkMatrix scaleMatrix = SkMatrix::MakeScale(scale.width(), scale.height());
4227 SkMatrix combined = SkMatrix::Concat(scaleMatrix, remaining);
4228 combined.dump();
4229#StdOut
4230[ 0.0000 -0.2500 0.0000][ 0.5000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
4231success: true scale: 0.5, 0.25
4232[ 0.0000 -0.5000 0.0000][ 2.0000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
4233[ 0.0000 -0.2500 0.0000][ 0.5000 0.0000 0.0000][ 0.0000 0.0000 1.0000]
4234##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004235##
4236
Cary Clark154beea2017-10-26 07:58:48 -04004237#SeeAlso setScale MakeScale
Cary Clarkbc5697d2017-10-04 14:31:33 -04004238
4239##
4240
4241# ------------------------------------------------------------------------------
4242
4243#Method static const SkMatrix& I()
Cary Clark4855f782018-02-06 09:41:53 -05004244#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05004245#Line # returns a reference to a const identity Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -04004246Returns reference to const identity Matrix. Returned Matrix is set to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04004247
Cary Clark154beea2017-10-26 07:58:48 -04004248#Code
4249#Literal
4250| 1 0 0 |
4251| 0 1 0 |
4252| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004253##
4254
Cary Clark154beea2017-10-26 07:58:48 -04004255#Return const identity Matrix ##
4256
4257#Example
4258 SkMatrix m1, m2, m3;
4259 m1.reset();
4260 m2.setIdentity();
4261 m3 = SkMatrix::I();
4262 SkDebugf("m1 %c= m2\n", m1 == m2 ? '=' : '!');
4263 SkDebugf("m2 %c= m3\n", m1 == m2 ? '=' : '!');
4264#StdOut
4265m1 == m2
4266m2 == m3
4267##
4268##
4269
4270#SeeAlso reset() setIdentity
Cary Clarkbc5697d2017-10-04 14:31:33 -04004271
4272##
4273
4274# ------------------------------------------------------------------------------
4275
4276#Method static const SkMatrix& InvalidMatrix()
Cary Clark4855f782018-02-06 09:41:53 -05004277#In Constructor
Cary Clark08895c42018-02-01 09:37:32 -05004278#Line # returns a reference to a const invalid Matrix ##
Cary Clark154beea2017-10-26 07:58:48 -04004279Returns reference to a const Matrix with invalid values. Returned Matrix is set
4280to:
Cary Clarkbc5697d2017-10-04 14:31:33 -04004281
Cary Clark154beea2017-10-26 07:58:48 -04004282#Code
4283#Literal
4284| SK_ScalarMax SK_ScalarMax SK_ScalarMax |
4285| SK_ScalarMax SK_ScalarMax SK_ScalarMax |
4286| SK_ScalarMax SK_ScalarMax SK_ScalarMax |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004287##
4288
Cary Clark154beea2017-10-26 07:58:48 -04004289#Return const invalid Matrix ##
4290
4291#Example
4292 SkDebugf("scaleX %g\n", SkMatrix::InvalidMatrix().getScaleX());
4293#StdOut
4294scaleX 3.40282e+38
4295##
4296##
4297
4298#SeeAlso SeeAlso getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04004299
4300##
4301
4302# ------------------------------------------------------------------------------
4303
4304#Method static SkMatrix Concat(const SkMatrix& a, const SkMatrix& b)
Cary Clark4855f782018-02-06 09:41:53 -05004305#In Operator
Cary Clark08895c42018-02-01 09:37:32 -05004306#Line # returns the concatenation of Matrix pair ##
Cary Clark154beea2017-10-26 07:58:48 -04004307Returns Matrix a multiplied by Matrix b.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004308
Cary Clark154beea2017-10-26 07:58:48 -04004309Given:
Cary Clarkbc5697d2017-10-04 14:31:33 -04004310
Cary Clark154beea2017-10-26 07:58:48 -04004311#Code
4312#Literal
4313 | A B C | | J K L |
4314a = | D E F |, b = | M N O |
4315 | G H I | | P Q R |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004316##
4317
Cary Clark154beea2017-10-26 07:58:48 -04004318sets Matrix to:
4319
4320#Code
4321#Literal
4322 | A B C | | J K L | | AJ+BM+CP AK+BN+CQ AL+BO+CR |
4323a * b = | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR |
4324 | G H I | | P Q R | | GJ+HM+IP GK+HN+IQ GL+HO+IR |
4325##
4326
4327#Param a Matrix on left side of multiply expression ##
4328#Param b Matrix on right side of multiply expression ##
4329
4330#Return Matrix computed from a times b ##
4331
4332#Example
4333#Height 64
4334#Image 4
4335#Description
4336setPolyToPoly creates perspective matrices, one the inverse of the other.
4337Multiplying the matrix by its inverse turns into an identity matrix.
4338##
4339SkMatrix matrix, matrix2;
4340SkPoint bitmapBounds[4], perspect[4] = {{50, 10}, {180, 40}, {236, 176}, {10, 206}};
4341SkRect::Make(source.bounds()).toQuad(bitmapBounds);
4342matrix.setPolyToPoly(bitmapBounds, perspect, 4);
4343matrix2.setPolyToPoly(perspect, bitmapBounds, 4);
4344SkMatrix concat = SkMatrix::Concat(matrix, matrix2);
4345canvas->concat(concat);
4346canvas->drawBitmap(source, 0, 0);
4347##
4348
4349#SeeAlso preConcat postConcat
Cary Clarkbc5697d2017-10-04 14:31:33 -04004350
4351##
4352
4353# ------------------------------------------------------------------------------
4354
4355#Method void dirtyMatrixTypeCache()
Cary Clark4855f782018-02-06 09:41:53 -05004356#In Utility
Cary Clark08895c42018-02-01 09:37:32 -05004357#Line # sets internal cache to unknown state ##
Cary Clark682c58d2018-05-16 07:07:07 -04004358Sets internal cache to unknown state. Use to force update after repeated
Cary Clark154beea2017-10-26 07:58:48 -04004359modifications to Matrix element reference returned by operator[](int index).
Cary Clarkbc5697d2017-10-04 14:31:33 -04004360
4361#Example
Cary Clark154beea2017-10-26 07:58:48 -04004362SkMatrix matrix;
4363matrix.setIdentity();
4364SkDebugf("with identity matrix: x = %g\n", matrix.mapXY(24, 42).fX);
4365SkScalar& skewRef = matrix[SkMatrix::kMSkewX];
4366skewRef = 0;
4367SkDebugf("after skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
4368skewRef = 1;
4369SkDebugf("after 2nd skew x mod: x = %g\n", matrix.mapXY(24, 42).fX);
4370matrix.dirtyMatrixTypeCache();
4371SkDebugf("after dirty cache: x = %g\n", matrix.mapXY(24, 42).fX);
4372#StdOut
4373with identity matrix: x = 24
4374after skew x mod: x = 24
4375after 2nd skew x mod: x = 24
4376after dirty cache: x = 66
4377##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004378##
4379
Cary Clark154beea2017-10-26 07:58:48 -04004380#SeeAlso operator[](int index) getType
Cary Clarkbc5697d2017-10-04 14:31:33 -04004381
4382##
4383
4384# ------------------------------------------------------------------------------
4385
4386#Method void setScaleTranslate(SkScalar sx, SkScalar sy, SkScalar tx, SkScalar ty)
Cary Clark4855f782018-02-06 09:41:53 -05004387#In Constructor
4388#In Set
Cary Clark08895c42018-02-01 09:37:32 -05004389#Line # sets to scale and translate ##
Cary Clark154beea2017-10-26 07:58:48 -04004390Initializes Matrix with scale and translate elements.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004391
Cary Clark154beea2017-10-26 07:58:48 -04004392#Code
4393#Literal
4394| sx 0 tx |
4395| 0 sy ty |
4396| 0 0 1 |
Cary Clarkbc5697d2017-10-04 14:31:33 -04004397##
4398
Cary Clark154beea2017-10-26 07:58:48 -04004399#Param sx horizontal scale factor to store ##
4400#Param sy vertical scale factor to store ##
4401#Param tx horizontal translation to store ##
4402#Param ty vertical translation to store ##
4403
4404#Example
4405SkMatrix matrix;
4406matrix.setScaleTranslate(1, 2, 3, 4);
4407matrix.dump();
4408#StdOut
4409[ 1.0000 0.0000 3.0000][ 0.0000 2.0000 4.0000][ 0.0000 0.0000 1.0000]
4410##
4411##
4412
4413#SeeAlso setScale preTranslate postTranslate
Cary Clarkbc5697d2017-10-04 14:31:33 -04004414
4415##
4416
4417# ------------------------------------------------------------------------------
4418
4419#Method bool isFinite() const
Cary Clark4855f782018-02-06 09:41:53 -05004420#In Property
Cary Clark08895c42018-02-01 09:37:32 -05004421#Line # returns if all Matrix values are not infinity, NaN ##
Cary Clark154beea2017-10-26 07:58:48 -04004422Returns true if all elements of the matrix are finite. Returns false if any
4423element is infinity, or NaN.
Cary Clarkbc5697d2017-10-04 14:31:33 -04004424
Cary Clark154beea2017-10-26 07:58:48 -04004425#Return true if matrix has only finite elements ##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004426
4427#Example
Cary Clark154beea2017-10-26 07:58:48 -04004428SkMatrix matrix = SkMatrix::MakeTrans(SK_ScalarNaN, 0);
4429matrix.dump();
4430SkDebugf("matrix is finite: %s\n", matrix.isFinite() ? "true" : "false");
4431SkDebugf("matrix %c= matrix\n", matrix == matrix ? '=' : '!');
4432#StdOut
4433[ 1.0000 0.0000 nan][ 0.0000 1.0000 0.0000][ 0.0000 0.0000 1.0000]
4434matrix is finite: false
4435matrix != matrix
4436##
Cary Clarkbc5697d2017-10-04 14:31:33 -04004437##
4438
Cary Clark154beea2017-10-26 07:58:48 -04004439#SeeAlso operator==
Cary Clarkbc5697d2017-10-04 14:31:33 -04004440
4441##
4442
4443#Class SkMatrix ##
4444
4445#Topic Matrix ##
Cary Clark4855f782018-02-06 09:41:53 -05004446