Fleshed out the controller
[kaka/rust-sdl-test.git] / src / common.rs
1 use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg};
2
3 pub type Nanoseconds = u64;
4
5 #[macro_export]
6 macro_rules! point {
7     ( $x:expr, $y:expr ) => {
8         Point2D { x: $x, y: $y }
9     };
10 }
11
12 #[derive(Debug, Default, Copy, Clone, PartialEq)]
13 pub struct Point2D<T> {
14     pub x: T,
15     pub y: T,
16 }
17
18 impl Point2D<f64> {
19     pub fn length(self) -> f64 {
20         ((self.x * self.x) + (self.y * self.y)).sqrt()
21     }
22
23     pub fn to_i32(self) -> Point2D<i32> {
24         Point2D {
25             x: self.x as i32,
26             y: self.y as i32,
27         }
28     }
29 }
30
31 macro_rules! point_op {
32     ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => {
33         impl<T: $trait<Output = T>> $trait<$Rhs> for Point2D<T> {
34             type Output = Self;
35
36             fn $fn(self, $rhs: $Rhs) -> Self {
37                 Self {
38                     x: self.x $op $x,
39                     y: self.y $op $y,
40                 }
41             }
42         }
43
44         impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point2D<T> {
45             fn $fn_assign(&mut self, $rhs: $Rhs) {
46                 *self = Self {
47                     x: self.x $op $x,
48                     y: self.y $op $y,
49                 }
50             }
51         }
52     }
53 }
54
55 point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y);
56 point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y);
57 point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y);
58 point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y);
59 point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
60 point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
61 point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
62 point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
63
64 ////////// multiply point with scalar //////////////////////////////////////////
65 impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> {
66     type Output = Self;
67
68     fn mul(self, rhs: T) -> Self {
69         Self {
70             x: self.x * rhs,
71             y: self.y * rhs,
72         }
73     }
74 }
75
76 impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> {
77     fn mul_assign(&mut self, rhs: T) {
78         *self = Self {
79             x: self.x * rhs,
80             y: self.y * rhs,
81         }
82     }
83 }
84
85 ////////// divide point with scalar ////////////////////////////////////////////
86 impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
87     type Output = Self;
88
89     fn div(self, rhs: T) -> Self {
90         Self {
91             x: self.x / rhs,
92             y: self.y / rhs,
93         }
94     }
95 }
96
97 impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> {
98     fn div_assign(&mut self, rhs: T) {
99         *self = Self {
100             x: self.x / rhs,
101             y: self.y / rhs,
102         }
103     }
104 }
105
106 impl<T: Neg<Output = T>> Neg for Point2D<T> {
107     type Output = Self;
108
109     fn neg(self) -> Self {
110         Self {
111             x: -self.x,
112             y: -self.y,
113         }
114     }
115 }
116
117 impl<T> From<(T, T)> for Point2D<T> {
118     fn from(item: (T, T)) -> Self {
119         Point2D {
120             x: item.0,
121             y: item.1,
122         }
123     }
124 }
125
126 impl<T> From<Point2D<T>> for (T, T) {
127     fn from(item: Point2D<T>) -> Self {
128         (item.x, item.y)
129     }
130 }
131
132 impl From<Degrees> for Point2D<f64> {
133     fn from(item: Degrees) -> Self {
134         Point2D {
135             x: (item.0 * std::f64::consts::PI / 180.0).cos(),
136             y: (item.0 * std::f64::consts::PI / 180.0).sin(),
137         }
138     }
139 }
140
141 impl From<Radians> for Point2D<f64> {
142     fn from(item: Radians) -> Self {
143         Point2D {
144             x: item.0.cos(),
145             y: item.0.sin(),
146         }
147     }
148 }
149
150 #[derive(Debug, Default, PartialEq, Clone, Copy)]
151 pub struct Degrees(pub f64);
152 #[derive(Debug, Default, PartialEq, Clone, Copy)]
153 pub struct Radians(pub f64);
154
155 impl Degrees {
156     #[allow(dead_code)]
157     fn to_radians(&self) -> Radians {
158         Radians(self.0 * std::f64::consts::PI / 180.0)
159     }
160 }
161
162 impl Radians {
163     #[allow(dead_code)]
164     fn to_degrees(&self) -> Degrees {
165         Degrees(self.0 * 180.0 * std::f64::consts::FRAC_1_PI)
166     }
167 }
168
169 #[macro_export]
170 macro_rules! rect {
171     ( $x:expr, $y:expr ) => {
172         Rect { x: $x, y: $y }
173     };
174 }
175
176 #[derive(Default)]
177 pub struct Rect<T> {
178     pub width: T,
179     pub height: T,
180 }
181
182 impl<T: Mul<Output = T> + Copy> Rect<T> {
183     #[allow(dead_code)]
184     pub fn area(&self) -> T {
185         self.width * self.height
186     }
187 }
188
189 impl<T> From<(T, T)> for Rect<T> {
190     fn from(item: (T, T)) -> Self {
191         Rect {
192             width: item.0,
193             height: item.1,
194         }
195     }
196 }
197
198 #[cfg(test)]
199 mod tests {
200     use super::*;
201
202     #[test]
203     fn immutable_copy_of_point() {
204         let a = point!(0, 0);
205         let mut b = a; // Copy
206         assert_eq!(a, b); // PartialEq
207         b.x = 1;
208         assert_ne!(a, b); // PartialEq
209     }
210
211     #[test]
212     fn add_points() {
213         let mut a = point!(1, 0);
214         assert_eq!(a + point!(2, 2), point!(3, 2)); // Add
215         a += point!(2, 2); // AddAssign
216         assert_eq!(a, point!(3, 2));
217         assert_eq!(point!(1, 0) + (2, 3), point!(3, 3));
218     }
219
220     #[test]
221     fn sub_points() {
222         let mut a = point!(1, 0);
223         assert_eq!(a - point!(2, 2), point!(-1, -2));
224         a -= point!(2, 2);
225         assert_eq!(a, point!(-1, -2));
226         assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3));
227     }
228
229     #[test]
230     fn mul_points() {
231         let mut a = point!(1, 2);
232         assert_eq!(a * 2, point!(2, 4));
233         assert_eq!(a * point!(2, 3), point!(2, 6));
234         a *= 2;
235         assert_eq!(a, point!(2, 4));
236         a *= point!(3, 1);
237         assert_eq!(a, point!(6, 4));
238         assert_eq!(point!(1, 0) * (2, 3), point!(2, 0));
239     }
240
241     #[test]
242     fn div_points() {
243         let mut a = point!(4, 8);
244         assert_eq!(a / 2, point!(2, 4));
245         assert_eq!(a / point!(2, 4), point!(2, 2));
246         a /= 2;
247         assert_eq!(a, point!(2, 4));
248         a /= point!(2, 4);
249         assert_eq!(a, point!(1, 1));
250         assert_eq!(point!(6, 3) / (2, 3), point!(3, 1));
251     }
252
253     #[test]
254     fn neg_point() {
255         assert_eq!(point!(1, 1), -point!(-1, -1));
256     }
257
258     #[test]
259     fn angles() {
260         assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0));
261         assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0));
262         assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI));
263         assert!((Point2D::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
264         assert!((Point2D::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
265     }
266
267     #[test]
268     fn area_for_rect_of_multipliable_type() {
269         let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
270         assert_eq!(r.area(), 30 * 20);
271         // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
272     }
273 }