From 6bbc2b7d9672a1b3608f3013dfc6119c94d90d96 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 30 Jan 2024 12:32:44 +0100 Subject: [PATCH] [FEAT] add operator * and / and magnitude --- .vscode/settings.json | 1 + raytracing/src/tuple.cpp | 35 ++++++++++++++++++++ raytracing/src/tuple.h | 4 +++ tests/chapitre01_tuples.cpp | 65 +++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 06362fd..d5acdbb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "cmake.sourceDirectory": "/home/jbnadal/sources/jb/raytracing_challenge", "cSpell.words": [ + "NADAL", "Raytracer" ] } \ No newline at end of file diff --git a/raytracing/src/tuple.cpp b/raytracing/src/tuple.cpp index 2773693..2957a63 100644 --- a/raytracing/src/tuple.cpp +++ b/raytracing/src/tuple.cpp @@ -34,6 +34,7 @@ #include "tuple.h" #define kEpsilon 0.00001 +#define square(a) (a)*(a) using namespace Raytracer; @@ -93,6 +94,30 @@ const Tuple &Tuple::operator-(void) /* ------------------------------------------------------------------------- */ +const Tuple &Tuple::operator*(float a_scalar) +{ + m_x *= a_scalar; + m_y *= a_scalar; + m_z *= a_scalar; + m_w *= a_scalar; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + +const Tuple &Tuple::operator/(float a_scalar) +{ + m_x /= a_scalar; + m_y /= a_scalar; + m_z /= a_scalar; + m_w /= a_scalar; + + return *this; +} + +/* ------------------------------------------------------------------------- */ + Tuple Tuple::Point(float an_x, float an_y, float an_z) { return Tuple(an_x, an_y, an_z, kRaytracerTuplePoint); @@ -149,6 +174,16 @@ bool Tuple::is_vector(void) /* ------------------------------------------------------------------------- */ +float Tuple::magnitude(void) +{ + return sqrtf( + square(m_x) + square(m_y) + square(m_z) + square(m_w) + ); + +} + +/* ------------------------------------------------------------------------- */ + bool Tuple::float_equal(float a, float b) const { if ((std::abs((a) - (b)) < kEpsilon)) diff --git a/raytracing/src/tuple.h b/raytracing/src/tuple.h index fc7aced..d72a728 100644 --- a/raytracing/src/tuple.h +++ b/raytracing/src/tuple.h @@ -44,6 +44,8 @@ namespace Raytracer const Tuple &operator+(const Tuple &an_other); const Tuple &operator-(const Tuple &an_other); const Tuple &operator-(void); + const Tuple &operator*(float a_scalar); + const Tuple &operator/(float a_scalar); static Tuple Point(float an_x, float an_y, float an_z); static Tuple Vector(float an_x, float an_y, float an_z); @@ -57,6 +59,8 @@ namespace Raytracer bool is_point(void); bool is_vector(void); + float magnitude(void); + protected: float m_x; float m_y; diff --git a/tests/chapitre01_tuples.cpp b/tests/chapitre01_tuples.cpp index 8b0d016..a8e958a 100644 --- a/tests/chapitre01_tuples.cpp +++ b/tests/chapitre01_tuples.cpp @@ -25,6 +25,8 @@ /*---------------------------------------------------------------------------*/ +#include + #include #include "tuple.h" @@ -136,3 +138,66 @@ TEST_CASE("Negative a tuple", "[Tuple][Operations]") REQUIRE(-a == Tuple(-1, 2, -3, 4)); } + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Multiplying a tuple by a scalar", "[Tuple][Multiplication]") +{ + Tuple a(1, -2, 3, -4); + + REQUIRE(a * 3.5 == Tuple(3.5, -7, 10.5, -14)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Dividing a tuple by a scalar", "[Tuple][Multiplication]") +{ + Tuple a(1, -2, 3, -4); + + REQUIRE(a / 2 == Tuple(0.5, -1, 1.5, -2)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Computing the magnitude of vector(1,0,0)", "[Tuple][Magnitude]") +{ + Tuple v = Tuple::Vector(1, 0, 0); + + REQUIRE(v.magnitude() == 1); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Computing the magnitude of vector(0,1,0)", "[Tuple][Magnitude]") +{ + Tuple v = Tuple::Vector(0, 1, 0); + + REQUIRE(v.magnitude() == 1); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Computing the magnitude of vector(0,0,1)", "[Tuple][Magnitude]") +{ + Tuple v = Tuple::Vector(0, 0, 1); + + REQUIRE(v.magnitude() == 1); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Computing the magnitude of vector(1,2,3)", "[Tuple][Magnitude]") +{ + Tuple v = Tuple::Vector(1, 2, 3); + + REQUIRE(v.magnitude() == sqrtf(14)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("Computing the magnitude of vector(-1,-2,-3)", "[Tuple][Magnitude]") +{ + Tuple v = Tuple::Vector(-1, -2, -3); + + REQUIRE(v.magnitude() == sqrtf(14)); +}