Browse Source

Vectors in C++: vec2 operator overloading

Godzil 1 year ago
parent
commit
1ffbdb5ab7
1 changed files with 70 additions and 3 deletions
  1. 70 3
      source/include/vec2.h

+ 70 - 3
source/include/vec2.h

@@ -30,10 +30,10 @@ struct vec2
         this->x -= v.x;
         this->y -= v.y;
     };
-    void scale(const float v)
+    void scale(const double n)
     {
-        this->x *= v;
-        this->y *= v;
+        this->x *= n;
+        this->y *= n;
     };
     vec2 rotate(const double angle) const
     {
@@ -87,6 +87,73 @@ struct vec2
         /* return the imaginary Z component */
         return (this->x *  v.y) - (this->y * v.x);
     };
+
+    vec2 & operator = (const vec2 &v)
+    {
+        this->x = v.x;
+        this->y = v.y;
+        return *this;
+    };
+    bool operator == (const vec2 &v) const
+    {
+        return (this->x == v.x) && (this->y == v.y);
+    };
+    bool operator != (const vec2 &v) const
+    {
+        return (this->x != v.x) || (this->y != v.y);
+    };
+
+    vec2 operator + (const vec2 &v) const
+    {
+        return vec2(this->x + v.x,
+                    this->y + v.y);
+    }
+    vec2 operator - (const vec2 &v) const
+    {
+        return vec2(this->x - v.x,
+                    this->y - v.y);
+    };
+    vec2 operator * (const double n) const
+    {
+        return vec2(this->x * n,
+                    this->y * n);
+    };
+    vec2 operator / (const double n) const
+    {
+        return vec2(this->x / n,
+                    this->y / n);
+    };
+    vec2 operator - ()
+    {
+        return vec2(-this->x,
+                    -this->y);
+    };
+
+    vec2 & operator += (const vec2 &v)
+    {
+        this->x += v.x;
+        this->y += v.y;
+        return *this;
+    };
+    vec2 & operator -= (const vec2 &v)
+    {
+        this->x -= v.x;
+        this->y -= v.y;
+        return *this;
+    }
+    vec2 & operator *= (const double n)
+    {
+        this->x *= n;
+        this->y *= n;
+        return *this;
+    }
+    vec2 & operator /= (const double n)
+    {
+        this->x /= n;
+        this->y /= n;
+        return *this;
+    }
+
 };
 
 #endif /* PHYSICENGINE_VEC2_H */