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