[FEAT] add dot and cross
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user