[FEAT] add dot and cross

This commit is contained in:
NADAL Jean-Baptiste
2024-01-30 15:55:42 +01:00
parent 65e26baf5f
commit 3d3b8b7f02
3 changed files with 73 additions and 4 deletions

View File

@@ -70,6 +70,18 @@ bool Tuple::operator==(const Tuple &an_other) const
/* ------------------------------------------------------------------------- */
const Tuple &Tuple::operator=(const Tuple &an_other)
{
m_x = an_other.m_x;
m_y = an_other.m_y;
m_z = an_other.m_z;
m_w = an_other.m_w;
return *this;
}
/* ------------------------------------------------------------------------- */
const Tuple &Tuple::operator+(const Tuple &an_other)
{
m_x += an_other.m_x;
@@ -206,6 +218,27 @@ Tuple Tuple::normalize(void)
/* ------------------------------------------------------------------------- */
double Tuple::dot(const Tuple &a_tuple)
{
return m_x * a_tuple.m_x +
m_y * a_tuple.m_y +
m_z * a_tuple.m_z +
m_w * a_tuple.m_w;
}
/* ------------------------------------------------------------------------- */
Tuple Tuple::cross(const Tuple &a_tuple)
{
return Vector(
m_y * a_tuple.m_z - m_z * a_tuple.m_y,
m_z * a_tuple.m_x - m_x * a_tuple.m_z,
m_x * a_tuple.m_y - m_y * a_tuple.m_x
);
}
/* ------------------------------------------------------------------------- */
bool Tuple::double_equal(double a, double b) const
{
if ((std::abs((a) - (b)) < kEpsilon))

View File

@@ -43,6 +43,7 @@ namespace Raytracer
Tuple(double an_x, double an_y, double an_z, double an_w);
bool operator==(const Tuple &an_other) const;
const Tuple &operator=(const Tuple &an_other);
const Tuple &operator+(const Tuple &an_other);
const Tuple &operator-(const Tuple &an_other);
const Tuple &operator-(void);
@@ -64,14 +65,16 @@ namespace Raytracer
double magnitude(void) const;
Tuple normalize(void);
protected:
double dot(const Tuple &a_tuple);
Tuple cross(const Tuple &a_tuple);
private:
bool double_equal(double a, double b) const;
double m_x;
double m_y;
double m_z;
double m_w;
private:
bool double_equal(double a, double b) const;
};
};

View File

@@ -64,6 +64,18 @@ TEST_CASE("a tuple with w=0 is a vector", "[Tuple]")
/* ------------------------------------------------------------------------- */
TEST_CASE("Tuple could be copy", "[Tuple]")
{
Tuple p = Tuple::Point(4, -4, 3);
Tuple n;
n = p;
REQUIRE(n == p);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Point() creates tuples with w=1", "[Tuple][Point]")
{
Tuple p = Tuple::Point(4, -4, 3);
@@ -231,3 +243,24 @@ TEST_CASE("The magnitude of a normalized vector", "[Tuple][Normalize]")
REQUIRE(norm.magnitude() == 1);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("The dot product of two tuples", "[Tuple][Dot]")
{
Tuple a = Tuple::Vector(1, 2, 3);
Tuple b = Tuple::Vector(2, 3, 4);
REQUIRE(a.dot(b) == 20);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("The cross product of two vector", "[Tuple][Cross]")
{
Tuple a = Tuple::Vector(1, 2, 3);
Tuple b = Tuple::Vector(2, 3, 4);
REQUIRE(a.cross(b) == Tuple::Vector(-1, 2, -1));
REQUIRE(b.cross(a) == Tuple::Vector(1, -2, 1));
}