[FEAT] Add normalize

This commit is contained in:
NADAL Jean-Baptiste
2024-01-30 15:26:14 +01:00
parent 6bbc2b7d96
commit 65e26baf5f
3 changed files with 101 additions and 46 deletions

View File

@@ -39,10 +39,10 @@ TEST_CASE("a tuple with w=1.0 is a point", "[Tuple]")
{
Tuple a(4.3, -4.2, 3.1, 1.0);
REQUIRE(a.x() == 4.3f);
REQUIRE(a.y() == -4.2f);
REQUIRE(a.z() == 3.1f);
REQUIRE(a.w() == 1.0f);
REQUIRE(a.x() == 4.3);
REQUIRE(a.y() == -4.2);
REQUIRE(a.z() == 3.1);
REQUIRE(a.w() == 1.0);
REQUIRE(a.is_point() == true);
REQUIRE(a.is_vector() == false);
@@ -54,10 +54,10 @@ TEST_CASE("a tuple with w=0 is a vector", "[Tuple]")
{
Tuple a(4.3, -4.2, 3.1, 0.0);
REQUIRE(a.x() == 4.3f);
REQUIRE(a.y() == -4.2f);
REQUIRE(a.z() == 3.1f);
REQUIRE(a.w() == 0.0f);
REQUIRE(a.x() == 4.3);
REQUIRE(a.y() == -4.2);
REQUIRE(a.z() == 3.1);
REQUIRE(a.w() == 0.0);
REQUIRE(a.is_point() == false);
}
@@ -105,7 +105,7 @@ TEST_CASE("Subtracting two points", "[Tuple][Operations]")
TEST_CASE("Subtracting a vector from a point", "[Tuple][Operations]")
{
Tuple p = Tuple::Point(3, 2, 1);
Tuple v= Tuple::Vector(5, 6, 7);
Tuple v = Tuple::Vector(5, 6, 7);
REQUIRE((p - v) == Tuple::Point(-2, -4, -6));
}
@@ -190,7 +190,7 @@ TEST_CASE("Computing the magnitude of vector(1,2,3)", "[Tuple][Magnitude]")
{
Tuple v = Tuple::Vector(1, 2, 3);
REQUIRE(v.magnitude() == sqrtf(14));
REQUIRE(v.magnitude() == sqrt(14));
}
/* ------------------------------------------------------------------------- */
@@ -199,5 +199,35 @@ TEST_CASE("Computing the magnitude of vector(-1,-2,-3)", "[Tuple][Magnitude]")
{
Tuple v = Tuple::Vector(-1, -2, -3);
REQUIRE(v.magnitude() == sqrtf(14));
REQUIRE(v.magnitude() == sqrt(14));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Normalize vector(4,0,0) gives (1,0,0)", "[Tuple][Normalize]")
{
Tuple v = Tuple::Vector(4, 0, 0);
REQUIRE(v.normalize() == Tuple::Vector(1, 0, 0));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Normalize vector(1,2,3)", "[Tuple][Normalize]")
{
Tuple v = Tuple::Vector(1, 2, 3);
REQUIRE(v.normalize() == Tuple::Vector(1 / sqrtf(14), 2 / sqrtf(14), 3 / sqrtf(14)));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("The magnitude of a normalized vector", "[Tuple][Normalize]")
{
Tuple norm;
Tuple v = Tuple::Vector(1, 2, 3);
norm = v.normalize();
REQUIRE(norm.magnitude() == 1);
}