-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Matrix.cs
879 lines (775 loc) · 21.2 KB
/
Matrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
#nullable disable
using System;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls.Shapes
{
internal enum MatrixTypes
{
Identity = 0,
Translation = 1,
Scaling = 2,
Unknown = 4
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="Type[@FullName='Microsoft.Maui.Controls.Shapes.Matrix']/Docs/*" />
[System.ComponentModel.TypeConverter(typeof(MatrixTypeConverter))]
public struct Matrix : IEquatable<Matrix>
{
internal double _m11;
internal double _m12;
internal double _m21;
internal double _m22;
internal double _offsetX;
internal double _offsetY;
internal MatrixTypes _type;
internal int _padding;
static Matrix IdentityMatrix = CreateIdentity();
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public Matrix(double m11, double m12,
double m21, double m22,
double offsetX, double offsetY)
{
_m11 = m11;
_m12 = m12;
_m21 = m21;
_m22 = m22;
_offsetX = offsetX;
_offsetY = offsetY;
_type = MatrixTypes.Unknown;
_padding = 0;
DeriveMatrixType();
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Identity']/Docs/*" />
public static Matrix Identity { get { return IdentityMatrix; } }
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='SetIdentity']/Docs/*" />
public void SetIdentity()
{
_type = MatrixTypes.Identity;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='IsIdentity']/Docs/*" />
public bool IsIdentity
{
get
{
return _type == MatrixTypes.Identity ||
(_m11 == 1 && _m12 == 0 && _m21 == 0 && _m22 == 1 && _offsetX == 0 && _offsetY == 0);
}
}
public static Matrix operator *(Matrix trans1, Matrix trans2)
{
MatrixUtil.MultiplyMatrix(ref trans1, ref trans2);
return trans1;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Multiply']/Docs/*" />
public static Matrix Multiply(Matrix trans1, Matrix trans2)
{
MatrixUtil.MultiplyMatrix(ref trans1, ref trans2);
return trans1;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Append']/Docs/*" />
public void Append(Matrix matrix)
{
this *= matrix;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Prepend']/Docs/*" />
public void Prepend(Matrix matrix)
{
this = matrix * this;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Rotate']/Docs/*" />
public void Rotate(double angle)
{
angle %= 360.0;
this *= CreateRotationRadians(angle * (Math.PI / 180.0));
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='RotatePrepend']/Docs/*" />
public void RotatePrepend(double angle)
{
angle %= 360.0;
this = CreateRotationRadians(angle * (Math.PI / 180.0)) * this;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='RotateAt']/Docs/*" />
public void RotateAt(double angle, double centerX, double centerY)
{
angle %= 360.0;
this *= CreateRotationRadians(angle * (Math.PI / 180.0), centerX, centerY);
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='RotateAtPrepend']/Docs/*" />
public void RotateAtPrepend(double angle, double centerX, double centerY)
{
angle %= 360.0;
this = CreateRotationRadians(angle * (Math.PI / 180.0), centerX, centerY) * this;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Scale']/Docs/*" />
public void Scale(double scaleX, double scaleY)
{
this *= CreateScaling(scaleX, scaleY);
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='ScalePrepend']/Docs/*" />
public void ScalePrepend(double scaleX, double scaleY)
{
this = CreateScaling(scaleX, scaleY) * this;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='ScaleAt']/Docs/*" />
public void ScaleAt(double scaleX, double scaleY, double centerX, double centerY)
{
this *= CreateScaling(scaleX, scaleY, centerX, centerY);
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='ScaleAtPrepend']/Docs/*" />
public void ScaleAtPrepend(double scaleX, double scaleY, double centerX, double centerY)
{
this = CreateScaling(scaleX, scaleY, centerX, centerY) * this;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Skew']/Docs/*" />
public void Skew(double skewX, double skewY)
{
skewX %= 360;
skewY %= 360;
this *= CreateSkewRadians(skewX * (Math.PI / 180.0),
skewY * (Math.PI / 180.0));
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='SkewPrepend']/Docs/*" />
public void SkewPrepend(double skewX, double skewY)
{
skewX %= 360;
skewY %= 360;
this = CreateSkewRadians(skewX * (Math.PI / 180.0),
skewY * (Math.PI / 180.0)) * this;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Translate']/Docs/*" />
public void Translate(double offsetX, double offsetY)
{
//
// / a b 0 \ / 1 0 0 \ / a b 0 \
// | c d 0 | * | 0 1 0 | = | c d 0 |
// \ e f 1 / \ x y 1 / \ e+x f+y 1 /
//
// (where e = _offsetX and f == _offsetY)
//
if (_type == MatrixTypes.Identity)
{
SetMatrix(1, 0,
0, 1,
offsetX, offsetY,
MatrixTypes.Translation);
}
else if (_type == MatrixTypes.Unknown)
{
_offsetX += offsetX;
_offsetY += offsetY;
}
else
{
_offsetX += offsetX;
_offsetY += offsetY;
_type |= MatrixTypes.Translation;
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='TranslatePrepend']/Docs/*" />
public void TranslatePrepend(double offsetX, double offsetY)
{
this = CreateTranslation(offsetX, offsetY) * this;
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Transform'][1]/Docs/*" />
public Point Transform(Point point)
{
Point newPoint = point;
double x = newPoint.X;
double y = newPoint.Y;
MultiplyPoint(ref x, ref y);
return new Point(x, y);
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Transform'][2]/Docs/*" />
public void Transform(Point[] points)
{
if (points != null)
{
for (int i = 0; i < points.Length; i++)
{
var point = points[i];
double x = point.X;
double y = point.Y;
MultiplyPoint(ref x, ref y);
points[i] = new Point(x, y);
}
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Determinant']/Docs/*" />
public double Determinant
{
get
{
switch (_type)
{
case MatrixTypes.Identity:
case MatrixTypes.Translation:
return 1.0;
case MatrixTypes.Scaling:
case MatrixTypes.Scaling | MatrixTypes.Translation:
return _m11 * _m22;
default:
return (_m11 * _m22) - (_m12 * _m21);
}
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='HasInverse']/Docs/*" />
public bool HasInverse { get { return Determinant != 0; } }
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='Invert']/Docs/*" />
public void Invert()
{
double determinant = Determinant;
if (determinant == 0)
{
throw new InvalidOperationException();
}
switch (_type)
{
case MatrixTypes.Identity:
break;
case MatrixTypes.Scaling:
{
_m11 = 1.0 / _m11;
_m22 = 1.0 / _m22;
}
break;
case MatrixTypes.Translation:
_offsetX = -_offsetX;
_offsetY = -_offsetY;
break;
case MatrixTypes.Scaling | MatrixTypes.Translation:
{
_m11 = 1.0 / _m11;
_m22 = 1.0 / _m22;
_offsetX = -_offsetX * _m11;
_offsetY = -_offsetY * _m22;
}
break;
default:
{
double invdet = 1.0 / determinant;
SetMatrix(_m22 * invdet,
-_m12 * invdet,
-_m21 * invdet,
_m11 * invdet,
(_m21 * _offsetY - _offsetX * _m22) * invdet,
(_offsetX * _m12 - _m11 * _offsetY) * invdet,
MatrixTypes.Unknown);
}
break;
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='M11']/Docs/*" />
public double M11
{
get
{
if (_type == MatrixTypes.Identity)
{
return 1.0;
}
else
{
return _m11;
}
}
set
{
if (_type == MatrixTypes.Identity)
{
SetMatrix(value, 0,
0, 1,
0, 0,
MatrixTypes.Scaling);
}
else
{
_m11 = value;
if (_type != MatrixTypes.Unknown)
{
_type |= MatrixTypes.Scaling;
}
}
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='M12']/Docs/*" />
public double M12
{
get
{
if (_type == MatrixTypes.Identity)
{
return 0;
}
else
{
return _m12;
}
}
set
{
if (_type == MatrixTypes.Identity)
{
SetMatrix(1, value,
0, 1,
0, 0,
MatrixTypes.Unknown);
}
else
{
_m12 = value;
_type = MatrixTypes.Unknown;
}
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='M21']/Docs/*" />
public double M21
{
get
{
if (_type == MatrixTypes.Identity)
{
return 0;
}
else
{
return _m21;
}
}
set
{
if (_type == MatrixTypes.Identity)
{
SetMatrix(1, 0,
value, 1,
0, 0,
MatrixTypes.Unknown);
}
else
{
_m21 = value;
_type = MatrixTypes.Unknown;
}
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='M22']/Docs/*" />
public double M22
{
get
{
if (_type == MatrixTypes.Identity)
{
return 1.0;
}
else
{
return _m22;
}
}
set
{
if (_type == MatrixTypes.Identity)
{
SetMatrix(1, 0,
0, value,
0, 0,
MatrixTypes.Scaling);
}
else
{
_m22 = value;
if (_type != MatrixTypes.Unknown)
{
_type |= MatrixTypes.Scaling;
}
}
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='OffsetX']/Docs/*" />
public double OffsetX
{
get
{
if (_type == MatrixTypes.Identity)
{
return 0;
}
else
{
return _offsetX;
}
}
set
{
if (_type == MatrixTypes.Identity)
{
SetMatrix(1, 0,
0, 1,
value, 0,
MatrixTypes.Translation);
}
else
{
_offsetX = value;
if (_type != MatrixTypes.Unknown)
{
_type |= MatrixTypes.Translation;
}
}
}
}
/// <include file="../../../docs/Microsoft.Maui.Controls.Shapes/Matrix.xml" path="//Member[@MemberName='OffsetY']/Docs/*" />
public double OffsetY
{
get
{
if (_type == MatrixTypes.Identity)
{
return 0;
}
else
{
return _offsetY;
}
}
set
{
if (_type == MatrixTypes.Identity)
{
SetMatrix(1, 0,
0, 1,
0, value,
MatrixTypes.Translation);
}
else
{
_offsetY = value;
if (_type != MatrixTypes.Unknown)
{
_type |= MatrixTypes.Translation;
}
}
}
}
internal void MultiplyVector(ref double x, ref double y)
{
switch (_type)
{
case MatrixTypes.Identity:
case MatrixTypes.Translation:
return;
case MatrixTypes.Scaling:
case MatrixTypes.Scaling | MatrixTypes.Translation:
x *= _m11;
y *= _m22;
break;
default:
double xadd = y * _m21;
double yadd = x * _m12;
x *= _m11;
x += xadd;
y *= _m22;
y += yadd;
break;
}
}
internal void MultiplyPoint(ref double x, ref double y)
{
switch (_type)
{
case MatrixTypes.Identity:
return;
case MatrixTypes.Translation:
x += _offsetX;
y += _offsetY;
return;
case MatrixTypes.Scaling:
x *= _m11;
y *= _m22;
return;
case MatrixTypes.Scaling | MatrixTypes.Translation:
x *= _m11;
x += _offsetX;
y *= _m22;
y += _offsetY;
break;
default:
double xadd = y * _m21 + _offsetX;
double yadd = x * _m12 + _offsetY;
x *= _m11;
x += xadd;
y *= _m22;
y += yadd;
break;
}
}
internal static Matrix CreateRotationRadians(double angle)
{
return CreateRotationRadians(angle, 0, 0);
}
internal static Matrix CreateRotationRadians(double angle, double centerX, double centerY)
{
Matrix matrix = new Matrix();
double sin = Math.Sin(angle);
double cos = Math.Cos(angle);
double dx = (centerX * (1.0 - cos)) + (centerY * sin);
double dy = (centerY * (1.0 - cos)) - (centerX * sin);
matrix.SetMatrix(cos, sin,
-sin, cos,
dx, dy,
MatrixTypes.Unknown);
return matrix;
}
internal static Matrix CreateScaling(double scaleX, double scaleY, double centerX, double centerY)
{
Matrix matrix = new Matrix();
matrix.SetMatrix(scaleX, 0,
0, scaleY,
centerX - scaleX * centerX, centerY - scaleY * centerY,
MatrixTypes.Scaling | MatrixTypes.Translation);
return matrix;
}
internal static Matrix CreateScaling(double scaleX, double scaleY)
{
Matrix matrix = new Matrix();
matrix.SetMatrix(scaleX, 0,
0, scaleY,
0, 0,
MatrixTypes.Scaling);
return matrix;
}
internal static Matrix CreateSkewRadians(double skewX, double skewY)
{
Matrix matrix = new Matrix();
matrix.SetMatrix(1.0, Math.Tan(skewY),
Math.Tan(skewX), 1.0,
0.0, 0.0,
MatrixTypes.Unknown);
return matrix;
}
/// <summary>
/// Sets the transformation to the given translation specified by the offset vector.
/// </summary>
/// <param name='offsetX'>The offset in X</param>
/// <param name='offsetY'>The offset in Y</param>
internal static Matrix CreateTranslation(double offsetX, double offsetY)
{
Matrix matrix = new Matrix();
matrix.SetMatrix(1, 0,
0, 1,
offsetX, offsetY,
MatrixTypes.Translation);
return matrix;
}
static Matrix CreateIdentity()
{
Matrix matrix = new Matrix();
matrix.SetMatrix(1, 0,
0, 1,
0, 0,
MatrixTypes.Identity);
return matrix;
}
void SetMatrix(double m11, double m12,
double m21, double m22,
double offsetX, double offsetY,
MatrixTypes type)
{
_m11 = m11;
_m12 = m12;
_m21 = m21;
_m22 = m22;
_offsetX = offsetX;
_offsetY = offsetY;
_type = type;
}
void DeriveMatrixType()
{
_type = 0;
if (!(_m21 == 0 && _m12 == 0))
{
_type = MatrixTypes.Unknown;
return;
}
if (!(_m11 == 1 && _m22 == 1))
{
_type = MatrixTypes.Scaling;
}
if (!(_offsetX == 0 && _offsetY == 0))
{
_type |= MatrixTypes.Translation;
}
if (0 == (_type & (MatrixTypes.Translation | MatrixTypes.Scaling)))
{
_type = MatrixTypes.Identity;
}
return;
}
public bool Equals(Matrix other) =>
_m11 == other._m11 && _m12 == other._m12 && _m21 == other._m21 && _m22 == other._m22 &&
_offsetX == other._offsetX && _offsetY == other._offsetY && _type == other._type && _padding == other._padding;
public override bool Equals(object obj) => obj is Matrix other && Equals(other);
public override int GetHashCode() =>
_m11.GetHashCode() ^ _m12.GetHashCode() ^ _m21.GetHashCode() ^ _m22.GetHashCode() ^
_offsetX.GetHashCode() ^ _offsetY.GetHashCode() ^ _type.GetHashCode() ^ _padding.GetHashCode();
public static bool operator ==(Matrix left, Matrix right) => left.Equals(right);
public static bool operator !=(Matrix left, Matrix right) => !(left == right);
}
internal static class MatrixUtil
{
internal static void TransformRect(ref Microsoft.Maui.Graphics.Rect rect, ref Matrix matrix)
{
if (rect.IsEmpty)
{
return;
}
MatrixTypes matrixType = matrix._type;
if (matrixType == MatrixTypes.Identity)
{
return;
}
// Scaling
if (0 != (matrixType & MatrixTypes.Scaling))
{
rect.X *= matrix._m11;
rect.Y *= matrix._m22;
rect.Width *= matrix._m11;
rect.Height *= matrix._m22;
if (rect.Width < 0.0)
{
rect.X += rect.Width;
rect.Width = -rect.Width;
}
if (rect.Height < 0.0)
{
rect.Y += rect.Height;
rect.Height = -rect.Height;
}
}
// Translation
if (0 != (matrixType & MatrixTypes.Translation))
{
// X
rect.X += matrix._offsetX;
// Y
rect.X += matrix._offsetY;
}
if (matrixType == MatrixTypes.Unknown)
{
Point point0 = matrix.Transform(new Point(rect.Right, rect.Top));
Point point1 = matrix.Transform(new Point(rect.Right, rect.Top));
Point point2 = matrix.Transform(new Point(rect.Right, rect.Bottom));
Point point3 = matrix.Transform(new Point(rect.Left, rect.Bottom));
rect.X = Math.Min(Math.Min(point0.X, point1.X), Math.Min(point2.X, point3.X));
rect.Y = Math.Min(Math.Min(point0.Y, point1.Y), Math.Min(point2.Y, point3.Y));
rect.Width = Math.Max(Math.Max(point0.X, point1.X), Math.Max(point2.X, point3.X)) - rect.X;
rect.Height = Math.Max(Math.Max(point0.Y, point1.Y), Math.Max(point2.Y, point3.Y)) - rect.Y;
}
}
internal static void MultiplyMatrix(ref Matrix matrix1, ref Matrix matrix2)
{
MatrixTypes type1 = matrix1._type;
MatrixTypes type2 = matrix2._type;
if (type2 == MatrixTypes.Identity)
{
return;
}
if (type1 == MatrixTypes.Identity)
{
matrix1 = matrix2;
return;
}
if (type2 == MatrixTypes.Translation)
{
matrix1._offsetX += matrix2._offsetX;
matrix1._offsetY += matrix2._offsetY;
if (type1 != MatrixTypes.Unknown)
{
matrix1._type |= MatrixTypes.Translation;
}
return;
}
// Check for the first value being a translate
if (type1 == MatrixTypes.Translation)
{
double offsetX = matrix1._offsetX;
double offsetY = matrix1._offsetY;
matrix1 = matrix2;
matrix1._offsetX = offsetX * matrix2._m11 + offsetY * matrix2._m21 + matrix2._offsetX;
matrix1._offsetY = offsetX * matrix2._m12 + offsetY * matrix2._m22 + matrix2._offsetY;
if (type2 == MatrixTypes.Unknown)
{
matrix1._type = MatrixTypes.Unknown;
}
else
{
matrix1._type = MatrixTypes.Scaling | MatrixTypes.Translation;
}
return;
}
// trans1._type | trans2._type
// 7 6 5 4 | 3 2 1 0
int combinedType = ((int)type1 << 4) | (int)type2;
switch (combinedType)
{
case 34: // S * S
// 2 multiplications
matrix1._m11 *= matrix2._m11;
matrix1._m22 *= matrix2._m22;
return;
case 35: // S * S|T
matrix1._m11 *= matrix2._m11;
matrix1._m22 *= matrix2._m22;
matrix1._offsetX = matrix2._offsetX;
matrix1._offsetY = matrix2._offsetY;
// Transform set to Translate and Scale
matrix1._type = MatrixTypes.Translation | MatrixTypes.Scaling;
return;
case 50: // S|T * S
matrix1._m11 *= matrix2._m11;
matrix1._m22 *= matrix2._m22;
matrix1._offsetX *= matrix2._m11;
matrix1._offsetY *= matrix2._m22;
return;
case 51: // S|T * S|T
matrix1._m11 *= matrix2._m11;
matrix1._m22 *= matrix2._m22;
matrix1._offsetX = matrix2._m11 * matrix1._offsetX + matrix2._offsetX;
matrix1._offsetY = matrix2._m22 * matrix1._offsetY + matrix2._offsetY;
return;
case 36: // S * U
case 52: // S|T * U
case 66: // U * S
case 67: // U * S|T
case 68: // U * U
matrix1 = new Matrix(
matrix1._m11 * matrix2._m11 + matrix1._m12 * matrix2._m21,
matrix1._m11 * matrix2._m12 + matrix1._m12 * matrix2._m22,
matrix1._m21 * matrix2._m11 + matrix1._m22 * matrix2._m21,
matrix1._m21 * matrix2._m12 + matrix1._m22 * matrix2._m22,
matrix1._offsetX * matrix2._m11 + matrix1._offsetY * matrix2._m21 + matrix2._offsetX,
matrix1._offsetX * matrix2._m12 + matrix1._offsetY * matrix2._m22 + matrix2._offsetY);
return;
default:
break;
}
}
internal static void PrependOffset(ref Matrix matrix, double offsetX, double offsetY)
{
if (matrix._type == MatrixTypes.Identity)
{
matrix = new Matrix(1, 0, 0, 1, offsetX, offsetY)
{
_type = MatrixTypes.Translation
};
}
else
{
matrix._offsetX += matrix._m11 * offsetX + matrix._m21 * offsetY;
matrix._offsetY += matrix._m12 * offsetX + matrix._m22 * offsetY;
if (matrix._type != MatrixTypes.Unknown)
{
matrix._type |= MatrixTypes.Translation;
}
}
}
}
}