From 965bade82c6078a6d55e06ff7d37e7fe6711f65a Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 13 Dec 2012 19:21:50 +1000 Subject: [PATCH] add normalize_to and normalize_self_to methods --- src/vec.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/vec.rs b/src/vec.rs index 42eb77d..e929ef0 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -246,6 +246,11 @@ pub trait EuclideanVector: NumericVector { */ pure fn normalize(&self) -> self; + /** + * Set the length of the vector whilst preserving the direction + */ + pure fn normalize_to(&self, length: T) -> self; + /** * Linearly intoperlate between the vector and `other` * @@ -270,6 +275,11 @@ pub trait MutableEuclideanVector: MutableNumericVector<&self/T> */ fn normalize_self(&mut self); + /** + * Set the vector to a specified length whilst preserving the direction + */ + fn normalize_self_to(&mut self, length: T); + /** * Linearly intoperlate the vector towards `other` */ @@ -453,6 +463,12 @@ pub impl Vec2: EuclideanVector { return self.mul_t(n); } + #[inline(always)] + pure fn normalize_to(&self, length: T) -> Vec2 { + let mut n: T = length / self.length(); + return self.mul_t(n); + } + #[inline(always)] pure fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { self.add_v(&other.sub_v(self).mul_t(amount)) @@ -467,6 +483,12 @@ pub impl Vec2: MutableEuclideanVector<&self/T> { self.mul_self_t(&n); } + #[inline(always)] + fn normalize_self_to(&mut self, length: &T) { + let mut n: T = length / self.length(); + self.mul_self_t(&n); + } + fn lerp_self(&mut self, other: &Vec2, amount: &T) { self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); } @@ -701,6 +723,12 @@ pub impl Vec3: EuclideanVector { return self.mul_t(n); } + #[inline(always)] + pure fn normalize_to(&self, length: T) -> Vec3 { + let mut n: T = length / self.length(); + return self.mul_t(n); + } + #[inline(always)] pure fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { self.add_v(&other.sub_v(self).mul_t(amount)) @@ -715,6 +743,12 @@ pub impl Vec3: MutableEuclideanVector<&self/T> { self.mul_self_t(&n); } + #[inline(always)] + fn normalize_self_to(&mut self, length: &T) { + let mut n: T = length / self.length(); + self.mul_self_t(&n); + } + fn lerp_self(&mut self, other: &Vec3, amount: &T) { self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); } @@ -949,6 +983,12 @@ pub impl Vec4: EuclideanVector { return self.mul_t(n); } + #[inline(always)] + pure fn normalize_to(&self, length: T) -> Vec4 { + let mut n: T = length / self.length(); + return self.mul_t(n); + } + #[inline(always)] pure fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { self.add_v(&other.sub_v(self).mul_t(amount)) @@ -963,6 +1003,12 @@ pub impl Vec4: MutableEuclideanVector<&self/T> { self.mul_self_t(&n); } + #[inline(always)] + fn normalize_self_to(&mut self, length: &T) { + let mut n: T = length / self.length(); + self.mul_self_t(&n); + } + fn lerp_self(&mut self, other: &Vec4, amount: &T) { self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); }