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
/// Represents a 2D affine transformation matrix.
///
/// The matrix is a 2x3 transformation:
/// [ [a, c, tx],
/// [b, d, ty] ]
///
/// It supports translation and rotation, and can be composed or inverted.
#[derive(Debug, Clone, Copy)]
pub struct AffineTransform {
/// The 2x3 transformation matrix: [ [a, c, tx], [b, d, ty] ]
pub matrix: [[f32; 3]; 2],
}
impl AffineTransform {
/// Returns the identity transform.
///
/// This is equivalent to no transformation.
pub fn identity() -> Self {
Self {
matrix: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
}
}
/// Creates a translation transform by (tx, ty).
pub fn translate(tx: f32, ty: f32) -> Self {
Self {
matrix: [[1.0, 0.0, tx], [0.0, 1.0, ty]],
}
}
/// Creates a rotation transform in degrees, counter-clockwise.
pub fn rotate(degrees: f32) -> Self {
let rad = degrees.to_radians();
let (sin, cos) = rad.sin_cos();
Self {
matrix: [[cos, -sin, 0.0], [sin, cos, 0.0]],
}
}
/// Creates a combined transform of translation followed by rotation.
pub fn new(tx: f32, ty: f32, rotation: f32) -> Self {
Self::translate(tx, ty).compose(&Self::rotate(rotation))
}
pub fn x(&self) -> f32 {
self.matrix[0][2]
}
pub fn y(&self) -> f32 {
self.matrix[1][2]
}
/// Composes this transform with another.
///
/// This is equivalent to applying `other` after `self`.
pub fn compose(&self, other: &Self) -> Self {
let a = self.matrix;
let b = other.matrix;
Self {
matrix: [
[
a[0][0] * b[0][0] + a[0][1] * b[1][0],
a[0][0] * b[0][1] + a[0][1] * b[1][1],
a[0][0] * b[0][2] + a[0][1] * b[1][2] + a[0][2],
],
[
a[1][0] * b[0][0] + a[1][1] * b[1][0],
a[1][0] * b[0][1] + a[1][1] * b[1][1],
a[1][0] * b[0][2] + a[1][1] * b[1][2] + a[1][2],
],
],
}
}
/// Returns the inverse of this affine transform, if it exists.
///
/// Returns `None` if the matrix is singular (i.e. non-invertible).
pub fn inverse(&self) -> Option<Self> {
let [[a, c, tx], [b, d, ty]] = self.matrix;
let det = a * d - b * c;
if det.abs() < std::f32::EPSILON {
return None;
}
let inv_det = 1.0 / det;
let a_inv = d * inv_det;
let b_inv = -b * inv_det;
let c_inv = -c * inv_det;
let d_inv = a * inv_det;
let tx_inv = -(a_inv * tx + c_inv * ty);
let ty_inv = -(b_inv * tx + d_inv * ty);
Some(Self {
matrix: [[a_inv, c_inv, tx_inv], [b_inv, d_inv, ty_inv]],
})
}
/// Sets the translation components of the transform.
/// This preserves any existing rotation.
pub fn set_translation(&mut self, tx: f32, ty: f32) {
self.matrix[0][2] = tx;
self.matrix[1][2] = ty;
}
/// Sets the rotation of the transform in radians.
/// This preserves any existing translation.
pub fn set_rotation(&mut self, angle: f32) {
let (sin, cos) = angle.sin_cos();
self.matrix[0][0] = cos;
self.matrix[0][1] = -sin;
self.matrix[1][0] = sin;
self.matrix[1][1] = cos;
}
/// Returns the rotation angle in radians.
pub fn rotation(&self) -> f32 {
self.matrix[1][0].atan2(self.matrix[0][0])
}
}