Collect wall points into walls and place them in a wall grid
[kaka/rust-sdl-test.git] / src / common / geometry.rs
index 1cf205e..5a03ffb 100644 (file)
@@ -3,17 +3,17 @@ use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, N
 #[macro_export]
 macro_rules! point {
     ( $x:expr, $y:expr ) => {
-        Point2D { x: $x, y: $y }
+        Point { x: $x, y: $y }
     };
 }
 
 #[derive(Debug, Default, Copy, Clone, PartialEq)]
-pub struct Point2D<T> {
+pub struct Point<T> {
     pub x: T,
     pub y: T,
 }
 
-impl Point2D<f64> {
+impl Point<f64> {
     pub fn length(&self) -> f64 {
         ((self.x * self.x) + (self.y * self.y)).sqrt()
     }
@@ -34,8 +34,8 @@ impl Point2D<f64> {
        self.to_radians().to_degrees()
     }
 
-    pub fn to_i32(self) -> Point2D<i32> {
-       Point2D {
+    pub fn to_i32(self) -> Point<i32> {
+       Point {
            x: self.x as i32,
            y: self.y as i32,
        }
@@ -44,7 +44,7 @@ impl Point2D<f64> {
 
 macro_rules! point_op {
     ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => {
-        impl<T: $trait<Output = T>> $trait<$Rhs> for Point2D<T> {
+        impl<T: $trait<Output = T>> $trait<$Rhs> for Point<T> {
             type Output = Self;
 
             fn $fn(self, $rhs: $Rhs) -> Self {
@@ -55,7 +55,7 @@ macro_rules! point_op {
             }
         }
 
-        impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point2D<T> {
+        impl<T: $trait<Output = T> + Copy> $trait_assign<$Rhs> for Point<T> {
             fn $fn_assign(&mut self, $rhs: $Rhs) {
                 *self = Self {
                     x: self.x $op $x,
@@ -66,17 +66,17 @@ macro_rules! point_op {
     }
 }
 
-point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D<T> => rhs.x, rhs.y);
-point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D<T> => rhs.x, rhs.y);
-point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D<T> => rhs.x, rhs.y);
-point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D<T> => rhs.x, rhs.y);
+point_op!(+, Add(add), AddAssign(add_assign), rhs = Point<T> => rhs.x, rhs.y);
+point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point<T> => rhs.x, rhs.y);
+point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point<T> => rhs.x, rhs.y);
+point_op!(/, Div(div), DivAssign(div_assign), rhs = Point<T> => rhs.x, rhs.y);
 point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
 point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
 point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
 point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
 
 ////////// multiply point with scalar //////////////////////////////////////////
-impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> {
+impl<T: Mul<Output = T> + Copy> Mul<T> for Point<T> {
     type Output = Self;
 
     fn mul(self, rhs: T) -> Self {
@@ -87,7 +87,7 @@ impl<T: Mul<Output = T> + Copy> Mul<T> for Point2D<T> {
     }
 }
 
-impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> {
+impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point<T> {
     fn mul_assign(&mut self, rhs: T) {
        *self = Self {
            x: self.x * rhs,
@@ -97,7 +97,7 @@ impl<T: Mul<Output = T> + Copy> MulAssign<T> for Point2D<T> {
 }
 
 ////////// divide point with scalar ////////////////////////////////////////////
-impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
+impl<T: Div<Output = T> + Copy> Div<T> for Point<T> {
     type Output = Self;
 
     fn div(self, rhs: T) -> Self {
@@ -108,7 +108,7 @@ impl<T: Div<Output = T> + Copy> Div<T> for Point2D<T> {
     }
 }
 
-impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> {
+impl<T: Div<Output = T> + Copy> DivAssign<T> for Point<T> {
     fn div_assign(&mut self, rhs: T) {
        *self = Self {
            x: self.x / rhs,
@@ -117,7 +117,7 @@ impl<T: Div<Output = T> + Copy> DivAssign<T> for Point2D<T> {
     }
 }
 
-impl<T: Neg<Output = T>> Neg for Point2D<T> {
+impl<T: Neg<Output = T>> Neg for Point<T> {
     type Output = Self;
 
     fn neg(self) -> Self {
@@ -128,34 +128,34 @@ impl<T: Neg<Output = T>> Neg for Point2D<T> {
     }
 }
 
-impl<T> From<(T, T)> for Point2D<T> {
+impl<T> From<(T, T)> for Point<T> {
     fn from(item: (T, T)) -> Self {
-        Point2D {
+        Point {
             x: item.0,
             y: item.1,
         }
     }
 }
 
-impl<T> From<Point2D<T>> for (T, T) {
-    fn from(item: Point2D<T>) -> Self {
+impl<T> From<Point<T>> for (T, T) {
+    fn from(item: Point<T>) -> Self {
         (item.x, item.y)
     }
 }
 
-impl From<Degrees> for Point2D<f64> {
+impl From<Degrees> for Point<f64> {
     fn from(item: Degrees) -> Self {
        let r = item.0.to_radians();
-        Point2D {
+        Point {
             x: r.cos(),
             y: r.sin(),
         }
     }
 }
 
-impl From<Radians> for Point2D<f64> {
+impl From<Radians> for Point<f64> {
     fn from(item: Radians) -> Self {
-        Point2D {
+        Point {
             x: item.0.cos(),
             y: item.0.sin(),
         }
@@ -182,28 +182,28 @@ impl Radians {
 }
 
 #[macro_export]
-macro_rules! rect {
-    ( $x:expr, $y:expr ) => {
-        Rect { x: $x, y: $y }
+macro_rules! dimen {
+    ( $w:expr, $h:expr ) => {
+        Dimension { width: $w, height: $h }
     };
 }
 
-#[derive(Default)]
-pub struct Rect<T> {
+#[derive(Debug, Default)]
+pub struct Dimension<T> {
     pub width: T,
     pub height: T,
 }
 
-impl<T: Mul<Output = T> + Copy> Rect<T> {
+impl<T: Mul<Output = T> + Copy> Dimension<T> {
     #[allow(dead_code)]
     pub fn area(&self) -> T {
         self.width * self.height
     }
 }
 
-impl<T> From<(T, T)> for Rect<T> {
+impl<T> From<(T, T)> for Dimension<T> {
     fn from(item: (T, T)) -> Self {
-        Rect {
+        Dimension {
             width: item.0,
             height: item.1,
         }
@@ -286,14 +286,14 @@ mod tests {
        assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0));
        assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0));
        assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI));
-       assert!((Point2D::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
-       assert!((Point2D::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
+       assert!((Point::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001);
+       assert!((Point::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001);
     }
 
     #[test]
-    fn area_for_rect_of_multipliable_type() {
-        let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait
+    fn area_for_dimension_of_multipliable_type() {
+        let r: Dimension<_> = (30, 20).into(); // the Into trait uses the From trait
         assert_eq!(r.area(), 30 * 20);
-        // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
+        // let a = Dimension::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String
     }
 }