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