Add component-wise vec/vec multiplication and division methods
This commit is contained in:
parent
bd91c24de0
commit
aae354e3a6
5 changed files with 144 additions and 14 deletions
|
@ -49,8 +49,10 @@ fn test_vec2() {
|
|||
assert a.mul_t(f1) == Vec2::new( 1.5f, 3.0f);
|
||||
assert a.div_t(f2) == Vec2::new( 2.0f, 4.0f);
|
||||
|
||||
assert a.add_v(&b) == Vec2::new( 4f, 6f);
|
||||
assert a.sub_v(&b) == Vec2::new(-2f, -2f);
|
||||
assert a.add_v(&b) == Vec2::new( 4f, 6f);
|
||||
assert a.sub_v(&b) == Vec2::new( -2f, -2f);
|
||||
assert a.mul_v(&b) == Vec2::new( 3f, 8f);
|
||||
assert a.div_v(&b) == Vec2::new(1f/3f, 2f/4f);
|
||||
|
||||
mut_a.neg_self();
|
||||
assert mut_a == -a;
|
||||
|
@ -70,6 +72,14 @@ fn test_vec2() {
|
|||
|
||||
mut_a.sub_self_v(&b);
|
||||
assert mut_a == a.sub_v(&b);
|
||||
mut_a = a;
|
||||
|
||||
mut_a.mul_self_v(&b);
|
||||
assert mut_a == a.mul_v(&b);
|
||||
mut_a = a;
|
||||
|
||||
mut_a.div_self_v(&b);
|
||||
assert mut_a == a.div_v(&b);
|
||||
// mut_a = a;
|
||||
|
||||
// assert c.abs() == Vec2::new( 2.0f, 1.0f);
|
||||
|
@ -192,8 +202,10 @@ fn test_vec3() {
|
|||
assert a.mul_t(f1) == Vec3::new( 1.5f, 3.0f, 4.5f);
|
||||
assert a.div_t(f2) == Vec3::new( 2.0f, 4.0f, 6.0f);
|
||||
|
||||
assert a.add_v(&b) == Vec3::new( 5f, 7f, 9f);
|
||||
assert a.sub_v(&b) == Vec3::new(-3f, -3f, -3f);
|
||||
assert a.add_v(&b) == Vec3::new( 5f, 7f, 9f);
|
||||
assert a.sub_v(&b) == Vec3::new( -3f, -3f, -3f);
|
||||
assert a.mul_v(&b) == Vec3::new( 4f, 10f, 18f);
|
||||
assert a.div_v(&b) == Vec3::new(1f/4f, 2f/5f, 3f/6f);
|
||||
|
||||
mut_a.neg_self();
|
||||
assert mut_a == -a;
|
||||
|
@ -213,6 +225,14 @@ fn test_vec3() {
|
|||
|
||||
mut_a.sub_self_v(&b);
|
||||
assert mut_a == a.sub_v(&b);
|
||||
mut_a = a;
|
||||
|
||||
mut_a.mul_self_v(&b);
|
||||
assert mut_a == a.mul_v(&b);
|
||||
mut_a = a;
|
||||
|
||||
mut_a.div_self_v(&b);
|
||||
assert mut_a == a.div_v(&b);
|
||||
// mut_a = a;
|
||||
|
||||
// exact_eq
|
||||
|
@ -337,8 +357,10 @@ fn test_vec4() {
|
|||
assert a.mul_t(f1) == Vec4::new( 1.5f, 3.0f, 4.5f, 6.0f);
|
||||
assert a.div_t(f2) == Vec4::new( 2.0f, 4.0f, 6.0f, 8.0f);
|
||||
|
||||
assert a.add_v(&b) == Vec4::new( 6f, 8f, 10f, 12f);
|
||||
assert a.sub_v(&b) == Vec4::new(-4f, -4f, -4f, -4f);
|
||||
assert a.add_v(&b) == Vec4::new( 6f, 8f, 10f, 12f);
|
||||
assert a.sub_v(&b) == Vec4::new( -4f, -4f, -4f, -4f);
|
||||
assert a.mul_v(&b) == Vec4::new( 5f, 12f, 21f, 32f);
|
||||
assert a.div_v(&b) == Vec4::new(1f/5f, 2f/6f, 3f/7f, 4f/8f);
|
||||
|
||||
assert a.dot(&b) == 70f;
|
||||
|
||||
|
@ -360,6 +382,14 @@ fn test_vec4() {
|
|||
|
||||
mut_a.sub_self_v(&b);
|
||||
assert mut_a == a.sub_v(&b);
|
||||
mut_a = a;
|
||||
|
||||
mut_a.mul_self_v(&b);
|
||||
assert mut_a == a.mul_v(&b);
|
||||
mut_a = a;
|
||||
|
||||
mut_a.div_self_v(&b);
|
||||
assert mut_a == a.div_v(&b);
|
||||
// mut_a = a;
|
||||
|
||||
// assert c.abs() == Vec4::new( 2.0f, 1.0f, 1.0f, 2.0f);
|
||||
|
|
32
src/vec.rs
32
src/vec.rs
|
@ -113,19 +113,25 @@ pub trait NumericVector<T>: Vector<T> Neg<self> {
|
|||
pure fn div_t(&self, value: T) -> self;
|
||||
|
||||
/**
|
||||
* # Return value
|
||||
*
|
||||
* The sum of the vector and `other`
|
||||
* Component-wise vector addition
|
||||
*/
|
||||
pure fn add_v(&self, other: &self) -> self;
|
||||
|
||||
/**
|
||||
* # Return value
|
||||
*
|
||||
* The difference between the vector and `other`
|
||||
* Component-wise vector subtraction
|
||||
*/
|
||||
pure fn sub_v(&self, other: &self) -> self;
|
||||
|
||||
/**
|
||||
* Component-wise vector multiplication
|
||||
*/
|
||||
pure fn mul_v(&self, other: &self) -> self;
|
||||
|
||||
/**
|
||||
* Component-wise vector division
|
||||
*/
|
||||
pure fn div_v(&self, other: &self) -> self;
|
||||
|
||||
/**
|
||||
* # Return value
|
||||
*
|
||||
|
@ -155,14 +161,24 @@ pub trait MutableNumericVector<T>: MutableVector<&self/T>
|
|||
fn div_self_t(&mut self, value: T);
|
||||
|
||||
/**
|
||||
* Set to the sum of the vector and `other`
|
||||
* Set the vector to the component-wise vector sum
|
||||
*/
|
||||
fn add_self_v(&mut self, other: &self);
|
||||
|
||||
/**
|
||||
* Set to the difference between the vector and `other`
|
||||
* Set the vector to the component-wise vector difference
|
||||
*/
|
||||
fn sub_self_v(&mut self, other: &self);
|
||||
|
||||
/**
|
||||
* Set the vector to the component-wise vector product
|
||||
*/
|
||||
fn mul_self_v(&mut self, other: &self);
|
||||
|
||||
/**
|
||||
* Set the vector to the component-wise vector quotient
|
||||
*/
|
||||
fn div_self_v(&mut self, other: &self);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
24
src/vec2.rs
24
src/vec2.rs
|
@ -115,6 +115,18 @@ pub impl<T:Copy Number> Vec2<T>: NumericVector<T> {
|
|||
self[1] - other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
||||
Vec2::new(self[0] * other[0],
|
||||
self[1] * other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn div_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
||||
Vec2::new(self[0] / other[0],
|
||||
self[1] / other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn dot(&self, other: &Vec2<T>) -> T {
|
||||
self[0] * other[0] +
|
||||
|
@ -159,6 +171,18 @@ pub impl<T:Copy Number> Vec2<T>: MutableNumericVector<&self/T> {
|
|||
*self.index_mut(0) -= other[0];
|
||||
*self.index_mut(1) -= other[1];
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_self_v(&mut self, other: &Vec2<T>) {
|
||||
*self.index_mut(0) *= other[0];
|
||||
*self.index_mut(1) *= other[1];
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_self_v(&mut self, other: &Vec2<T>) {
|
||||
*self.index_mut(0) /= other[0];
|
||||
*self.index_mut(1) /= other[1];
|
||||
}
|
||||
}
|
||||
|
||||
pub impl<T:Copy Number> Vec2<T>: NumericVector2<T> {
|
||||
|
|
28
src/vec3.rs
28
src/vec3.rs
|
@ -124,6 +124,20 @@ pub impl<T:Copy Number> Vec3<T>: NumericVector<T> {
|
|||
self[2] - other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_v(&self, other: &Vec3<T>) -> Vec3<T>{
|
||||
Vec3::new(self[0] * other[0],
|
||||
self[1] * other[1],
|
||||
self[2] * other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn div_v(&self, other: &Vec3<T>) -> Vec3<T>{
|
||||
Vec3::new(self[0] / other[0],
|
||||
self[1] / other[1],
|
||||
self[2] / other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn dot(&self, other: &Vec3<T>) -> T {
|
||||
self[0] * other[0] +
|
||||
|
@ -174,6 +188,20 @@ pub impl<T:Copy Number> Vec3<T>: MutableNumericVector<&self/T> {
|
|||
*self.index_mut(1) -= other[1];
|
||||
*self.index_mut(2) -= other[2];
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_self_v(&mut self, other: &Vec3<T>) {
|
||||
*self.index_mut(0) *= other[0];
|
||||
*self.index_mut(1) *= other[1];
|
||||
*self.index_mut(2) *= other[2];
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_self_v(&mut self, other: &Vec3<T>) {
|
||||
*self.index_mut(0) /= other[0];
|
||||
*self.index_mut(1) /= other[1];
|
||||
*self.index_mut(2) /= other[2];
|
||||
}
|
||||
}
|
||||
|
||||
pub impl<T:Copy Number> Vec3<T>: NumericVector3<T> {
|
||||
|
|
32
src/vec4.rs
32
src/vec4.rs
|
@ -133,6 +133,22 @@ pub impl<T:Copy Number> Vec4<T>: NumericVector<T> {
|
|||
self[3] - other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn mul_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
||||
Vec4::new(self[0] * other[0],
|
||||
self[1] * other[1],
|
||||
self[2] * other[2],
|
||||
self[3] * other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn div_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
||||
Vec4::new(self[0] / other[0],
|
||||
self[1] / other[1],
|
||||
self[2] / other[2],
|
||||
self[3] / other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pure fn dot(&self, other: &Vec4<T>) -> T {
|
||||
self[0] * other[0] +
|
||||
|
@ -189,6 +205,22 @@ pub impl<T:Copy Number> Vec4<T>: MutableNumericVector<&self/T> {
|
|||
*self.index_mut(2) -= other[2];
|
||||
*self.index_mut(3) -= other[3];
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_self_v(&mut self, other: &Vec4<T>) {
|
||||
*self.index_mut(0) *= other[0];
|
||||
*self.index_mut(1) *= other[1];
|
||||
*self.index_mut(2) *= other[2];
|
||||
*self.index_mut(3) *= other[3];
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_self_v(&mut self, other: &Vec4<T>) {
|
||||
*self.index_mut(0) /= other[0];
|
||||
*self.index_mut(1) /= other[1];
|
||||
*self.index_mut(2) /= other[2];
|
||||
*self.index_mut(3) /= other[3];
|
||||
}
|
||||
}
|
||||
|
||||
pub impl<T:Copy Float> Vec4<T>: EuclideanVector<T> {
|
||||
|
|
Loading…
Reference in a new issue