1use crate::geometry::Point;
2
3const TWO_PI: f32 = 2.0 * core::f32::consts::PI;
4
5pub(crate) fn sin_cos(x: f32) -> (f32, f32) {
6 let mut theta = x % TWO_PI;
7 if theta < 0.0 {
8 theta += TWO_PI;
9 }
10
11 let (sign_sin, sign_cos, y) = if theta <= core::f32::consts::FRAC_PI_2 {
12 (1.0, 1.0, theta)
13 } else if theta <= core::f32::consts::PI {
14 (1.0, -1.0, core::f32::consts::PI - theta)
15 } else if theta <= 3.0 * core::f32::consts::FRAC_PI_2 {
16 (-1.0, -1.0, theta - core::f32::consts::PI)
17 } else {
18 (-1.0, 1.0, TWO_PI - theta)
19 };
20
21 let y2 = y * y;
22 let y3 = y2 * y;
23 let y5 = y3 * y2;
24 let y7 = y5 * y2;
25 let y9 = y7 * y2;
26
27 let sin = y - y3 / 6.0 + y5 / 120.0 - y7 / 5_040.0 + y9 / 362_880.0;
28
29 let y4 = y2 * y2;
30 let y6 = y4 * y2;
31 let y8 = y6 * y2;
32 let cos = 1.0 - y2 / 2.0 + y4 / 24.0 - y6 / 720.0 + y8 / 40_320.0;
33
34 (sign_sin * sin, sign_cos * cos)
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Default)]
38pub struct Transform {
39 pub(crate) m11: f32,
40 pub(crate) m12: f32,
41 pub(crate) m21: f32,
42 pub(crate) m22: f32,
43 pub(crate) m31: f32,
44 pub(crate) m32: f32,
45}
46
47impl Transform {
48 pub const fn identity() -> Self {
49 Self {
50 m11: 1.0,
51 m12: 0.0,
52 m21: 0.0,
53 m22: 1.0,
54 m31: 0.0,
55 m32: 0.0,
56 }
57 }
58
59 pub const fn translate(tx: f32, ty: f32) -> Self {
60 Self {
61 m11: 1.0,
62 m12: 0.0,
63 m21: 0.0,
64 m22: 1.0,
65 m31: tx,
66 m32: ty,
67 }
68 }
69
70 pub const fn scale(sx: f32, sy: f32) -> Self {
71 Self {
72 m11: sx,
73 m12: 0.0,
74 m21: 0.0,
75 m22: sy,
76 m31: 0.0,
77 m32: 0.0,
78 }
79 }
80
81 pub fn rotate(angle_rad: f32) -> Self {
82 let (sin, cos) = sin_cos(angle_rad);
83 Self {
84 m11: cos,
85 m12: sin,
86 m21: -sin,
87 m22: cos,
88 m31: 0.0,
89 m32: 0.0,
90 }
91 }
92
93 pub fn then(&self, other: &Transform) -> Self {
94 Self {
95 m11: self.m11 * other.m11 + self.m12 * other.m21,
96 m12: self.m11 * other.m12 + self.m12 * other.m22,
97 m21: self.m21 * other.m11 + self.m22 * other.m21,
98 m22: self.m21 * other.m12 + self.m22 * other.m22,
99 m31: self.m31 * other.m11 + self.m32 * other.m21 + other.m31,
100 m32: self.m31 * other.m12 + self.m32 * other.m22 + other.m32,
101 }
102 }
103
104 pub fn apply(&self, point: Point<f32>) -> Point<f32> {
105 Point::new(
106 self.m11 * point.x + self.m21 * point.y + self.m31,
107 self.m12 * point.x + self.m22 * point.y + self.m32,
108 )
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115 use crate::geometry::Pointf;
116
117 #[test]
118 fn identity_transform() {
119 let t = Transform::identity();
120 assert_eq!(t.apply(Pointf::new(3.0, 4.0)), Pointf::new(3.0, 4.0));
121 }
122
123 #[test]
124 fn translate_transform() {
125 let t = Transform::translate(2.0, 3.0);
126 assert_eq!(t.apply(Pointf::new(1.0, 1.0)), Pointf::new(3.0, 4.0));
127 }
128
129 #[test]
130 fn scale_transform() {
131 let t = Transform::scale(2.0, -1.0);
132 assert_eq!(t.apply(Pointf::new(3.0, 4.0)), Pointf::new(6.0, -4.0));
133 }
134
135 #[test]
136 fn rotate_transform() {
137 let t = Transform::rotate(0.0);
138 assert_eq!(t.apply(Pointf::new(1.0, 0.0)).x, 1.0);
139 }
140
141 #[test]
142 fn compose_transforms() {
143 let translate = Transform::translate(1.0, 0.0);
144 let scale = Transform::scale(2.0, 2.0);
145 let composed = scale.then(&translate);
146 assert_eq!(composed.apply(Pointf::new(1.0, 1.0)), Pointf::new(3.0, 2.0));
147 }
148}