From 3595f21d70b77cb96f4cf97663d9ea7b9aec28d4 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Mon, 29 Jan 2024 23:17:31 +0100 Subject: [PATCH] [FEAT] Tuple: add operator + and - --- src/tuple.cpp | 28 +++++++++++++++++++++-- src/tuple.h | 4 +++- tests/chapitre01_tuples.cpp | 44 +++++++++++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/tuple.cpp b/src/tuple.cpp index 3400a76..769c3ea 100644 --- a/src/tuple.cpp +++ b/src/tuple.cpp @@ -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; } @@ -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) { return Tuple(an_x, an_y, an_z, kRaytracerTuplePoint); diff --git a/src/tuple.h b/src/tuple.h index 451896e..8cea465 100644 --- a/src/tuple.h +++ b/src/tuple.h @@ -40,7 +40,9 @@ namespace Raytracer public: 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 Vector(float an_x, float an_y, float an_z); diff --git a/tests/chapitre01_tuples.cpp b/tests/chapitre01_tuples.cpp index 560fde1..9b429ea 100644 --- a/tests/chapitre01_tuples.cpp +++ b/tests/chapitre01_tuples.cpp @@ -69,7 +69,7 @@ TEST_CASE("Point() creates tuples with w=1", "[Tuple][Point]") { 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); - 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)); }