diff --git a/src/tuple.cpp b/src/tuple.cpp index 769c3ea..e63fec0 100644 --- a/src/tuple.cpp +++ b/src/tuple.cpp @@ -29,6 +29,7 @@ /* ------------------------------------------------------------------------- */ #include +#include #include "tuple.h" @@ -56,7 +57,7 @@ bool Tuple::operator==(const Tuple &an_other) const /* ------------------------------------------------------------------------- */ -Tuple &Tuple::operator+(const Tuple &an_other) +const Tuple &Tuple::operator+(const Tuple &an_other) { m_x += an_other.m_x; m_y += an_other.m_y; @@ -68,7 +69,7 @@ Tuple &Tuple::operator+(const Tuple &an_other) /* ------------------------------------------------------------------------- */ -Tuple &Tuple::operator-(const Tuple &an_other) +const Tuple &Tuple::operator-(const Tuple &an_other) { m_x -= an_other.m_x; m_y -= an_other.m_y; @@ -80,6 +81,18 @@ Tuple &Tuple::operator-(const Tuple &an_other) /* ------------------------------------------------------------------------- */ +const Tuple &Tuple::operator-(void) +{ + m_x = -m_x; + m_y = -m_y; + m_z = -m_z; + m_w = -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 8cea465..fc7aced 100644 --- a/src/tuple.h +++ b/src/tuple.h @@ -41,8 +41,9 @@ namespace Raytracer Tuple(float an_x, float an_y, float an_z, float an_w); bool operator==(const Tuple &an_other) const; - Tuple &operator+(const Tuple &an_other); - Tuple &operator-(const Tuple &an_other); + const Tuple &operator+(const Tuple &an_other); + const Tuple &operator-(const Tuple &an_other); + const Tuple &operator-(void); 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 9b429ea..0afe901 100644 --- a/tests/chapitre01_tuples.cpp +++ b/tests/chapitre01_tuples.cpp @@ -120,3 +120,22 @@ TEST_CASE("Subtracting two vectors", "[Tuple][Operations]") REQUIRE((v1 - v2) == Tuple::Vector(-2, -4, -6)); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Subtracting a vector from the zero vector", "[Tuple][Operations]") +{ + Tuple zero = Tuple::Vector(0, 0, 0); + Tuple v = Tuple::Vector(1, -2, 3); + + REQUIRE((zero - v) == Tuple::Vector(-1, 2, -3)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Negative a tuple", "[Tuple][Operations]") +{ + Tuple a(1, -2, 3, -4); + + REQUIRE(-a == Tuple(-1, 2, -3, 4)); +}