1use super::*;
2
3impl Matrix3x2 {
4 pub const fn identity() -> Self {
6 Self {
7 m11: 1.0,
8 m12: 0.0,
9 m21: 0.0,
10 m22: 1.0,
11 m31: 0.0,
12 m32: 0.0,
13 }
14 }
15 pub const fn translation(x: f32, y: f32) -> Self {
17 Self {
18 m11: 1.0,
19 m12: 0.0,
20 m21: 0.0,
21 m22: 1.0,
22 m31: x,
23 m32: y,
24 }
25 }
26 #[cfg(feature = "std")]
28 pub fn rotation(angle: f32) -> Self {
29 Self::rotation_around(angle, Vector2::zero())
30 }
31 #[cfg(feature = "std")]
33 pub fn rotation_around(angle: f32, center: Vector2) -> Self {
34 let (sin, cos) = angle.to_radians().sin_cos();
35 Self {
36 m11: cos,
37 m12: sin,
38 m21: -sin,
39 m22: cos,
40 m31: center.x * (1.0 - cos) + center.y * sin,
41 m32: center.y * (1.0 - cos) - center.x * sin,
42 }
43 }
44 pub fn scale(scale_x: f32, scale_y: f32) -> Self {
46 Self::scale_around(scale_x, scale_y, Vector2::zero())
47 }
48 pub fn scale_around(scale_x: f32, scale_y: f32, center: Vector2) -> Self {
50 Self {
51 m11: scale_x,
52 m12: 0.0,
53 m21: 0.0,
54 m22: scale_y,
55 m31: center.x - scale_x * center.x,
56 m32: center.y - scale_y * center.y,
57 }
58 }
59 #[cfg(feature = "std")]
62 pub fn skew(angle_x: f32, angle_y: f32) -> Self {
63 Self::skew_around(angle_x, angle_y, Vector2::zero())
64 }
65 #[cfg(feature = "std")]
68 pub fn skew_around(angle_x: f32, angle_y: f32, center: Vector2) -> Self {
69 let tan_x = angle_x.to_radians().tan();
70 let tan_y = angle_y.to_radians().tan();
71 Self {
72 m11: 1.0,
73 m12: tan_y,
74 m21: tan_x,
75 m22: 1.0,
76 m31: -center.y * tan_x,
77 m32: -center.x * tan_y,
78 }
79 }
80 fn impl_add(&self, rhs: &Self) -> Self {
81 Self {
82 m11: self.m11 + rhs.m11,
83 m12: self.m12 + rhs.m12,
84 m21: self.m21 + rhs.m21,
85 m22: self.m22 + rhs.m22,
86 m31: self.m31 + rhs.m31,
87 m32: self.m32 + rhs.m32,
88 }
89 }
90 fn impl_sub(&self, rhs: &Self) -> Self {
91 Self {
92 m11: self.m11 - rhs.m11,
93 m12: self.m12 - rhs.m12,
94 m21: self.m21 - rhs.m21,
95 m22: self.m22 - rhs.m22,
96 m31: self.m31 - rhs.m31,
97 m32: self.m32 - rhs.m32,
98 }
99 }
100 fn impl_mul(&self, rhs: &Self) -> Self {
101 Self {
102 m11: self.m11 * rhs.m11 + self.m12 * rhs.m21,
103 m12: self.m11 * rhs.m12 + self.m12 * rhs.m22,
104 m21: self.m21 * rhs.m11 + self.m22 * rhs.m21,
105 m22: self.m21 * rhs.m12 + self.m22 * rhs.m22,
106 m31: self.m31 * rhs.m11 + self.m32 * rhs.m21 + rhs.m31,
107 m32: self.m31 * rhs.m12 + self.m32 * rhs.m22 + rhs.m32,
108 }
109 }
110 fn impl_mul_f32(&self, rhs: f32) -> Self {
111 Self {
112 m11: self.m11 * rhs,
113 m12: self.m12 * rhs,
114 m21: self.m21 * rhs,
115 m22: self.m22 * rhs,
116 m31: self.m31 * rhs,
117 m32: self.m32 * rhs,
118 }
119 }
120}
121
122impl core::ops::Add<Self> for Matrix3x2 {
123 type Output = Self;
124 fn add(self, rhs: Self) -> Self {
125 self.impl_add(&rhs)
126 }
127}
128impl core::ops::Add<&Self> for Matrix3x2 {
129 type Output = Self;
130 fn add(self, rhs: &Self) -> Self {
131 self.impl_add(rhs)
132 }
133}
134impl core::ops::Add<Matrix3x2> for &Matrix3x2 {
135 type Output = Matrix3x2;
136 fn add(self, rhs: Matrix3x2) -> Matrix3x2 {
137 self.impl_add(&rhs)
138 }
139}
140impl core::ops::Add<&Matrix3x2> for &Matrix3x2 {
141 type Output = Matrix3x2;
142 fn add(self, rhs: &Matrix3x2) -> Matrix3x2 {
143 self.impl_add(rhs)
144 }
145}
146impl core::ops::Sub<Self> for Matrix3x2 {
147 type Output = Self;
148 fn sub(self, rhs: Self) -> Self {
149 self.impl_sub(&rhs)
150 }
151}
152impl core::ops::Sub<&Self> for Matrix3x2 {
153 type Output = Self;
154 fn sub(self, rhs: &Self) -> Self {
155 self.impl_sub(rhs)
156 }
157}
158impl core::ops::Sub<Matrix3x2> for &Matrix3x2 {
159 type Output = Matrix3x2;
160 fn sub(self, rhs: Matrix3x2) -> Matrix3x2 {
161 self.impl_sub(&rhs)
162 }
163}
164impl core::ops::Sub<&Matrix3x2> for &Matrix3x2 {
165 type Output = Matrix3x2;
166 fn sub(self, rhs: &Matrix3x2) -> Matrix3x2 {
167 self.impl_sub(rhs)
168 }
169}
170impl core::ops::Mul<Self> for Matrix3x2 {
171 type Output = Self;
172 fn mul(self, rhs: Self) -> Self {
173 self.impl_mul(&rhs)
174 }
175}
176impl core::ops::Mul<&Self> for Matrix3x2 {
177 type Output = Self;
178 fn mul(self, rhs: &Self) -> Self {
179 self.impl_mul(rhs)
180 }
181}
182impl core::ops::Mul<Matrix3x2> for &Matrix3x2 {
183 type Output = Matrix3x2;
184 fn mul(self, rhs: Matrix3x2) -> Matrix3x2 {
185 self.impl_mul(&rhs)
186 }
187}
188impl core::ops::Mul<&Matrix3x2> for &Matrix3x2 {
189 type Output = Matrix3x2;
190 fn mul(self, rhs: &Matrix3x2) -> Matrix3x2 {
191 self.impl_mul(rhs)
192 }
193}
194impl core::ops::Mul<f32> for Matrix3x2 {
195 type Output = Self;
196 fn mul(self, rhs: f32) -> Self {
197 self.impl_mul_f32(rhs)
198 }
199}
200impl core::ops::Mul<f32> for &Matrix3x2 {
201 type Output = Matrix3x2;
202 fn mul(self, rhs: f32) -> Matrix3x2 {
203 self.impl_mul_f32(rhs)
204 }
205}