diff options
Diffstat (limited to 'src/geometry.rs')
-rw-r--r-- | src/geometry.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/geometry.rs b/src/geometry.rs index e9d534a..be903d7 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -1,3 +1,5 @@ +use std::ops::*; + #[derive(Debug, Clone, Copy, PartialEq)] pub struct Vec2d { pub x: f64, @@ -8,4 +10,40 @@ impl Vec2d { pub fn distance(&self, other: Vec2d) -> f64 { ((other.x-self.x).powi(2) + (other.y-self.y).powi(2)).sqrt() } + pub fn angle(&self) -> f64 { + self.y.atan2(self.x) + } +} + +impl Add for Vec2d { + type Output = Vec2d; + + fn add(self, other: Self) -> Self { + Vec2d { + x: self.x + other.x, + y: self.y + other.y + } + } +} + +impl Sub for Vec2d { + type Output = Vec2d; + + fn sub(self, other: Self) -> Self { + Vec2d { + x: self.x - other.x, + y: self.y - other.y + } + } +} + +impl Neg for Vec2d { + type Output = Vec2d; + + fn neg(self) -> Self { + Vec2d { + x: -self.x, + y: -self.y + } + } } |