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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::ops::{Mul, MulAssign};
use crate::Vec2;
#[derive(Clone, Copy, Debug)]
pub struct Affine([f64; 6]);
impl Affine {
#[inline]
pub fn new(c: [f64; 6]) -> Affine {
Affine(c)
}
#[inline]
pub fn scale(s: f64) -> Affine {
Affine([s, 0.0, 0.0, s, 0.0, 0.0])
}
#[inline]
pub fn rotate(th: f64) -> Affine {
let s = th.sin();
let c = th.cos();
Affine([c, s, -s, c, 0.0, 0.0])
}
#[inline]
pub fn translate<V: Into<Vec2>>(p: V) -> Affine {
let p = p.into();
Affine([1.0, 0.0, 0.0, 1.0, p.x, p.y])
}
#[inline]
pub fn as_coeffs(self) -> [f64; 6] {
self.0
}
}
impl Default for Affine {
#[inline]
fn default() -> Affine {
Affine::scale(1.0)
}
}
impl Mul<Vec2> for Affine {
type Output = Vec2;
#[inline]
fn mul(self, other: Vec2) -> Vec2 {
Vec2::new(
self.0[0] * other.x + self.0[2] * other.y + self.0[4],
self.0[1] * other.x + self.0[3] * other.y + self.0[5],
)
}
}
impl Mul for Affine {
type Output = Affine;
#[inline]
fn mul(self, other: Affine) -> Affine {
Affine([
self.0[0] * other.0[0] + self.0[2] * other.0[1],
self.0[1] * other.0[0] + self.0[3] * other.0[1],
self.0[0] * other.0[2] + self.0[2] * other.0[3],
self.0[1] * other.0[2] + self.0[3] * other.0[3],
self.0[0] * other.0[4] + self.0[2] * other.0[5] + self.0[4],
self.0[1] * other.0[4] + self.0[3] * other.0[5] + self.0[5],
])
}
}
impl MulAssign for Affine {
#[inline]
fn mul_assign(&mut self, other: Affine) {
*self = self.mul(other);
}
}
impl Mul<Affine> for f64 {
type Output = Affine;
#[inline]
fn mul(self, other: Affine) -> Affine {
Affine([
self * other.0[0],
self * other.0[1],
self * other.0[2],
self * other.0[3],
self * other.0[4],
self * other.0[5],
])
}
}
#[cfg(feature = "mint")]
impl From<Affine> for mint::ColumnMatrix2x3<f64> {
#[inline]
fn from(a: Affine) -> mint::ColumnMatrix2x3<f64> {
mint::ColumnMatrix2x3 {
x: mint::Vector2 {
x: a.0[0],
y: a.0[1],
},
y: mint::Vector2 {
x: a.0[2],
y: a.0[3],
},
z: mint::Vector2 {
x: a.0[4],
y: a.0[5],
},
}
}
}
#[cfg(feature = "mint")]
impl From<mint::ColumnMatrix2x3<f64>> for Affine {
#[inline]
fn from(m: mint::ColumnMatrix2x3<f64>) -> Affine {
Affine([m.x.x, m.x.y, m.y.x, m.y.y, m.z.x, m.z.y])
}
}
#[cfg(test)]
mod tests {
use crate::{Affine, Vec2};
use std::f64::consts::PI;
fn assert_near(p0: Vec2, p1: Vec2) {
assert!((p1 - p0).hypot() < 1e-9, "{:?} != {:?}", p0, p1);
}
#[test]
fn affine_basic() {
let p = Vec2::new(3.0, 4.0);
assert_near(Affine::default() * p, p);
assert_near(Affine::scale(2.0) * p, Vec2::new(6.0, 8.0));
assert_near(Affine::rotate(0.0) * p, p);
assert_near(Affine::rotate(PI / 2.0) * p, Vec2::new(-4.0, 3.0));
assert_near(Affine::translate((5.0, 6.0)) * p, Vec2::new(8.0, 10.0));
}
#[test]
fn affine_mul() {
let a1 = Affine::new([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let a2 = Affine::new([0.1, 1.2, 2.3, 3.4, 4.5, 5.6]);
let px = Vec2::new(1.0, 0.0);
let py = Vec2::new(0.0, 1.0);
assert_near(a1 * (a2 * px), (a1 * a2) * px);
assert_near(a1 * (a2 * py), (a1 * a2) * py);
assert_near(a1 * (a2 * (px + py)), (a1 * a2) * (px + py));
}
}