Fleshed out the controller
[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 }
bf7b5671
TW
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 }
296187ca
TW
29}
30
6cd86b94
TW
31macro_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 }
2836f506 42 }
2836f506 43
6cd86b94
TW
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 }
2836f506
TW
51 }
52 }
53}
54
6cd86b94
TW
55point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y);
56point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y);
57point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y);
58point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y);
59point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
60point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
61point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
62point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
2836f506
TW
63
64////////// multiply point with scalar //////////////////////////////////////////
65impl<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
76impl<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
2836f506
TW
85////////// divide point with scalar ////////////////////////////////////////////
86impl<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
97impl<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
2836f506
TW
106impl<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 }
296187ca
TW
114 }
115}
116
b0566120
TW
117impl<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
bf7b5671
TW
126impl<T> From<Point2D<T>> for (T, T) {
127 fn from(item: Point2D<T>) -> Self {
128 (item.x, item.y)
129 }
130}
131
e58a1769
TW
132impl 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
141impl 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
bf7b5671
TW
150#[derive(Debug, Default, PartialEq, Clone, Copy)]
151pub struct Degrees(pub f64);
152#[derive(Debug, Default, PartialEq, Clone, Copy)]
153pub struct Radians(pub f64);
e58a1769
TW
154
155impl Degrees {
bf7b5671 156 #[allow(dead_code)]
e58a1769
TW
157 fn to_radians(&self) -> Radians {
158 Radians(self.0 * std::f64::consts::PI / 180.0)
159 }
160}
161
162impl Radians {
bf7b5671 163 #[allow(dead_code)]
e58a1769
TW
164 fn to_degrees(&self) -> Degrees {
165 Degrees(self.0 * 180.0 * std::f64::consts::FRAC_1_PI)
166 }
167}
168
0b5024d1
TW
169#[macro_export]
170macro_rules! rect {
171 ( $x:expr, $y:expr ) => {
172 Rect { x: $x, y: $y }
173 };
174}
175
6edafdc0
TW
176#[derive(Default)]
177pub struct Rect<T> {
178 pub width: T,
179 pub height: T,
180}
181
6ba7aef1 182impl<T: Mul<Output = T> + Copy> Rect<T> {
6edafdc0
TW
183 #[allow(dead_code)]
184 pub fn area(&self) -> T {
6ba7aef1 185 self.width * self.height
6edafdc0
TW
186 }
187}
188
189impl<T> From<(T, T)> for Rect<T> {
190 fn from(item: (T, T)) -> Self {
6ba7aef1
TW
191 Rect {
192 width: item.0,
193 height: item.1,
194 }
6edafdc0
TW
195 }
196}
197
296187ca
TW
198#[cfg(test)]
199mod 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));
2836f506
TW
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));
6cd86b94 226 assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3));
2836f506
TW
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));
6cd86b94 238 assert_eq!(point!(1, 0) * (2, 3), point!(2, 0));
2836f506
TW
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));
6cd86b94 250 assert_eq!(point!(6, 3) / (2, 3), point!(3, 1));
2836f506
TW
251 }
252
253 #[test]
254 fn neg_point() {
255 assert_eq!(point!(1, 1), -point!(-1, -1));
296187ca 256 }
6edafdc0
TW
257
258 #[test]
e58a1769
TW
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]
6edafdc0
TW
268 fn area_for_rect_of_multipliable_type() {
269 let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
6ba7aef1 270 assert_eq!(r.area(), 30 * 20);
6edafdc0
TW
271 // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
272 }
296187ca 273}