浏览代码

Add scalar product operator to Vector2 class

Patrick-Christopher Mattulat 2 年之前
父节点
当前提交
a79d4b70f5

+ 1 - 0
include/ls-math/core/types/VectorTypes.hpp

@@ -16,6 +16,7 @@ namespace ls::math::core::type
 {
   using vector2_component = double;
   using vector2_components = std::array<ls::math::core::type::vector2_component, 2>;
+  using vector_scalar = ls::math::core::type::vector2_component;
 }
 
 #endif

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

@@ -26,6 +26,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);
 
       // additional functionality
 

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

@@ -33,6 +33,11 @@ ls::math::vector::Vector2 ls::math::vector::Vector2::operator-(ls::math::vector:
   return calculatedVector;
 }
 
+ls::math::core::type::vector_scalar ls::math::vector::Vector2::operator*(ls::math::vector::Vector2 _vector)
+{
+  return this->components[0] * _vector.getX() + this->components[1] * _vector.getY();
+}
+
 ls::math::core::type::vector2_component ls::math::vector::Vector2::getX()
 {
   return this->components[0];

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

@@ -11,6 +11,7 @@
 #include <ls-math/ls_math_vector.hpp>
 
 using namespace ls::math::vector;
+using namespace ls::math::core::type;
 
 namespace
 {
@@ -44,6 +45,12 @@ namespace
     ASSERT_FLOAT_EQ(1, c.getY());
   }
 
+  TEST_F(Vector2Test, operator_scalar)
+  {
+    vector_scalar scalar = Vector2(3, 4) * Vector2(5, 3);
+    ASSERT_FLOAT_EQ(27, scalar);
+  }
+
   TEST_F(Vector2Test, getX)
   {
     Vector2 a{3, 4};