浏览代码

Add comfort methods for arithmetic operations to Vector2 class

Patrick-Christopher Mattulat 1 年之前
父节点
当前提交
1a9e9f2ce7
共有 3 个文件被更改,包括 67 次插入9 次删除
  1. 9 3
      include/ls-math/vector/Vector2.hpp
  2. 36 6
      source/ls-math/vector/Vector2.cpp
  3. 22 0
      test/cases/vector/Vector2Test.cpp

+ 9 - 3
include/ls-math/vector/Vector2.hpp

@@ -24,9 +24,9 @@ namespace ls::math::vector
 
       // arithmetic operations
 
-      ls::math::vector::Vector2 operator+(const ls::math::vector::Vector2& _vector);
-      ls::math::vector::Vector2 operator-(const ls::math::vector::Vector2& _vector);
-      ls::math::core::type::vector_scalar operator*(const ls::math::vector::Vector2& _vector);
+      ls::math::vector::Vector2 operator+(const ls::math::vector::Vector2& _vector) const;
+      ls::math::vector::Vector2 operator-(const ls::math::vector::Vector2& _vector) const;
+      ls::math::core::type::vector_scalar operator*(const ls::math::vector::Vector2& _vector) const;
       ls::math::vector::Vector2 operator*(double _value);
       ls::math::vector::Vector2 operator/(double _divisor);
 
@@ -37,19 +37,25 @@ namespace ls::math::vector
 
       // additional functionality
 
+      static ls::math::vector::Vector2 add(const ls::math::vector::Vector2& _leftAddend, const ls::math::vector::Vector2& _rightAddend);
+      static ls::math::core::type::vector_scalar dot(const ls::math::vector::Vector2& _leftAddend, const ls::math::vector::Vector2& _rightAddend);
       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);
+      static ls::math::vector::Vector2 subtract(const ls::math::vector::Vector2& _leftAddend, const ls::math::vector::Vector2& _rightAddend);
 
     private:
 
       ls::math::core::type::vector2_components components{};
 
+      static ls::math::vector::Vector2 _add(const ls::math::vector::Vector2& _leftAddend, const ls::math::vector::Vector2& _rightAddend);
+      static ls::math::core::type::vector_scalar _dot(const ls::math::vector::Vector2& _leftAddend, const ls::math::vector::Vector2& _rightAddend);
       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);
+      static ls::math::vector::Vector2 _subtract(const ls::math::vector::Vector2& _leftAddend, const ls::math::vector::Vector2& _rightAddend);
   };
 }
 

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

@@ -16,19 +16,19 @@ ls::math::vector::Vector2::Vector2(const ls::math::core::type::vector2_component
   this->components[1] = _y;
 }
 
-ls::math::vector::Vector2 ls::math::vector::Vector2::operator+(const ls::math::vector::Vector2& _vector)
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator+(const ls::math::vector::Vector2& _vector) const
 {
-  return ls::math::vector::Vector2{this->components[0] + _vector.components[0], this->components[1] + _vector.components[1]};
+  return ls::math::vector::Vector2::_add(*this, _vector);
 }
 
-ls::math::vector::Vector2 ls::math::vector::Vector2::operator-(const ls::math::vector::Vector2& _vector)
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator-(const ls::math::vector::Vector2& _vector) const
 {
-  return ls::math::vector::Vector2{this->components[0] - _vector.components[0], this->components[1] - _vector.components[1]};
+  return ls::math::vector::Vector2::_subtract(*this, _vector);
 }
 
-ls::math::core::type::vector_scalar ls::math::vector::Vector2::operator*(const ls::math::vector::Vector2& _vector)
+ls::math::core::type::vector_scalar ls::math::vector::Vector2::operator*(const ls::math::vector::Vector2& _vector) const
 {
-  return this->components[0] * _vector.components[0] + this->components[1] * _vector.components[1];
+  return ls::math::vector::Vector2::_dot(*this, _vector);
 }
 
 ls::math::vector::Vector2 ls::math::vector::Vector2::operator*(double _value)
@@ -51,6 +51,16 @@ 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];
 }
 
+ls::math::vector::Vector2 ls::math::vector::Vector2::add(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
+{
+  return ls::math::vector::Vector2::_add(_leftAddend, _rightAddend);
+}
+
+ls::math::core::type::vector_scalar ls::math::vector::Vector2::dot(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
+{
+  return ls::math::vector::Vector2::_dot(_leftAddend, _rightAddend);
+}
+
 double ls::math::vector::Vector2::getLength()
 {
   return ls::math::vector::Vector2::_getLength(this->components[0], this->components[1]);
@@ -82,6 +92,26 @@ void ls::math::vector::Vector2::setY(const ls::math::core::type::vector2_compone
   this->components[1] = _value;
 }
 
+ls::math::vector::Vector2 ls::math::vector::Vector2::subtract(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
+{
+  return ls::math::vector::Vector2::_subtract(_leftAddend, _rightAddend);
+}
+
+ls::math::vector::Vector2 ls::math::vector::Vector2::_add(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
+{
+  return ls::math::vector::Vector2{_leftAddend.components[0] + _rightAddend.components[0], _leftAddend.components[1] + _rightAddend.components[1]};
+}
+
+ls::math::core::type::vector_scalar ls::math::vector::Vector2::_dot(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
+{
+  return _leftAddend.components[0] * _rightAddend.components[0] + _leftAddend.components[1] * _rightAddend.components[1];
+}
+
+ls::math::vector::Vector2 ls::math::vector::Vector2::_subtract(const ls::math::vector::Vector2 &_leftAddend, const ls::math::vector::Vector2 &_rightAddend)
+{
+  return ls::math::vector::Vector2{_leftAddend.components[0] - _rightAddend.components[0], _leftAddend.components[1] - _rightAddend.components[1]};
+}
+
 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);

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

@@ -97,6 +97,20 @@ namespace
     ASSERT_FALSE(Vector2(3, 4) != Vector2(3, 4));
   }
 
+  TEST_F(Vector2Test, add)
+  {
+    Vector2 c = Vector2::add(Vector2(3, 4), Vector2(5, 3));
+
+    ASSERT_FLOAT_EQ(8, c.getX());
+    ASSERT_FLOAT_EQ(7, c.getY());
+  }
+
+  TEST_F(Vector2Test, dot)
+  {
+    vector_scalar scalar = ls::math::vector::Vector2::dot(Vector2(3, 4), Vector2(5, 3));
+    ASSERT_FLOAT_EQ(27, scalar);
+  }
+
   TEST_F(Vector2Test, getLength)
   {
     Vector2 a{6, 8};
@@ -147,4 +161,12 @@ namespace
 
     ASSERT_FLOAT_EQ(8, a.getY());
   }
+
+  TEST_F(Vector2Test, subtract)
+  {
+    Vector2 c = ls::math::vector::Vector2::subtract(Vector2(3, 4), Vector2(5, 3));
+
+    ASSERT_FLOAT_EQ(-2, c.getX());
+    ASSERT_FLOAT_EQ(1, c.getY());
+  }
 }