Calculations from the world's 2D to the wall's 1D
[kaka/rust-sdl-test.git] / src / geometry.rs
index 7ddc3ec..f5e2b0e 100644 (file)
@@ -42,6 +42,12 @@ impl Point<f64> {
     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 {
@@ -225,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 {