diff options
Diffstat (limited to 'src/geometry.rs')
-rw-r--r-- | src/geometry.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/geometry.rs b/src/geometry.rs index c49fe2a..f84a973 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -14,9 +14,24 @@ impl Vec2d { ((other.x-self.x).powi(2) + (other.y-self.y).powi(2)) } + pub fn magnitude(&self) -> f64 { + self.magnitude_squared().sqrt() + } + pub fn magnitude_squared(&self) -> f64 { + self.x.powi(2) + self.y.powi(2) + } + pub fn angle(&self) -> f64 { self.y.atan2(self.x) } + + pub fn unit(&self) -> Vec2d { + let mag = self.magnitude(); + Vec2d { + x: self.x / mag, + y: self.y / mag + } + } } impl Add for Vec2d { @@ -51,3 +66,14 @@ impl Neg for Vec2d { } } } + +impl Mul<f64> for Vec2d { + type Output = Vec2d; + + fn mul(self, rhs: f64) -> Self { + Vec2d { + x: self.x * rhs, + y: self.y * rhs + } + } +} |