diff --git a/rtchallenge/src/matrices.rs b/rtchallenge/src/matrices.rs index 3d3c35f..2d6d96e 100644 --- a/rtchallenge/src/matrices.rs +++ b/rtchallenge/src/matrices.rs @@ -8,6 +8,38 @@ pub struct Matrix4x4 { m: [[f32; 4]; 4], } +pub struct Matrix2x2 { + m: [[f32; 2]; 2], +} +impl Matrix2x2 { + /// Create a `Matrix2x2` with each of the given rows. + pub fn new(r0: [f32; 2], r1: [f32; 2]) -> Matrix2x2 { + Matrix2x2 { m: [r0, r1] } + } +} +impl Index<(usize, usize)> for Matrix2x2 { + type Output = f32; + fn index(&self, (row, col): (usize, usize)) -> &Self::Output { + &self.m[row][col] + } +} + +pub struct Matrix3x3 { + m: [[f32; 3]; 3], +} +impl Matrix3x3 { + /// Create a `Matrix3x2` with each of the given rows. + pub fn new(r0: [f32; 3], r1: [f32; 3], r2: [f32; 3]) -> Matrix3x3 { + Matrix3x3 { m: [r0, r1, r2] } + } +} +impl Index<(usize, usize)> for Matrix3x3 { + type Output = f32; + fn index(&self, (row, col): (usize, usize)) -> &Self::Output { + &self.m[row][col] + } +} + impl From<[f32; 16]> for Matrix4x4 { fn from(t: [f32; 16]) -> Self { Matrix4x4 { @@ -241,6 +273,23 @@ impl Index<(usize, usize)> for Matrix4x4 { mod tests { use super::*; + #[test] + fn construct2x2() { + let m = Matrix2x2::new([-3., 5.], [1., -2.]); + + assert_eq!(m[(0, 0)], -3.); + assert_eq!(m[(0, 1)], 5.); + assert_eq!(m[(1, 0)], 1.); + assert_eq!(m[(1, 1)], -2.); + } + #[test] + fn construct3x3() { + let m = Matrix3x3::new([-3., 5., 0.], [1., -2., -7.], [0., 1., 1.]); + + assert_eq!(m[(0, 0)], -3.); + assert_eq!(m[(1, 1)], -2.); + assert_eq!(m[(2, 2)], 1.); + } #[test] fn construct4x4() { let m = Matrix4x4::new(