transposition

This commit is contained in:
Jon Janzen
2021-03-29 19:38:00 -06:00
parent f106344504
commit 6c685aef15

View File

@@ -55,6 +55,15 @@ impl Matrix {
vec![0.0, 0.0, 0.0, 1.0],
])
}
pub fn transpose(&mut self) {
self.matrix = vec![
vec![self.matrix[0][0], self.matrix[1][0], self.matrix[2][0], self.matrix[3][0], ],
vec![self.matrix[0][1], self.matrix[1][1], self.matrix[2][1], self.matrix[3][1], ],
vec![self.matrix[0][2], self.matrix[1][2], self.matrix[2][2], self.matrix[3][2], ],
vec![self.matrix[0][3], self.matrix[1][3], self.matrix[2][3], self.matrix[3][3], ],
];
}
}
impl Index<usize> for Matrix {
@@ -313,4 +322,30 @@ mod tests {
assert_eq!(Matrix::identity() * t, expected);
}
#[test]
fn transposition() {
let mut m = Matrix::from_array([
[0.0, 9.0, 3.0, 0.0],
[9.0, 8.0, 0.0, 8.0],
[1.0, 8.0, 5.0, 3.0],
[0.0, 0.0, 5.0, 8.0],
]);
let expected = Matrix::from_array([
[0.0, 9.0, 1.0, 0.0],
[9.0, 8.0, 8.0, 0.0],
[3.0, 0.0, 5.0, 5.0],
[0.0, 8.0, 3.0, 8.0],
]);
m.transpose();
assert_eq!(m, expected);
}
#[test]
fn transpose_identity() {
let mut m = Matrix::identity();
m.transpose();
assert_eq!(m, Matrix::identity());
}
}