Implement Mul for f64 * Vector3D

This commit is contained in:
Matthew Gordon 2019-10-24 21:00:34 -04:00
parent 423be7700d
commit 7bc7b3520a
1 changed files with 58 additions and 0 deletions

View File

@ -64,6 +64,14 @@ impl<T: Mul + Copy> Mul<T> for Vector3D<T> {
} }
} }
impl Mul<Vector3D<f64>> for f64 {
type Output = Vector3D<f64>;
fn mul(self, other: Vector3D<f64>) -> Vector3D<f64> {
Vector3D(self * other.0, self * other.1, self * other.2)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -664,4 +672,54 @@ mod tests {
assert!(c.1 == a.1 * -2); assert!(c.1 == a.1 * -2);
assert!(c.2 == a.2 * -2); assert!(c.2 == a.2 * -2);
} }
#[test]
fn test_vector3_multiply_zeroes_yields_zeroes_scalar_first() {
let a = Vector3D(0.0, 0.0, 0.0);
let b = 0.0;
let c = b * a;
assert!(c.0 == 0.0);
assert!(c.1 == 0.0);
assert!(c.2 == 0.0);
}
#[test]
fn test_vector3_multiply_zero_byvector2d_yields_zeroes() {
let a = Vector3D(1.0, 2.0, 3.0);
let b = 0.0;
let c = b * a;
assert!(c.0 == 0.0);
assert!(c.1 == 0.0);
assert!(c.2 == 0.0);
}
#[test]
fn test_vector3_multiplication_identity_scalar_first() {
let a = Vector3D(1.0, 2.0, 3.0);
let b = 1.0;
let c = b * a;
assert!(c.0 == a.0);
assert!(c.1 == a.1);
assert!(c.2 == a.2);
}
#[test]
fn test_vector3_multiply_positive_float_by_vector2d() {
let a = Vector3D(1.0, 2.0, 3.0);
let b = 2.0;
let c = b * a;
assert!(c.0 == a.0 * 2.0);
assert!(c.1 == a.1 * 2.0);
assert!(c.2 == a.2 * 2.0);
}
#[test]
fn test_vector3_multiply_negative_float_by_vector2d() {
let a = Vector3D(1.0, 2.0, 3.0);
let b = -2.0;
let c = b * a;
assert!(c.0 == a.0 * -2.0);
assert!(c.1 == a.1 * -2.0);
assert!(c.2 == a.2 * -2.0);
}
} }