diff --git a/raytracing/src/matrix.cpp b/raytracing/src/matrix.cpp index ec58753..81073f3 100644 --- a/raytracing/src/matrix.cpp +++ b/raytracing/src/matrix.cpp @@ -190,6 +190,13 @@ bool Matrix::transpose(void) /* ------------------------------------------------------------------------- */ +double Matrix::determinant(void) +{ + return (m_data[0][0] * m_data[1][1]) - (m_data[0][1] * m_data[1][0]); +} + +/* ------------------------------------------------------------------------- */ + Matrix Matrix::identity(void) { Matrix the_identity = { diff --git a/raytracing/src/matrix.h b/raytracing/src/matrix.h index af19ffb..746d893 100644 --- a/raytracing/src/matrix.h +++ b/raytracing/src/matrix.h @@ -58,6 +58,7 @@ namespace Raytracer const Tuple operator*(const Tuple &a_tuple) const; bool transpose(void); + double determinant(void); static Matrix identity(void); diff --git a/tests/03_matrix.cpp b/tests/03_matrix.cpp index d24a042..31c213e 100644 --- a/tests/03_matrix.cpp +++ b/tests/03_matrix.cpp @@ -227,3 +227,15 @@ TEST_CASE("[Matrix] Transposing the identity matrix", "[Matrix]") REQUIRE(a == Matrix::identity()); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[Matrix] Calculating the determinant of a 2x2 matrix", "[Matrix]") +{ + Matrix a = { + { 1, 5}, + {-3, 2} + }; + + REQUIRE(a.determinant() == 17); +}