[FEAT] Tuple add negative

This commit is contained in:
2024-01-29 23:56:45 +01:00
parent 3595f21d70
commit 3573d634ac
3 changed files with 37 additions and 4 deletions

View File

@@ -29,6 +29,7 @@
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
#include <cmath> #include <cmath>
#include <cstdio>
#include "tuple.h" #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_x += an_other.m_x;
m_y += an_other.m_y; 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_x -= an_other.m_x;
m_y -= an_other.m_y; 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) 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

@@ -41,8 +41,9 @@ namespace Raytracer
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 &an_other) const; bool operator==(const Tuple &an_other) const;
Tuple &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-(void);
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

@@ -120,3 +120,22 @@ TEST_CASE("Subtracting two vectors", "[Tuple][Operations]")
REQUIRE((v1 - v2) == Tuple::Vector(-2, -4, -6)); 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));
}