Auto merge of #368 - pcwalton:vector-additions, r=pcwalton

Add `recip`, `angle` and `angle_between` methods to `Vector2F`
This commit is contained in:
bors-servo 2020-06-24 21:35:04 -04:00 committed by GitHub
commit 72a9deed9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -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()