diff --git a/raytracing/src/matrix.cpp b/raytracing/src/matrix.cpp index 755d3e3..ec58753 100644 --- a/raytracing/src/matrix.cpp +++ b/raytracing/src/matrix.cpp @@ -173,6 +173,23 @@ const Tuple Matrix::operator*(const Tuple &a_tuple) const /* ------------------------------------------------------------------------- */ +bool Matrix::transpose(void) +{ + std::vector> the_copy = m_data; + + for (int the_row = 0; the_row < m_rows; the_row++) + { + for (int the_col = 0; the_col < m_cols; the_col++) + { + m_data[the_col][the_row] = the_copy[the_row][the_col]; + } + } + + return true; +} + +/* ------------------------------------------------------------------------- */ + Matrix Matrix::identity(void) { Matrix the_identity = { diff --git a/raytracing/src/matrix.h b/raytracing/src/matrix.h index c6fc741..af19ffb 100644 --- a/raytracing/src/matrix.h +++ b/raytracing/src/matrix.h @@ -57,6 +57,8 @@ namespace Raytracer const Matrix operator*(const Matrix &a_matrix) const; const Tuple operator*(const Tuple &a_tuple) const; + bool transpose(void); + static Matrix identity(void); private: diff --git a/tests/03_matrix.cpp b/tests/03_matrix.cpp index 0ca4959..31e375c 100644 --- a/tests/03_matrix.cpp +++ b/tests/03_matrix.cpp @@ -194,3 +194,25 @@ TEST_CASE("[Matrix] Multiplying the identity matrix by a tuple", "[Matrix]") REQUIRE((Matrix::identity() * a) == a); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[Matrix] Transposing a matrix", "[Matrix]") +{ + Matrix a = { + {0, 9, 3, 0}, + {9, 8, 0, 8}, + {1, 8, 5, 3}, + {0, 0, 5, 8} + }; + Matrix transposed = { + {0, 9, 1, 0}, + {9, 8, 8, 0}, + {3, 0, 5, 5}, + {0, 8, 3, 8} + }; + + a.transpose(); + + REQUIRE(a == transposed); +}