Add Degrees and Radians as newtypes
[kaka/rust-sdl-test.git] / src / common.rs
CommitLineData
2836f506 1use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg};
296187ca 2
b0566120
TW
3pub type Nanoseconds = u64;
4
787dbfb4 5#[macro_export]
296187ca 6macro_rules! point {
6ba7aef1
TW
7 ( $x:expr, $y:expr ) => {
8 Point2D { x: $x, y: $y }
9 };
296187ca
TW
10}
11
b0566120 12#[derive(Debug, Default, Copy, Clone, PartialEq)]
296187ca
TW
13pub struct Point2D<T> {
14 pub x: T,
15 pub y: T,
16}
17
18impl Point2D<f64> {
19 pub fn length(self) -> f64 {
20 ((self.x * self.x) + (self.y * self.y)).sqrt()
21 }
22}
23
6cd86b94
TW
24macro_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 }
2836f506 35 }
2836f506 36
6cd86b94
TW
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 }
2836f506
TW
44 }
45 }
46}
47
6cd86b94
TW
48point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y);
49point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y);
50point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y);
51point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y);
52point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
53point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
54point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
55point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
2836f506
TW
56
57////////// multiply point with scalar //////////////////////////////////////////
58impl<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
69impl<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
2836f506
TW
78////////// divide point with scalar ////////////////////////////////////////////
79impl<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
90impl<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
2836f506
TW
99impl<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 }
296187ca
TW
107 }
108}
109
b0566120
TW
110impl<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
e58a1769
TW
119impl 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
128impl 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)]
138struct Degrees(f64);
139#[derive(Debug, PartialEq, Clone, Copy)]
140struct Radians(f64);
141
142impl Degrees {
143 fn to_radians(&self) -> Radians {
144 Radians(self.0 * std::f64::consts::PI / 180.0)
145 }
146}
147
148impl Radians {
149 fn to_degrees(&self) -> Degrees {
150 Degrees(self.0 * 180.0 * std::f64::consts::FRAC_1_PI)
151 }
152}
153
0b5024d1
TW
154#[macro_export]
155macro_rules! rect {
156 ( $x:expr, $y:expr ) => {
157 Rect { x: $x, y: $y }
158 };
159}
160
6edafdc0
TW
161#[derive(Default)]
162pub struct Rect<T> {
163 pub width: T,
164 pub height: T,
165}
166
6ba7aef1 167impl<T: Mul<Output = T> + Copy> Rect<T> {
6edafdc0
TW
168 #[allow(dead_code)]
169 pub fn area(&self) -> T {
6ba7aef1 170 self.width * self.height
6edafdc0
TW
171 }
172}
173
174impl<T> From<(T, T)> for Rect<T> {
175 fn from(item: (T, T)) -> Self {
6ba7aef1
TW
176 Rect {
177 width: item.0,
178 height: item.1,
179 }
6edafdc0
TW
180 }
181}
182
296187ca
TW
183#[cfg(test)]
184mod 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));
2836f506
TW
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));
6cd86b94 211 assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3));
2836f506
TW
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));
6cd86b94 223 assert_eq!(point!(1, 0) * (2, 3), point!(2, 0));
2836f506
TW
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));
6cd86b94 235 assert_eq!(point!(6, 3) / (2, 3), point!(3, 1));
2836f506
TW
236 }
237
238 #[test]
239 fn neg_point() {
240 assert_eq!(point!(1, 1), -point!(-1, -1));
296187ca 241 }
6edafdc0
TW
242
243 #[test]
e58a1769
TW
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]
6edafdc0
TW
253 fn area_for_rect_of_multipliable_type() {
254 let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
6ba7aef1 255 assert_eq!(r.area(), 30 * 20);
6edafdc0
TW
256 // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
257 }
296187ca 258}