diff --git a/tempest/vector/C3Vector.cpp b/tempest/vector/C3Vector.cpp index c0d386a..df3054e 100644 --- a/tempest/vector/C3Vector.cpp +++ b/tempest/vector/C3Vector.cpp @@ -1,4 +1,5 @@ #include "tempest/vector/C3Vector.hpp" +#include "tempest/Math.hpp" C3Vector& C3Vector::operator*=(float a) { this->x *= a; @@ -8,6 +9,10 @@ C3Vector& C3Vector::operator*=(float a) { return *this; } +float C3Vector::Mag() const { + return CMath::sqrt(this->SquaredMag()); +} + float C3Vector::SquaredMag() const { return this->x * this->x + this->y * this->y + this->z * this->z; } diff --git a/tempest/vector/C3Vector.hpp b/tempest/vector/C3Vector.hpp index 3e870f4..7531a9b 100644 --- a/tempest/vector/C3Vector.hpp +++ b/tempest/vector/C3Vector.hpp @@ -15,6 +15,7 @@ class C3Vector { , y(y) , z(z) {}; C3Vector& operator*=(float a); + float Mag() const; float SquaredMag() const; }; diff --git a/test/Vector.cpp b/test/Vector.cpp index 341c4b6..18ad882 100644 --- a/test/Vector.cpp +++ b/test/Vector.cpp @@ -43,6 +43,23 @@ TEST_CASE("C3Vector::operator*=", "[vector]") { } } +TEST_CASE("C3Vector::Mag", "[vector]") { + SECTION("calculates mag") { + auto vector = C3Vector(4.0f, 16.0f, 32.0f); + REQUIRE(vector.Mag() == 36.0f); + } + + SECTION("calculates mag of C3Vector(0.0f, 0.0f, 0.0f)") { + auto vector = C3Vector(0.0f, 0.0f, 0.0f); + REQUIRE(vector.Mag() == 0.0f); + } + + SECTION("calculates mag of C3Vector(-4.0f, -16.0f, -32.0f)") { + auto vector = C3Vector(-4.0f, -16.0f, -32.0f); + REQUIRE(vector.Mag() == 36.0f); + } +} + TEST_CASE("C3Vector::SquaredMag", "[vector]") { SECTION("calculates squared mag") { auto vector = C3Vector(1.0f, 2.0f, 3.0f);