[FEAT] Add scaling

This commit is contained in:
NADAL Jean-Baptiste
2024-02-05 17:32:22 +01:00
parent b531e0b714
commit dbac8a37c4
3 changed files with 56 additions and 0 deletions

View File

@@ -326,6 +326,20 @@ Matrix Matrix::translation(double an_x, double an_y, double an_z)
/* ------------------------------------------------------------------------- */
Matrix Matrix::scaling(double an_x, double an_y, double an_z)
{
Matrix the_scaling = {
{an_x, 0, 0, 0},
{ 0, an_y, 0, 0},
{ 0, 0, an_z, 0},
{ 0, 0, 0, 1}
};
return the_scaling;
}
/* ------------------------------------------------------------------------- */
bool Matrix::validate_dimensions(const std::initializer_list<std::initializer_list<double>> &a_values) const
{
for (const auto &the_row : a_values)

View File

@@ -72,6 +72,7 @@ namespace Raytracer
static Matrix identity(void);
static Matrix translation(double an_x, double an_y, double an_z);
static Matrix scaling(double an_x, double an_y, double an_z);
private:
bool validate_dimensions(const std::initializer_list<std::initializer_list<double>> &a_values) const;

View File

@@ -61,3 +61,44 @@ TEST_CASE("[04][TRANSFORMATION] Translation does not affect vectors", "[Matrix]"
REQUIRE(transform * v == v);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[04][TRANSFORMATION] A scaling matrix applied to a point", "[Matrix]")
{
Matrix transform = Matrix::scaling(2, 3, 4);
Tuple p = Tuple::Point(-4, 6, 8);
REQUIRE(transform * p == Tuple::Point(-8, 18, 32));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[04][TRANSFORMATION] A scaling matrix applied to a vector", "[Matrix]")
{
Matrix transform = Matrix::scaling(2, 3, 4);
Tuple v = Tuple::Vector(-4, 6, 8);
REQUIRE(transform * v == Tuple::Vector(-8, 18, 32));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[04][TRANSFORMATION] Multiplying by the inverse of a scaling matrix", "[Matrix]")
{
Matrix transform = Matrix::scaling(2, 3, 4);
Matrix inv = transform.inverse();
Tuple v = Tuple::Vector(-4, 6, 8);
REQUIRE(inv * v == Tuple::Vector(-2, 2, 2));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[04][TRANSFORMATION] Reflection is scaling by a negative value", "[Matrix]")
{
Matrix transform = Matrix::scaling(-1, 1, 1);
Tuple p = Tuple::Point(2, 3, 4);
REQUIRE(transform * p == Tuple::Point(-2, 3, 4));
}