mirror of
https://github.com/thunderbrewhq/typhoon.git
synced 2025-12-12 10:32:29 +00:00
feat(vector): add C3Vector::Normalize
This commit is contained in:
parent
e38af58adb
commit
4278157faf
3 changed files with 23 additions and 0 deletions
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue