diff --git a/rtchallenge/src/matrices.rs b/rtchallenge/src/matrices.rs index bb03631..6363d13 100644 --- a/rtchallenge/src/matrices.rs +++ b/rtchallenge/src/matrices.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::ops::{Index, IndexMut, Mul}; +use std::ops::{Index, IndexMut, Mul, Sub}; use crate::{tuples::Tuple, EPSILON}; @@ -161,7 +161,6 @@ impl PartialEq for Matrix3x3 { } } -#[derive(Copy, Clone, Default)] /// Matrix4x4 represents a 4x4 matrix in row-major form. So, element `m[i][j]` corresponds to mi,j /// where `i` is the row number and `j` is the column number. /// @@ -194,6 +193,7 @@ impl PartialEq for Matrix3x3 { /// let t = c * b * a; /// assert_eq!(t * p, Tuple::point(15., 0., 7.)); /// ``` +#[derive(Copy, Clone, Default)] pub struct Matrix4x4 { m: [[f32; 4]; 4], } @@ -764,7 +764,7 @@ impl fmt::Debug for Matrix4x4 { if f.alternate() { write!( f, - "{:?}\n {:?}\n {:?}\n {:?}", + "\n {:8.5?}\n {:8.5?}\n {:8.5?}\n {:8.5?}", self.m[0], self.m[1], self.m[2], self.m[3] ) } else { @@ -838,6 +838,21 @@ impl Mul for Matrix4x4 { } } +impl Sub for Matrix4x4 { + type Output = Matrix4x4; + + fn sub(self, m2: Matrix4x4) -> Matrix4x4 { + let m1 = self; + let mut r: Matrix4x4 = Default::default(); + for i in 0..4 { + for j in 0..4 { + r.m[i][j] = m1.m[i][j] - m2.m[i][j]; + } + } + r + } +} + impl PartialEq for Matrix4x4 { fn eq(&self, rhs: &Matrix4x4) -> bool { let l = self.m;