X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fgeometry.rs;h=f5e2b0ec2f5398b32291b07d5b72cda64625bfce;hb=15cda3333ba8d5600b1afa9426a112dea99d4941;hp=540db53bff9568f488024837ac44857636c5eb34;hpb=953b4c960649b82f4e186c2a9afee5367270f0fc;p=kaka%2Frust-sdl-test.git diff --git a/src/geometry.rs b/src/geometry.rs index 540db53..f5e2b0e 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -38,6 +38,16 @@ impl Point { 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 { + 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 From> for (T, T) { pub fn supercover_line_int(p1: Point, p2: Point) -> Vec> { 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));