Browse Source

vector division

Kajetan Johannes Hammerle 3 years ago
parent
commit
b7e0240de4
2 changed files with 28 additions and 0 deletions
  1. 13 0
      math/Vector.h
  2. 15 0
      tests/VectorTests.cpp

+ 13 - 0
math/Vector.h

@@ -77,6 +77,19 @@ public:
         return v;
     }
 
+    Vector& operator/=(float factor) {
+        for(int i = 0; i < N; i++) {
+            values[i] /= factor;
+        }
+        return *this;
+    }
+
+    Vector operator/(float factor) const {
+        Vector v = *this;
+        v /= factor;
+        return v;
+    }
+
     float dot(const Vector& v) const {
         float length = 0.0f;
         for(int i = 0; i < N; i++) {

+ 15 - 0
tests/VectorTests.cpp

@@ -168,6 +168,19 @@ static void testMul(Test& test) {
                    Vector3(1.0f, 2.0f, 3.0f) * 3.0, "mul 2");
 }
 
+static void testSetDiv(Test& test) {
+    Vector3 v(12.0f, 24.0f, 9.0f);
+    v /= 3.0f;
+    compareVectors(test, Vector3(4.0f, 8.0f, 3.0f), v, "set div 1");
+    v /= -2.0f;
+    compareVectors(test, Vector3(-2.0f, -4.0f, -1.5f), v, "set div 2");
+}
+
+static void testDiv(Test& test) {
+    compareVectors(test, Vector3(-1.0f, -2.0f, -3.0f),
+                   Vector3(-3.0f, -6.0f, -9.0f) / 3.0f, "div");
+}
+
 static void testDot(Test& test) {
     test.checkFloat(
         9.0f, Vector3(-4.0f, 2.0f, -3.0f).dot(Vector3(-1.0f, -2.0f, -3.0f)),
@@ -223,6 +236,8 @@ void VectorTests::test() {
     testInvert(test);
     testSetMul(test);
     testMul(test);
+    testSetDiv(test);
+    testDiv(test);
     testDot(test);
     testSquareLength(test);
     testLength(test);