Add cross_self method

This commit is contained in:
Brendan Zabarauskas 2012-12-05 01:34:41 +10:00
parent 9012589f5f
commit ea17c63200
2 changed files with 31 additions and 10 deletions

View file

@ -130,7 +130,7 @@ fn test_Vec3() {
mut_a.swap(1, 2);
assert mut_a[1] == a[2];
assert mut_a[2] == a[1];
let mut mut_a = a;
mut_a = a;
assert a.x == 1f;
assert a.y == 2f;
@ -141,6 +141,10 @@ fn test_Vec3() {
assert a.cross(&b) == Vec3::new(-3f, 6f, -3f);
mut_a.cross_self(&b);
assert mut_a == a.cross(&b);
mut_a = a;
assert -a == Vec3::new(-1f, -2f, -3f);
assert a.neg() == Vec3::new(-1f, -2f, -3f);

View file

@ -143,6 +143,16 @@ pub trait NumericVector3<T>: NumericVector<T> {
pure fn cross(&self, other: &self) -> self;
}
/**
* A mutable 3-dimensional vector with numeric components
*/
pub trait MutableNumericVector3<T>: MutableNumericVector<&self/T> {
/**
* Set to the cross product of the vector and `other`
*/
fn cross_self(&mut self, other: &self);
}
/**
* A 4-dimensional vector with numeric components
*/
@ -525,15 +535,6 @@ pub impl<T:Copy Number> Vec3<T>: Neg<Vec3<T>> {
}
}
pub impl<T:Copy Number> Vec3<T>: NumericVector3<T> {
#[inline(always)]
pure fn cross(&self, other: &Vec3<T>) -> Vec3<T> {
Vec3::new((self[1] * other[2]) - (self[2] * other[1]),
(self[2] * other[0]) - (self[0] * other[2]),
(self[0] * other[1]) - (self[1] * other[0]))
}
}
pub impl<T:Copy Number> Vec3<T>: MutableNumericVector<&self/T> {
#[inline(always)]
fn neg_self(&mut self) {
@ -571,6 +572,22 @@ pub impl<T:Copy Number> Vec3<T>: MutableNumericVector<&self/T> {
}
}
pub impl<T:Copy Number> Vec3<T>: NumericVector3<T> {
#[inline(always)]
pure fn cross(&self, other: &Vec3<T>) -> Vec3<T> {
Vec3::new((self[1] * other[2]) - (self[2] * other[1]),
(self[2] * other[0]) - (self[0] * other[2]),
(self[0] * other[1]) - (self[1] * other[0]))
}
}
pub impl<T:Copy Number> Vec3<T>: MutableNumericVector3<&self/T> {
#[inline(always)]
fn cross_self(&mut self, other: &Vec3<T>) {
*self = self.cross(other);
}
}
pub impl<T:Copy Number Exp> Vec3<T>: GeometricVector<T> {
#[inline(always)]
pure fn length2(&self) -> T {