Renamed operator impl macro for Point
[kaka/rust-sdl-test.git] / src / common / geometry.rs
index e46f23e..3fee2ad 100644 (file)
@@ -44,7 +44,7 @@ impl Point<f64> {
     }
 }
 
-macro_rules! point_op {
+macro_rules! impl_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 Point<T> {
             type Output = Self;
@@ -68,16 +68,16 @@ macro_rules! point_op {
     }
 }
 
-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);
-point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Dimension<T> => rhs.width, rhs.height);
-point_op!(/, Div(div), DivAssign(div_assign), rhs = Dimension<T> => rhs.width, rhs.height);
+impl_point_op!(+, Add(add), AddAssign(add_assign), rhs = Point<T> => rhs.x, rhs.y);
+impl_point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point<T> => rhs.x, rhs.y);
+impl_point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point<T> => rhs.x, rhs.y);
+impl_point_op!(/, Div(div), DivAssign(div_assign), rhs = Point<T> => rhs.x, rhs.y);
+impl_point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1);
+impl_point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1);
+impl_point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1);
+impl_point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1);
+impl_point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Dimension<T> => rhs.width, rhs.height);
+impl_point_op!(/, Div(div), DivAssign(div_assign), rhs = Dimension<T> => rhs.width, rhs.height);
 
 ////////// multiply point with scalar //////////////////////////////////////////
 impl<T: Mul<Output = T> + Copy> Mul<T> for Point<T> {
@@ -173,16 +173,21 @@ pub struct Radians(pub f64);
 
 impl Degrees {
     #[allow(dead_code)]
-    fn to_radians(&self) -> Radians {
+    pub fn to_radians(&self) -> Radians {
        Radians(self.0.to_radians())
     }
 }
 
 impl Radians {
     #[allow(dead_code)]
-    fn to_degrees(&self) -> Degrees {
+    pub fn to_degrees(&self) -> Degrees {
        Degrees(self.0.to_degrees())
     }
+
+    /// Returns the reflection of the incident when mirrored along this angle.
+    pub fn mirror(&self, incidence: Radians) -> Radians {
+       Radians((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU)
+    }
 }
 
 ////////// INTERSECTION ////////////////////////////////////////////////////////