Skip to content

Commit 72a9dee

Browse files
authored
Auto merge of #368 - pcwalton:vector-additions, r=pcwalton
Add `recip`, `angle` and `angle_between` methods to `Vector2F`
2 parents 495708d + 7b244ca commit 72a9dee

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

geometry/src/vector.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ impl Vector2F {
137137
Vector2F(self.0.abs())
138138
}
139139

140+
/// Returns the reciprocal of the vector, `(1.0 / x, 1.0 / y)`.
141+
#[inline]
142+
pub fn recip(self) -> Vector2F {
143+
Vector2F::splat(1.0) / self
144+
}
145+
146+
/// Returns the counterclockwise angle of the vector from the +x axis.
147+
#[inline]
148+
pub fn angle(self) -> f32 {
149+
self.y().atan2(self.x())
150+
}
151+
140152
/// Returns the coefficient when the given vector `a` is projected onto this one.
141153
///
142154
/// That is, if this vector is `v` and this function returns `c`, then `proj_v a = cv`. In
@@ -146,6 +158,12 @@ impl Vector2F {
146158
a.dot(self) / self.square_length()
147159
}
148160

161+
/// Returns the angle between the two vectors.
162+
#[inline]
163+
pub fn angle_between(self, a: Vector2F) -> f32 {
164+
self.projection_coefficient(a).acos()
165+
}
166+
149167
#[inline]
150168
pub fn is_zero(self) -> bool {
151169
self == Vector2F::zero()

0 commit comments

Comments
 (0)