From 7b244cad68154e31792a7692d4f21c5177f56fc3 Mon Sep 17 00:00:00 2001 From: Sebastian K Date: Wed, 24 Jun 2020 17:56:46 -0700 Subject: [PATCH] Add `recip`, `angle` and `angle_between` methods to `Vector2F` --- geometry/src/vector.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/geometry/src/vector.rs b/geometry/src/vector.rs index 444fce50..fa864f5d 100644 --- a/geometry/src/vector.rs +++ b/geometry/src/vector.rs @@ -137,6 +137,18 @@ impl Vector2F { Vector2F(self.0.abs()) } + /// Returns the reciprocal of the vector, `(1.0 / x, 1.0 / y)`. + #[inline] + pub fn recip(self) -> Vector2F { + Vector2F::splat(1.0) / self + } + + /// Returns the counterclockwise angle of the vector from the +x axis. + #[inline] + pub fn angle(self) -> f32 { + self.y().atan2(self.x()) + } + /// Returns the coefficient when the given vector `a` is projected onto this one. /// /// That is, if this vector is `v` and this function returns `c`, then `proj_v a = cv`. In @@ -146,6 +158,12 @@ impl Vector2F { a.dot(self) / self.square_length() } + /// Returns the angle between the two vectors. + #[inline] + pub fn angle_between(self, a: Vector2F) -> f32 { + self.projection_coefficient(a).acos() + } + #[inline] pub fn is_zero(self) -> bool { self == Vector2F::zero()