add normalize_to and normalize_self_to methods
This commit is contained in:
parent
7f14c6784b
commit
965bade82c
1 changed files with 46 additions and 0 deletions
46
src/vec.rs
46
src/vec.rs
|
@ -246,6 +246,11 @@ pub trait EuclideanVector<T>: NumericVector<T> {
|
|||
*/
|
||||
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<T>: 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<T:Copy Number Exp> Vec2<T>: EuclideanVector<T> {
|
|||
return self.mul_t(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn normalize_to(&self, length: T) -> Vec2<T> {
|
||||
let mut n: T = length / self.length();
|
||||
return self.mul_t(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
|
||||
self.add_v(&other.sub_v(self).mul_t(amount))
|
||||
|
@ -467,6 +483,12 @@ pub impl<T:Copy Number Exp> Vec2<T>: 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<T>, amount: &T) {
|
||||
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
|
||||
}
|
||||
|
@ -701,6 +723,12 @@ pub impl<T:Copy Number Exp> Vec3<T>: EuclideanVector<T> {
|
|||
return self.mul_t(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn normalize_to(&self, length: T) -> Vec3<T> {
|
||||
let mut n: T = length / self.length();
|
||||
return self.mul_t(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn lerp(&self, other: &Vec3<T>, amount: T) -> Vec3<T> {
|
||||
self.add_v(&other.sub_v(self).mul_t(amount))
|
||||
|
@ -715,6 +743,12 @@ pub impl<T:Copy Number Exp> Vec3<T>: 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<T>, amount: &T) {
|
||||
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
|
||||
}
|
||||
|
@ -949,6 +983,12 @@ pub impl<T:Copy Number Exp> Vec4<T>: EuclideanVector<T> {
|
|||
return self.mul_t(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn normalize_to(&self, length: T) -> Vec4<T> {
|
||||
let mut n: T = length / self.length();
|
||||
return self.mul_t(n);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn lerp(&self, other: &Vec4<T>, amount: T) -> Vec4<T> {
|
||||
self.add_v(&other.sub_v(self).mul_t(amount))
|
||||
|
@ -963,6 +1003,12 @@ pub impl<T:Copy Number Exp> Vec4<T>: 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<T>, amount: &T) {
|
||||
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue