Skip to content

Commit

Permalink
Add recip, angle and angle_between methods to Vector2F
Browse files Browse the repository at this point in the history
  • Loading branch information
s3bk authored and pcwalton committed Jun 25, 2020
1 parent 265e781 commit 7b244ca
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions geometry/src/vector.rs
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 7b244ca

Please sign in to comment.