Переглянути джерело

Add normalize functionality to Vector2 class

Patrick-Christopher Mattulat 1 рік тому
батько
коміт
4978424be3

+ 5 - 1
include/ls-math/vector/Vector2.hpp

@@ -28,7 +28,7 @@ namespace ls::math::vector
       ls::math::vector::Vector2 operator-(ls::math::vector::Vector2 _vector);
       ls::math::core::type::vector_scalar operator*(ls::math::vector::Vector2 _vector);
       ls::math::vector::Vector2 operator*(double _value);
-      ls::math::vector::Vector2 operator/(double _value);
+      ls::math::vector::Vector2 operator/(double _divisor);
 
       // comparison operations
 
@@ -40,12 +40,16 @@ namespace ls::math::vector
       double getLength();
       ls::math::core::type::vector2_component getX();
       ls::math::core::type::vector2_component getY();
+      ls::math::vector::Vector2 normalize();
       void setX(const ls::math::core::type::vector2_component& _value);
       void setY(const ls::math::core::type::vector2_component& _value);
 
     private:
 
       ls::math::core::type::vector2_components components{};
+
+      static double _getLength(const ls::math::core::type::vector2_component& _x, const ls::math::core::type::vector2_component& _y);
+      static ls::math::vector::Vector2 _operatorDivision(const ls::math::core::type::vector2_component& _x, const ls::math::core::type::vector2_component& _y, double _divisor);
   };
 }
 

+ 19 - 3
source/ls-math/vector/Vector2.cpp

@@ -36,9 +36,9 @@ ls::math::vector::Vector2 ls::math::vector::Vector2::operator*(double _value)
   return ls::math::vector::Vector2{components[0] * _value, components[1] * _value};
 }
 
-ls::math::vector::Vector2 ls::math::vector::Vector2::operator/(double _value)
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator/(double _divisor)
 {
-  return ls::math::vector::Vector2{this->components[0] / _value, this->components[1] / _value};
+  return ls::math::vector::Vector2::_operatorDivision(this->components[0], this->components[1], _divisor);
 }
 
 bool ls::math::vector::Vector2::operator==(const ls::math::vector::Vector2 &_vector)
@@ -53,7 +53,7 @@ bool ls::math::vector::Vector2::operator!=(const ls::math::vector::Vector2 &_vec
 
 double ls::math::vector::Vector2::getLength()
 {
-  return sqrt(this->components[0] * this->components[0] + this->components[1] * this->components[1]);
+  return ls::math::vector::Vector2::_getLength(this->components[0], this->components[1]);
 }
 
 ls::math::core::type::vector2_component ls::math::vector::Vector2::getX()
@@ -66,6 +66,12 @@ ls::math::core::type::vector2_component ls::math::vector::Vector2::getY()
   return this->components[1];
 }
 
+ls::math::vector::Vector2 ls::math::vector::Vector2::normalize()
+{
+  double length = ls::math::vector::Vector2::_getLength(this->components[0], this->components[1]);
+  return ls::math::vector::Vector2::_operatorDivision(this->components[0], this->components[1], length);
+}
+
 void ls::math::vector::Vector2::setX(const ls::math::core::type::vector2_component &_value)
 {
   this->components[0] = _value;
@@ -75,3 +81,13 @@ void ls::math::vector::Vector2::setY(const ls::math::core::type::vector2_compone
 {
   this->components[1] = _value;
 }
+
+double ls::math::vector::Vector2::_getLength(const ls::math::core::type::vector2_component& _x, const ls::math::core::type::vector2_component& _y)
+{
+  return sqrt(_x * _x + _y * _y);
+}
+
+ls::math::vector::Vector2 ls::math::vector::Vector2::_operatorDivision(const ls::math::core::type::vector2_component &_x, const ls::math::core::type::vector2_component &_y, double _divisor)
+{
+  return ls::math::vector::Vector2{_x / _divisor, _y / _divisor};
+}

+ 17 - 0
test/cases/vector/Vector2Test.cpp

@@ -115,6 +115,23 @@ namespace
     ASSERT_FLOAT_EQ(4, a.getY());
   }
 
+  TEST_F(Vector2Test, normalize)
+  {
+    Vector2 a{150.0f, -25.0f};
+    Vector2 n = a.normalize();
+
+    // round by two decimals
+
+    double roundedX = round(n.getX() * 1000.0f) / 1000.0f;
+    double roundedY = round(n.getY() * 1000.0f) / 1000.0f;
+
+    // evaluate results
+
+    ASSERT_FLOAT_EQ(0.986f, roundedX);
+    ASSERT_FLOAT_EQ(-0.164f, roundedY);
+    ASSERT_FLOAT_EQ(1.0f, n.getLength());
+  }
+
   TEST_F(Vector2Test, setX)
   {
     Vector2 a{3, 4};