Calculations from the world's 2D to the wall's 1D
[kaka/rust-sdl-test.git] / src / geometry.rs
index 540db53..f5e2b0e 100644 (file)
@@ -38,6 +38,16 @@ impl Point<f64> {
            y: self.y as i32,
        }
     }
+
+    pub fn cross_product(&self, p: Self) -> f64 {
+        return self.x * p.y - self.y * p.x;
+    }
+
+    /// Returns the perpendicular projection of this vector on a line with the specified angle.
+    pub fn project_onto(&self, angle: Angle) -> Point<f64> {
+       let dot_product = self.length() * (self.to_angle() - angle).to_radians().cos();
+       Point::from(angle) * dot_product
+    }
 }
 
 macro_rules! impl_point_op {
@@ -221,6 +231,10 @@ impl Angle {
     pub fn mirror(&self, incidence: Angle) -> Angle {
        Angle((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU)
     }
+
+    pub fn reverse(&self) -> Angle {
+       Angle((self.0 + std::f64::consts::PI) % std::f64::consts::TAU)
+    }
 }
 
 impl PartialEq for Angle {
@@ -330,10 +344,7 @@ impl<T> From<Dimension<T>> for (T, T) {
 pub fn supercover_line_int(p1: Point<isize>, p2: Point<isize>) -> Vec<Point<isize>> {
     let d = p2 - p1;
     let n = point!(d.x.abs(), d.y.abs());
-    let step = point!(
-       if d.x > 0 { 1 } else { -1 },
-       if d.y > 0 { 1 } else { -1 }
-    );
+    let step = point!(d.x.signum(), d.y.signum());
 
     let mut p = p1;
     let mut points = vec!(point!(p.x as isize, p.y as isize));