11using Synercoding . Primitives ;
22using System ;
3+ using System . Runtime . CompilerServices ;
34
45namespace Synercoding . FileFormats . Pdf . Primitives
56{
@@ -14,8 +15,21 @@ namespace Synercoding.FileFormats.Pdf.Primitives
1415 /// E F 1
1516 /// </code>
1617 /// </remarks>
17- public readonly struct Matrix
18+ public readonly struct Matrix : IEquatable < Matrix >
1819 {
20+ /// <summary>
21+ /// The identity matrix
22+ /// </summary>
23+ /// <remarks>
24+ /// This matrix can be interpreted as:
25+ /// <list type="bullet">
26+ /// <item>translation with (0,0)</item>
27+ /// <item>rotation with 0 degrees</item>
28+ /// <item>scaling with (1,1)</item>
29+ /// </list>
30+ /// </remarks>
31+ public static Matrix Identity { get ; } = new Matrix ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
32+
1933 /// <summary>
2034 /// Copy constructor for the <see cref="Matrix"/>
2135 /// </summary>
@@ -52,32 +66,54 @@ public Matrix(double a, double b, double c, double d, double e, double f)
5266 /// <summary>
5367 /// The A value
5468 /// </summary>
55- public double A { get ; }
69+ public double A { get ; init ; }
5670
5771 /// <summary>
5872 /// The B value
5973 /// </summary>
60- public double B { get ; }
74+ public double B { get ; init ; }
6175
6276 /// <summary>
6377 /// The C value
6478 /// </summary>
65- public double C { get ; }
79+ public double C { get ; init ; }
6680
6781 /// <summary>
6882 /// The D value
6983 /// </summary>
70- public double D { get ; }
84+ public double D { get ; init ; }
7185
7286 /// <summary>
7387 /// The E value
7488 /// </summary>
75- public double E { get ; }
89+ public double E { get ; init ; }
7690
7791 /// <summary>
7892 /// The F value
7993 /// </summary>
80- public double F { get ; }
94+ public double F { get ; init ; }
95+
96+ /// <inheritdoc/>
97+ public override bool Equals ( object ? obj )
98+ => obj is Rectangle other && Equals ( other ) ;
99+
100+ /// <inheritdoc/>
101+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
102+ public bool Equals ( Matrix other )
103+ => A . Equals ( other . A )
104+ && B . Equals ( other . B )
105+ && C . Equals ( other . C )
106+ && D . Equals ( other . D )
107+ && E . Equals ( other . E )
108+ && F . Equals ( other . F ) ;
109+
110+ /// <inheritdoc/>
111+ public override int GetHashCode ( )
112+ => HashCode . Combine ( A , B , C , D , E , F ) ;
113+
114+ /// <inheritdoc/>
115+ public override string ToString ( )
116+ => $ "Matrix [ { A } , { B } , { C } , { D } , { E } , { F } ]";
81117
82118 /// <summary>
83119 /// Apply a rotation operation on the matrix
@@ -87,6 +123,20 @@ public Matrix(double a, double b, double c, double d, double e, double f)
87123 public Matrix Rotate ( double degrees )
88124 => Multiply ( CreateRotationMatrix ( degrees ) ) ;
89125
126+ /// <summary>
127+ /// Apply a horizontal flip operation on the matrix
128+ /// </summary>
129+ /// <returns>The new <see cref="Matrix"/></returns>
130+ public Matrix FlipHorizontal ( )
131+ => Multiply ( CreateScaleMatrix ( - 1 , 1 ) ) ;
132+
133+ /// <summary>
134+ /// Apply a vertical flip operation on the matrix
135+ /// </summary>
136+ /// <returns>The new <see cref="Matrix"/></returns>
137+ public Matrix FlipVertical ( )
138+ => Multiply ( CreateScaleMatrix ( 1 , - 1 ) ) ;
139+
90140 /// <summary>
91141 /// Apply a scale operation on the matrix
92142 /// </summary>
@@ -146,19 +196,15 @@ public Matrix Translate(Value x, Value y)
146196 /// </summary>
147197 /// <param name="other">The matrix to multiply with</param>
148198 /// <returns>The new <see cref="Matrix"/></returns>
199+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
149200 public Matrix Multiply ( in Matrix other )
150- {
151- double a , b , c , d , e , f ;
152-
153- a = ( A * other . A ) + ( B * other . C ) ;
154- b = ( A * other . B ) + ( B * other . D ) ;
155- c = ( C * other . A ) + ( D * other . C ) ;
156- d = ( C * other . B ) + ( D * other . D ) ;
157- e = ( E * other . A ) + ( F * other . C ) + other . E ;
158- f = ( E * other . B ) + ( F * other . D ) + other . F ;
159-
160- return new Matrix ( a , b , c , d , e , f ) ;
161- }
201+ => new Matrix (
202+ a : ( A * other . A ) + ( B * other . C ) ,
203+ b : ( A * other . B ) + ( B * other . D ) ,
204+ c : ( C * other . A ) + ( D * other . C ) ,
205+ d : ( C * other . B ) + ( D * other . D ) ,
206+ e : ( E * other . A ) + ( F * other . C ) + other . E ,
207+ f : ( E * other . B ) + ( F * other . D ) + other . F ) ;
162208
163209 /// <summary>
164210 /// Create a matrix used for rotation
@@ -234,9 +280,8 @@ public static Matrix CreateTranslationMatrix(Value x, Value y)
234280 0 , 1 ,
235281 x . ConvertTo ( Unit . Points ) . Raw , y . ConvertTo ( Unit . Points ) . Raw ) ;
236282
283+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
237284 private static double _degreeToRad ( double degree )
238- {
239- return degree * Math . PI / 180 ;
240- }
285+ => degree * Math . PI / 180 ;
241286 }
242287}
0 commit comments