@@ -13,16 +13,12 @@ namespace Microsoft.Toolkit.Uwp.UI
13
13
public static class MatrixExtensions
14
14
{
15
15
/// <summary>
16
- /// Implements WPF's Matrix.HasInverse.
16
+ /// Implements WPF's <c> Matrix.HasInverse</c> logic .
17
17
/// </summary>
18
18
/// <param name="matrix">The matrix.</param>
19
19
/// <returns>True if matrix has an inverse.</returns>
20
20
public static bool HasInverse ( this Matrix matrix )
21
21
{
22
- // TODO: Check if we can make this an extension property in C#8.
23
-
24
- // WPF equivalent of following code:
25
- // return matrix.HasInverse;
26
22
return ( ( matrix . M11 * matrix . M22 ) - ( matrix . M12 * matrix . M21 ) ) != 0 ;
27
23
}
28
24
@@ -34,7 +30,30 @@ public static bool HasInverse(this Matrix matrix)
34
30
/// <returns>Multiplied Matrix</returns>
35
31
public static Matrix Multiply ( this Matrix matrix1 , Matrix matrix2 )
36
32
{
37
- return MatrixHelperEx . Multiply ( matrix1 , matrix2 ) ;
33
+ return new (
34
+ ( matrix1 . M11 * matrix2 . M11 ) + ( matrix1 . M12 * matrix2 . M21 ) ,
35
+ ( matrix1 . M11 * matrix2 . M12 ) + ( matrix1 . M12 * matrix2 . M22 ) ,
36
+ ( matrix1 . M21 * matrix2 . M11 ) + ( matrix1 . M22 * matrix2 . M21 ) ,
37
+ ( matrix1 . M21 * matrix2 . M12 ) + ( matrix1 . M22 * matrix2 . M22 ) ,
38
+ ( matrix1 . OffsetX * matrix2 . M11 ) + ( matrix1 . OffsetY * matrix2 . M21 ) + matrix2 . OffsetX ,
39
+ ( matrix1 . OffsetX * matrix2 . M12 ) + ( matrix1 . OffsetY * matrix2 . M22 ) + matrix2 . OffsetY ) ;
40
+ }
41
+
42
+ /// <summary>
43
+ /// Rounds the non-offset elements of a matrix to avoid issues due to floating point imprecision and returns the result.
44
+ /// </summary>
45
+ /// <param name="matrix">The matrix to round.</param>
46
+ /// <param name="decimalsAfterRound">The number of decimals after the round.</param>
47
+ /// <returns>The rounded matrix.</returns>
48
+ public static Matrix Round ( this Matrix matrix , int decimalsAfterRound )
49
+ {
50
+ return new (
51
+ Math . Round ( matrix . M11 , decimalsAfterRound ) ,
52
+ Math . Round ( matrix . M12 , decimalsAfterRound ) ,
53
+ Math . Round ( matrix . M21 , decimalsAfterRound ) ,
54
+ Math . Round ( matrix . M22 , decimalsAfterRound ) ,
55
+ matrix . OffsetX ,
56
+ matrix . OffsetY ) ;
38
57
}
39
58
40
59
/// <summary>
@@ -108,53 +127,37 @@ public static Matrix Skew(this Matrix matrix, double skewX, double skewY)
108
127
/// <returns>Translated Matrix.</returns>
109
128
public static Matrix Translate ( this Matrix matrix , double offsetX , double offsetY )
110
129
{
111
- return new Matrix ( matrix . M11 , matrix . M12 , matrix . M21 , matrix . M22 , matrix . OffsetX + offsetX , matrix . OffsetY + offsetY ) ;
130
+ return new ( matrix . M11 , matrix . M12 , matrix . M21 , matrix . M22 , matrix . OffsetX + offsetX , matrix . OffsetY + offsetY ) ;
112
131
}
113
132
114
- internal static Matrix CreateRotationRadians ( double angle )
133
+ private static Matrix CreateRotationRadians ( double angle )
115
134
{
116
135
return CreateRotationRadians ( angle , 0 , 0 ) ;
117
136
}
118
137
119
- internal static Matrix CreateRotationRadians ( double angle , double centerX , double centerY )
138
+ private static Matrix CreateRotationRadians ( double angle , double centerX , double centerY )
120
139
{
121
140
var sin = Math . Sin ( angle ) ;
122
141
var cos = Math . Cos ( angle ) ;
123
142
var dx = ( centerX * ( 1.0 - cos ) ) + ( centerY * sin ) ;
124
143
var dy = ( centerY * ( 1.0 - cos ) ) - ( centerX * sin ) ;
125
144
126
- #pragma warning disable SA1117 // Parameters must be on same line or separate lines
127
- return new Matrix ( cos , sin ,
128
- - sin , cos ,
129
- dx , dy ) ;
130
- #pragma warning restore SA1117 // Parameters must be on same line or separate lines
145
+ return new ( cos , sin , - sin , cos , dx , dy ) ;
131
146
}
132
147
133
- internal static Matrix CreateScaling ( double scaleX , double scaleY )
148
+ private static Matrix CreateScaling ( double scaleX , double scaleY )
134
149
{
135
- #pragma warning disable SA1117 // Parameters must be on same line or separate lines
136
- return new Matrix ( scaleX , 0 ,
137
- 0 , scaleY ,
138
- 0 , 0 ) ;
139
- #pragma warning restore SA1117 // Parameters must be on same line or separate lines
150
+ return new ( scaleX , 0 , 0 , scaleY , 0 , 0 ) ;
140
151
}
141
152
142
- internal static Matrix CreateScaling ( double scaleX , double scaleY , double centerX , double centerY )
153
+ private static Matrix CreateScaling ( double scaleX , double scaleY , double centerX , double centerY )
143
154
{
144
- #pragma warning disable SA1117 // Parameters must be on same line or separate lines
145
- return new Matrix ( scaleX , 0 ,
146
- 0 , scaleY ,
147
- centerX - ( scaleX * centerX ) , centerY - ( scaleY * centerY ) ) ;
148
- #pragma warning restore SA1117 // Parameters must be on same line or separate lines
155
+ return new ( scaleX , 0 , 0 , scaleY , centerX - ( scaleX * centerX ) , centerY - ( scaleY * centerY ) ) ;
149
156
}
150
157
151
- internal static Matrix CreateSkewRadians ( double skewX , double skewY )
158
+ private static Matrix CreateSkewRadians ( double skewX , double skewY )
152
159
{
153
- #pragma warning disable SA1117 // Parameters must be on same line or separate lines
154
- return new Matrix ( 1.0 , Math . Tan ( skewY ) ,
155
- Math . Tan ( skewX ) , 1.0 ,
156
- 0.0 , 0.0 ) ;
157
- #pragma warning restore SA1117 // Parameters must be on same line or separate lines
160
+ return new ( 1.0 , Math . Tan ( skewY ) , Math . Tan ( skewX ) , 1.0 , 0.0 , 0.0 ) ;
158
161
}
159
162
}
160
163
}
0 commit comments