Add distance methods

This commit is contained in:
Brendan Zabarauskas 2012-11-14 11:09:35 +10:00
parent 07df95313e
commit 26bab58b0d

View file

@ -48,6 +48,8 @@ pub trait NumericVector<T>: Vector<T>, Neg<self>{
pub trait GeometricVector<T>: NumericVector<T> {
pure fn length2() -> T;
pure fn length() -> T;
pure fn distance2(other: &self) -> T;
pure fn distance(other: &self) -> T;
pure fn normalize() -> self;
pure fn lerp(other: &self, amount: T) -> self;
}
@ -179,6 +181,18 @@ pub impl<T:Copy Num NumCast Exp> Vec2<T>: GeometricVector<T> {
self.length2().sqrt()
}
// TODO: tests
#[inline(always)]
pure fn distance2(other: &Vec2<T>) -> T {
other.sub_v(&self).length2()
}
// TODO: tests
#[inline(always)]
pure fn distance(other: &Vec2<T>) -> T {
other.distance2(&self).sqrt()
}
#[inline(always)]
pure fn normalize() -> Vec2<T> {
let mut n: T = cast(1);
@ -361,6 +375,18 @@ pub impl<T:Copy Num NumCast Exp> Vec3<T>: GeometricVector<T> {
self.length2().sqrt()
}
// TODO: tests
#[inline(always)]
pure fn distance2(other: &Vec3<T>) -> T {
other.sub_v(&self).length2()
}
// TODO: tests
#[inline(always)]
pure fn distance(other: &Vec3<T>) -> T {
other.distance2(&self).sqrt()
}
#[inline(always)]
pure fn normalize() -> Vec3<T> {
let mut n: T = cast(1);
@ -549,6 +575,18 @@ pub impl<T:Copy Num NumCast Exp> Vec4<T>: GeometricVector<T> {
self.length2().sqrt()
}
// TODO: tests
#[inline(always)]
pure fn distance2(other: &Vec4<T>) -> T {
other.sub_v(&self).length2()
}
// TODO: tests
#[inline(always)]
pure fn distance(other: &Vec4<T>) -> T {
other.distance2(&self).sqrt()
}
#[inline(always)]
pure fn normalize() -> Vec4<T> {
let mut n: T = cast(1);