[FEAT] add operator * and / and magnitude
This commit is contained in:
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"cmake.sourceDirectory": "/home/jbnadal/sources/jb/raytracing_challenge",
|
||||
"cSpell.words": [
|
||||
"NADAL",
|
||||
"Raytracer"
|
||||
]
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user