Browse Source

Add length functionality for Vector2 class

Patrick-Christopher Mattulat 2 years ago
parent
commit
b73e6206cd

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

@@ -36,6 +36,7 @@ namespace ls::math::vector
 
       // additional functionality
 
+      double getLength();
       ls::math::core::type::vector2_component getX();
       ls::math::core::type::vector2_component getY();
       void setX(const ls::math::core::type::vector2_component& _value);

+ 6 - 0
source/ls-math/vector/Vector2.cpp

@@ -8,6 +8,7 @@
  * */
 
 #include <ls-math/vector/Vector2.hpp>
+#include <math.h>
 
 ls::math::vector::Vector2::Vector2(const ls::math::core::type::vector2_component &_x, const ls::math::core::type::vector2_component &_y)
 {
@@ -57,6 +58,11 @@ bool ls::math::vector::Vector2::operator!=(const ls::math::vector::Vector2 &_vec
   return this->components[0] != _vector.components[0] || this->components[1] != _vector.components[1];
 }
 
+double ls::math::vector::Vector2::getLength()
+{
+  return sqrt(this->components[0] * this->components[0] + this->components[1] * this->components[1]);
+}
+
 ls::math::core::type::vector2_component ls::math::vector::Vector2::getX()
 {
   return this->components[0];

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

@@ -79,6 +79,12 @@ namespace
     ASSERT_FALSE(Vector2(3, 4) != Vector2(3, 4));
   }
 
+  TEST_F(Vector2Test, getLength)
+  {
+    Vector2 a{6, 8};
+    ASSERT_FLOAT_EQ(10, a.getLength());
+  }
+
   TEST_F(Vector2Test, getX)
   {
     Vector2 a{3, 4};