Browse Source

Add division functionality to Vector2 class

Patrick-Christopher Mattulat 2 years ago
parent
commit
6c9234d27e

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

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-08-05
- * Changed:         2022-08-05
+ * Changed:         2022-08-07
  *
  * */
 
@@ -28,6 +28,7 @@ namespace ls::math::vector
       ls::math::vector::Vector2 operator-(ls::math::vector::Vector2 _vector);
       ls::math::core::type::vector_scalar operator*(ls::math::vector::Vector2 _vector);
       ls::math::vector::Vector2 operator*(double _value);
+      ls::math::vector::Vector2 operator/(double _value);
 
       // comparison operations
 

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

@@ -3,12 +3,12 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-08-05
- * Changed:         2022-08-05
+ * Changed:         2022-08-07
  *
  * */
 
 #include <ls-math/vector/Vector2.hpp>
-#include <math.h>
+#include <cmath>
 
 ls::math::vector::Vector2::Vector2(const ls::math::core::type::vector2_component &_x, const ls::math::core::type::vector2_component &_y)
 {
@@ -48,6 +48,11 @@ ls::math::vector::Vector2 ls::math::vector::Vector2::operator*(double _value)
   return calculatedVector;
 }
 
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator/(double _value)
+{
+  return ls::math::vector::Vector2{this->components[0] / _value, this->components[1] / _value};
+}
+
 bool ls::math::vector::Vector2::operator==(const ls::math::vector::Vector2 &_vector)
 {
   return this->components[0] == _vector.components[0] && this->components[1] == _vector.components[1];

+ 19 - 1
test/cases/vector/Vector2Test.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-08-05
- * Changed:         2022-08-05
+ * Changed:         2022-08-07
  *
  * */
 
 #include <gtest/gtest.h>
 #include <ls-math/ls_math_vector.hpp>
+#include <cmath>
 
 using namespace ls::math::vector;
 using namespace ls::math::core::type;
@@ -59,6 +60,23 @@ namespace
     ASSERT_FLOAT_EQ(6.0f, c.getY());
   }
 
+  TEST_F(Vector2Test, operator_divisor)
+  {
+    Vector2 c = Vector2(150.0f, -25.0f);
+    Vector2 n = c / 152.4f;
+
+    // round by two decimals
+
+    double powValue = pow(10.0f, (double)2);
+    double roundedX = round(n.getX() * powValue) / powValue;
+    double roundedY = round(n.getY() * powValue) / powValue;
+
+    // evaluate result
+
+    ASSERT_FLOAT_EQ(0.98f, roundedX);
+    ASSERT_FLOAT_EQ(-0.16f, roundedY);
+  }
+
   TEST_F(Vector2Test, operator_equals)
   {
     ASSERT_TRUE(Vector2(3, 4) == Vector2(3, 4));