[FEAT] Tuple: add operator + and -

This commit is contained in:
2024-01-29 23:17:31 +01:00
parent b212ca2bac
commit 3595f21d70
3 changed files with 71 additions and 5 deletions

View File

@@ -44,9 +44,9 @@ Tuple::Tuple(float a_x, float a_y, float a_z, float a_w) : m_x(a_x), m_y(a_y), m
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
bool Tuple::operator==(const Tuple &a_copy) const bool Tuple::operator==(const Tuple &an_other) const
{ {
if (float_equal(m_x, a_copy.m_x) && float_equal(m_y, a_copy.m_y) && float_equal(m_z, a_copy.m_z) && float_equal(m_w, a_copy.m_w)) if (float_equal(m_x, an_other.m_x) && float_equal(m_y, an_other.m_y) && float_equal(m_z, an_other.m_z) && float_equal(m_w, an_other.m_w))
{ {
return true; return true;
} }
@@ -56,6 +56,30 @@ bool Tuple::operator==(const Tuple &a_copy) const
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Tuple &Tuple::operator+(const Tuple &an_other)
{
m_x += an_other.m_x;
m_y += an_other.m_y;
m_z += an_other.m_z;
m_w += an_other.m_w;
return *this;
}
/* ------------------------------------------------------------------------- */
Tuple &Tuple::operator-(const Tuple &an_other)
{
m_x -= an_other.m_x;
m_y -= an_other.m_y;
m_z -= an_other.m_z;
m_w -= an_other.m_w;
return *this;
}
/* ------------------------------------------------------------------------- */
Tuple Tuple::Point(float an_x, float an_y, float an_z) Tuple Tuple::Point(float an_x, float an_y, float an_z)
{ {
return Tuple(an_x, an_y, an_z, kRaytracerTuplePoint); return Tuple(an_x, an_y, an_z, kRaytracerTuplePoint);

View File

@@ -40,7 +40,9 @@ namespace Raytracer
public: public:
Tuple(float an_x, float an_y, float an_z, float an_w); Tuple(float an_x, float an_y, float an_z, float an_w);
bool operator==(const Tuple &) const; bool operator==(const Tuple &an_other) const;
Tuple &operator+(const Tuple &an_other);
Tuple &operator-(const Tuple &an_other);
static Tuple Point(float an_x, float an_y, float an_z); static Tuple Point(float an_x, float an_y, float an_z);
static Tuple Vector(float an_x, float an_y, float an_z); static Tuple Vector(float an_x, float an_y, float an_z);

View File

@@ -69,7 +69,7 @@ TEST_CASE("Point() creates tuples with w=1", "[Tuple][Point]")
{ {
Tuple p = Tuple::Point(4, -4, 3); Tuple p = Tuple::Point(4, -4, 3);
REQUIRE(p == Tuple(4, -4, 3, kRaytracerTuplePoint)); REQUIRE(p == Tuple(4, -4, 3, 1));
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@@ -78,5 +78,45 @@ TEST_CASE("Vector() creates tuples with w=0", "[Tuple][Vector]")
{ {
Tuple v = Tuple::Vector(4, -4, 3); Tuple v = Tuple::Vector(4, -4, 3);
REQUIRE(v == Tuple(4, -4, 3, kRaytracerTupleVector)); REQUIRE(v == Tuple(4, -4, 3, 0));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Adding two tuples", "[Tuple][Operations]")
{
Tuple a1(3, -2, 5, 1);
Tuple a2(-2, 3, 1, 0);
REQUIRE((a1 + a2) == Tuple(1, 1, 6, 1));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Subtracting two points", "[Tuple][Operations]")
{
Tuple p1 = Tuple::Point(3, 2, 1);
Tuple p2 = Tuple::Point(5, 6, 7);
REQUIRE((p1 - p2) == Tuple::Vector(-2, -4, -6));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Subtracting a vector from a point", "[Tuple][Operations]")
{
Tuple p = Tuple::Point(3, 2, 1);
Tuple v= Tuple::Vector(5, 6, 7);
REQUIRE((p - v) == Tuple::Point(-2, -4, -6));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Subtracting two vectors", "[Tuple][Operations]")
{
Tuple v1 = Tuple::Vector(3, 2, 1);
Tuple v2 = Tuple::Vector(5, 6, 7);
REQUIRE((v1 - v2) == Tuple::Vector(-2, -4, -6));
} }