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