[FEAT] Matrix add transpose

This commit is contained in:
NADAL Jean-Baptiste
2024-02-02 16:15:28 +01:00
parent 5866f77915
commit 9bf4dd4f7e
3 changed files with 41 additions and 0 deletions

View File

@@ -173,6 +173,23 @@ const Tuple Matrix::operator*(const Tuple &a_tuple) const
/* ------------------------------------------------------------------------- */
bool Matrix::transpose(void)
{
std::vector<std::vector<double>> 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 = {

View File

@@ -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:

View File

@@ -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);
}