[FEAT] Tuple add negative
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user