feat(vector): add C3Vector::Normalize

This commit is contained in:
fallenoak 2020-11-26 10:59:42 -06:00
parent e38af58adb
commit 4278157faf
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
3 changed files with 23 additions and 0 deletions

View file

@ -13,6 +13,10 @@ float C3Vector::Mag() const {
return CMath::sqrt(this->SquaredMag()); return CMath::sqrt(this->SquaredMag());
} }
void C3Vector::Normalize() {
this->operator*=(1.0f / this->Mag());
}
float C3Vector::SquaredMag() const { float C3Vector::SquaredMag() const {
return this->x * this->x + this->y * this->y + this->z * this->z; return this->x * this->x + this->y * this->y + this->z * this->z;
} }

View file

@ -16,6 +16,7 @@ class C3Vector {
, z(z) {}; , z(z) {};
C3Vector& operator*=(float a); C3Vector& operator*=(float a);
float Mag() const; float Mag() const;
void Normalize();
float SquaredMag() const; float SquaredMag() const;
}; };

View file

@ -60,6 +60,24 @@ TEST_CASE("C3Vector::Mag", "[vector]") {
} }
} }
TEST_CASE("C3Vector::Normalize", "[vector]") {
SECTION("normalizes C3Vector(1.0f, 1.0f, 1.0f)") {
auto vector = C3Vector(1.0f, 1.0f, 1.0f);
vector.Normalize();
REQUIRE(vector.x == Approx(0.57735f));
REQUIRE(vector.y == Approx(0.57735f));
REQUIRE(vector.z == Approx(0.57735f));
}
SECTION("normalizes C3Vector(4.0f, 16.0f, 32.0f)") {
auto vector = C3Vector(4.0f, 16.0f, 32.0f);
vector.Normalize();
REQUIRE(vector.x == Approx(0.11111f));
REQUIRE(vector.y == Approx(0.44444f));
REQUIRE(vector.z == Approx(0.88888f));
}
}
TEST_CASE("C3Vector::SquaredMag", "[vector]") { TEST_CASE("C3Vector::SquaredMag", "[vector]") {
SECTION("calculates squared mag") { SECTION("calculates squared mag") {
auto vector = C3Vector(1.0f, 2.0f, 3.0f); auto vector = C3Vector(1.0f, 2.0f, 3.0f);