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