Browse Source

Add factor multiplication operator to Vector2 class

Patrick-Christopher Mattulat 1 year ago
parent
commit
39d83003c4

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

@@ -27,6 +27,7 @@ namespace ls::math::vector
       ls::math::vector::Vector2 operator+(Vector2 _vector);
       ls::math::vector::Vector2 operator-(Vector2 _vector);
       ls::math::core::type::vector_scalar operator*(Vector2 _vector);
+      ls::math::vector::Vector2 operator*(double _value);
 
       // additional functionality
 

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

@@ -38,6 +38,15 @@ ls::math::core::type::vector_scalar ls::math::vector::Vector2::operator*(ls::mat
   return this->components[0] * _vector.getX() + this->components[1] * _vector.getY();
 }
 
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator*(double _value)
+{
+  ls::math::vector::Vector2 calculatedVector{0, 0};
+  calculatedVector.setX(components[0] * _value);
+  calculatedVector.setY(components[1] * _value);
+
+  return calculatedVector;
+}
+
 ls::math::core::type::vector2_component ls::math::vector::Vector2::getX()
 {
   return this->components[0];

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

@@ -51,6 +51,14 @@ namespace
     ASSERT_FLOAT_EQ(27, scalar);
   }
 
+  TEST_F(Vector2Test, operator_multiplicator)
+  {
+    Vector2 c = Vector2(3, 4) * 1.5f;
+
+    ASSERT_FLOAT_EQ(4.5f, c.getX());
+    ASSERT_FLOAT_EQ(6.0f, c.getY());
+  }
+
   TEST_F(Vector2Test, getX)
   {
     Vector2 a{3, 4};