1use super::*;
2
3impl Matrix3x2 {
4 pub const fn identity() -> Self {
5 Self {
6 M11: 1.0,
7 M12: 0.0,
8 M21: 0.0,
9 M22: 1.0,
10 M31: 0.0,
11 M32: 0.0,
12 }
13 }
14 pub const fn translation(x: f32, y: f32) -> Self {
15 Self {
16 M11: 1.0,
17 M12: 0.0,
18 M21: 0.0,
19 M22: 1.0,
20 M31: x,
21 M32: y,
22 }
23 }
24 pub fn rotation(angle: f32) -> Self {
25 Self::rotation_around(angle, Vector2::zero())
26 }
27 pub fn rotation_around(angle: f32, center: Vector2) -> Self {
28 windows_link::link!("d2d1.dll" "system" fn D2D1MakeRotateMatrix(angle: f32, center: Vector2, matrix: *mut Matrix3x2));
29 let mut matrix = Self::default();
30 unsafe {
31 D2D1MakeRotateMatrix(angle, center, &mut matrix);
32 }
33 matrix
34 }
35 pub fn scale(scale_x: f32, scale_y: f32) -> Self {
36 Self::scale_around(scale_x, scale_y, Vector2::zero())
37 }
38 pub fn scale_around(scale_x: f32, scale_y: f32, center: Vector2) -> Self {
39 Self {
40 M11: scale_x,
41 M12: 0.0,
42 M21: 0.0,
43 M22: scale_y,
44 M31: center.X - scale_x * center.X,
45 M32: center.Y - scale_y * center.Y,
46 }
47 }
48 pub fn skew(angle_x: f32, angle_y: f32) -> Self {
49 Self::skew_around(angle_x, angle_y, Vector2::zero())
50 }
51 pub fn skew_around(angle_x: f32, angle_y: f32, center: Vector2) -> Self {
52 windows_link::link!("d2d1.dll" "system" fn D2D1MakeSkewMatrix(angle_x: f32, angle_y: f32, center: Vector2, matrix: *mut Matrix3x2));
53 let mut matrix = Self::default();
54 unsafe {
55 D2D1MakeSkewMatrix(angle_x, angle_y, center, &mut matrix);
56 }
57 matrix
58 }
59 fn impl_add(&self, rhs: &Self) -> Self {
60 Self {
61 M11: self.M11 + rhs.M11,
62 M12: self.M12 + rhs.M12,
63 M21: self.M21 + rhs.M21,
64 M22: self.M22 + rhs.M22,
65 M31: self.M31 + rhs.M31,
66 M32: self.M32 + rhs.M32,
67 }
68 }
69 fn impl_sub(&self, rhs: &Self) -> Self {
70 Self {
71 M11: self.M11 - rhs.M11,
72 M12: self.M12 - rhs.M12,
73 M21: self.M21 - rhs.M21,
74 M22: self.M22 - rhs.M22,
75 M31: self.M31 - rhs.M31,
76 M32: self.M32 - rhs.M32,
77 }
78 }
79 fn impl_mul(&self, rhs: &Self) -> Self {
80 Self {
81 M11: self.M11 * rhs.M11 + self.M12 * rhs.M21,
82 M12: self.M11 * rhs.M12 + self.M12 * rhs.M22,
83 M21: self.M21 * rhs.M11 + self.M22 * rhs.M21,
84 M22: self.M21 * rhs.M12 + self.M22 * rhs.M22,
85 M31: self.M31 * rhs.M11 + self.M32 * rhs.M21 + rhs.M31,
86 M32: self.M31 * rhs.M12 + self.M32 * rhs.M22 + rhs.M32,
87 }
88 }
89 fn impl_mul_f32(&self, rhs: f32) -> Self {
90 Self {
91 M11: self.M11 * rhs,
92 M12: self.M12 * rhs,
93 M21: self.M21 * rhs,
94 M22: self.M22 * rhs,
95 M31: self.M31 * rhs,
96 M32: self.M32 * rhs,
97 }
98 }
99}
100
101impl core::ops::Add<Matrix3x2> for Matrix3x2 {
102 type Output = Matrix3x2;
103 fn add(self, rhs: Matrix3x2) -> Matrix3x2 {
104 self.impl_add(&rhs)
105 }
106}
107impl core::ops::Add<&Matrix3x2> for Matrix3x2 {
108 type Output = Matrix3x2;
109 fn add(self, rhs: &Matrix3x2) -> Matrix3x2 {
110 self.impl_add(rhs)
111 }
112}
113impl core::ops::Add<Matrix3x2> for &Matrix3x2 {
114 type Output = Matrix3x2;
115 fn add(self, rhs: Matrix3x2) -> Matrix3x2 {
116 self.impl_add(&rhs)
117 }
118}
119impl core::ops::Add<&Matrix3x2> for &Matrix3x2 {
120 type Output = Matrix3x2;
121 fn add(self, rhs: &Matrix3x2) -> Matrix3x2 {
122 self.impl_add(rhs)
123 }
124}
125impl core::ops::Sub<Matrix3x2> for Matrix3x2 {
126 type Output = Matrix3x2;
127 fn sub(self, rhs: Matrix3x2) -> Matrix3x2 {
128 self.impl_sub(&rhs)
129 }
130}
131impl core::ops::Sub<&Matrix3x2> for Matrix3x2 {
132 type Output = Matrix3x2;
133 fn sub(self, rhs: &Matrix3x2) -> Matrix3x2 {
134 self.impl_sub(rhs)
135 }
136}
137impl core::ops::Sub<Matrix3x2> for &Matrix3x2 {
138 type Output = Matrix3x2;
139 fn sub(self, rhs: Matrix3x2) -> Matrix3x2 {
140 self.impl_sub(&rhs)
141 }
142}
143impl core::ops::Sub<&Matrix3x2> for &Matrix3x2 {
144 type Output = Matrix3x2;
145 fn sub(self, rhs: &Matrix3x2) -> Matrix3x2 {
146 self.impl_sub(rhs)
147 }
148}
149impl core::ops::Mul<Matrix3x2> for Matrix3x2 {
150 type Output = Matrix3x2;
151 fn mul(self, rhs: Matrix3x2) -> Matrix3x2 {
152 self.impl_mul(&rhs)
153 }
154}
155impl core::ops::Mul<&Matrix3x2> for Matrix3x2 {
156 type Output = Matrix3x2;
157 fn mul(self, rhs: &Matrix3x2) -> Matrix3x2 {
158 self.impl_mul(rhs)
159 }
160}
161impl core::ops::Mul<Matrix3x2> for &Matrix3x2 {
162 type Output = Matrix3x2;
163 fn mul(self, rhs: Matrix3x2) -> Matrix3x2 {
164 self.impl_mul(&rhs)
165 }
166}
167impl core::ops::Mul<&Matrix3x2> for &Matrix3x2 {
168 type Output = Matrix3x2;
169 fn mul(self, rhs: &Matrix3x2) -> Matrix3x2 {
170 self.impl_mul(rhs)
171 }
172}
173impl core::ops::Mul<f32> for Matrix3x2 {
174 type Output = Matrix3x2;
175 fn mul(self, rhs: f32) -> Matrix3x2 {
176 self.impl_mul_f32(rhs)
177 }
178}
179impl core::ops::Mul<f32> for &Matrix3x2 {
180 type Output = Matrix3x2;
181 fn mul(self, rhs: f32) -> Matrix3x2 {
182 self.impl_mul_f32(rhs)
183 }
184}
185