From 7635d0127660ebd6b1aebe794d2a10c68d8da2b4 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 3 Sep 2013 22:12:54 +1000 Subject: [PATCH] Add normalize_self, normalize_self_to and lerp_self methods --- src/cgmath/vector.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index 2dbd804..06ff90d 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -199,11 +199,32 @@ pub trait EuclideanVector } /// Returns the result of linarly interpolating the length of the vector - /// to the length of `other` by the specified amount. + /// towards the length of `other` by the specified amount. #[inline] fn lerp(&self, other: &Self, amount: S) -> Self { self.add_v(&other.sub_v(self).mul_s(amount)) } + + /// Normalises the vector to a length of `1`. + #[inline] + fn normalize_self(&mut self) { + let rlen = self.length().recip(); + self.mul_self_s(rlen); + } + + /// Normalizes the vector to `length`. + #[inline] + fn normalize_self_to(&mut self, length: S) { + let n = length / self.length(); + self.mul_self_s(n); + } + + /// Linearly interpolates the length of the vector towards the length of + /// `other` by the specified amount. + fn lerp_self(&mut self, other: &Self, amount: S) { + let v = other.sub_v(self).mul_s(amount); + self.add_self_v(&v); + } } impl EuclideanVector for Vec2 {