Browse Source

Add arithmetic operations "+" and "-" for Vector2 class

Patrick-Christopher Mattulat 2 năm trước cách đây
mục cha
commit
b45befc68d

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

@@ -22,7 +22,17 @@ namespace ls::math::vector
       Vector2(const ls::math::core::type::vector2_component& _x, const ls::math::core::type::vector2_component& _y);
       ~Vector2() = default;
 
-      ls::math::core::type::vector2_component getComponent(size_t _index);
+      // arithmetic operations
+
+      ls::math::vector::Vector2 operator+(Vector2 _vector);
+      ls::math::vector::Vector2 operator-(Vector2 _vector);
+
+      // additional functionality
+
+      ls::math::core::type::vector2_component getX();
+      ls::math::core::type::vector2_component getY();
+      void setX(const ls::math::core::type::vector2_component& _value);
+      void setY(const ls::math::core::type::vector2_component& _value);
 
     private:
 

+ 35 - 2
source/ls-math/vector/Vector2.cpp

@@ -15,7 +15,40 @@ ls::math::vector::Vector2::Vector2(const ls::math::core::type::vector2_component
   this->components[1] = _y;
 }
 
-ls::math::core::type::vector2_component ls::math::vector::Vector2::getComponent(std::size_t _index)
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator+(Vector2 _vector)
 {
-  return this->components.at(_index); // TODO: throw IndexOutOfBoundsException
+  ls::math::vector::Vector2 calculatedVector{0, 0};
+  calculatedVector.setX(this->components[0] + _vector.getX());
+  calculatedVector.setY(this->components[1] + _vector.getY());
+
+  return calculatedVector;
+}
+
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator-(ls::math::vector::Vector2 _vector)
+{
+  ls::math::vector::Vector2 calculatedVector{0, 0};
+  calculatedVector.setX(this->components[0] - _vector.getX());
+  calculatedVector.setY(this->components[1] - _vector.getY());
+
+  return calculatedVector;
+}
+
+ls::math::core::type::vector2_component ls::math::vector::Vector2::getX()
+{
+  return this->components[0];
+}
+
+ls::math::core::type::vector2_component ls::math::vector::Vector2::getY()
+{
+  return this->components[1];
+}
+
+void ls::math::vector::Vector2::setX(const ls::math::core::type::vector2_component &_value)
+{
+  this->components[0] = _value;
+}
+
+void ls::math::vector::Vector2::setY(const ls::math::core::type::vector2_component &_value)
+{
+  this->components[1] = _value;
 }

+ 39 - 3
test/cases/vector/Vector2Test.cpp

@@ -28,11 +28,47 @@ namespace
       {}
   };
 
-  TEST_F(Vector2Test, getComponent)
+  TEST_F(Vector2Test, operator_addition)
+  {
+    Vector2 c = Vector2(3, 4) + Vector2(5, 3);
+
+    ASSERT_FLOAT_EQ(8, c.getX());
+    ASSERT_FLOAT_EQ(7, c.getY());
+  }
+
+  TEST_F(Vector2Test, operator_substraction)
+  {
+    Vector2 c = Vector2(3, 4) - Vector2(5, 3);
+
+    ASSERT_FLOAT_EQ(-2, c.getX());
+    ASSERT_FLOAT_EQ(1, c.getY());
+  }
+
+  TEST_F(Vector2Test, getX)
+  {
+    Vector2 a{3, 4};
+    ASSERT_FLOAT_EQ(3, a.getX());
+  }
+
+  TEST_F(Vector2Test, getY)
+  {
+    Vector2 a{3, 4};
+    ASSERT_FLOAT_EQ(4, a.getY());
+  }
+
+  TEST_F(Vector2Test, setX)
+  {
+    Vector2 a{3, 4};
+    a.setX(14);
+
+    ASSERT_FLOAT_EQ(14, a.getX());
+  }
+
+  TEST_F(Vector2Test, setY)
   {
     Vector2 a{3, 4};
+    a.setY(8);
 
-    ASSERT_FLOAT_EQ(3, a.getComponent(0));
-    ASSERT_FLOAT_EQ(4, a.getComponent(1));
+    ASSERT_FLOAT_EQ(8, a.getY());
   }
 }