src/Vector.hh
changeset 370 39e59dd36b6e
parent 300 417183866f35
child 377 01d3c340b372
--- a/src/Vector.hh	Tue Dec 09 04:49:33 2008 +0000
+++ b/src/Vector.hh	Mon Dec 15 14:24:38 2008 +0000
@@ -7,106 +7,171 @@
 /**
  * A 2D Vector class. Implements standard vector operations.
  */
-template <typename T>
-class _Vector {
+template <typename T> class VectorType {
 public:
+    /** Horizontal component */
     T x;
+    /** Vertical component */
     T y;
 
     /**
-     * Default constructor.
+     * Default constructor. Values are zero
      */
-    _Vector() : x(0), y(0) {}
+    VectorType() : 
+        x(0), y(0) 
+    {
+
+    }
     
     /**
-     * Constuctor.
+     * Scalar constuctor.
      *
      * @param x Initial x-coordinate
      * @param y Initial y-coordinate
      */
-    _Vector(T x, T y) : x(x), y(y) {}
+    VectorType (const T &x, const T &y) : 
+        x(x), y(y) 
+    {
+    
+    }
 
     /**
      * Copy constructor.
      *
      * @param v Vector to be copied.
      */
-    _Vector(const _Vector &v) : x(v.x), y(v.y) {}
-
-    // Operator declarations
-    void operator=(const _Vector &v) {
-        this->x = v.x;
-        this->y = v.y;
-    }
-    _Vector operator+(const _Vector &v) const {
-        return _Vector(this->x+v.x, this->y+v.y);
-    }
-    _Vector operator-(const _Vector &v) const {
-        return _Vector(this->x-v.x, this->y-v.y);
-    }
-    _Vector operator- (void) const {
-        return _Vector(-this->x, -this->y);
-    }
-    _Vector operator*(const T &scalar) const {
-        return _Vector(this->x*scalar, this->y*scalar);
-    }
-    T operator*(const _Vector &v) const {
-        return (this->x*v.x + this->y*v.y);
-    }
-    _Vector operator/ (const T &d) const {
-        return _Vector(this->x / d, this->y / d);
-    }
-    void operator+=(const _Vector &v) {
-        *this = *this + v;
-    }
-    void operator-=(const _Vector &v) {
-        *this = *this - v;
-    }
-    void operator*=(const T &scalar) {
-        *this = *this * scalar;
-    }
-    void operator/=(const T &scalar) {
-        *this = *this / scalar;
+    VectorType (const VectorType &v) : 
+        x(v.x), 
+        y(v.y) 
+    {
+    
     }
 
-    // Other operations
-    T length() const {
+    /**
+     * @name Standard operators
+     *
+     * @{
+     */
+    VectorType& operator= (const VectorType &v) {
+        x = v.x;
+        y = v.y;
+
+        return *this;
+    }
+
+    VectorType operator+ (const VectorType &v) const {
+        return VectorType(x + v.x, y + v.y);
+    }
+
+    VectorType operator- (const VectorType &v) const {
+        return VectorType(x - v.x, y - v.y);
+    }
+    
+    /** Unary minus (v + -v = 0) */
+    VectorType operator- (void) const {
+        return VectorType(-x, -y);
+    }
+    
+    /** Scalar multiplication */
+    VectorType operator* (const T &scalar) const {
+        return VectorType(x * scalar, y * scalar);
+    }
+   
+    /** Scalar division */
+    VectorType operator/ (const T &scalar) const {
+        return VectorType(x / scalar, y / scalar);
+    }
+
+    /** Dot product */
+    T operator* (const VectorType &v) const {
+        return (x * v.x + y * v.y);
+    }
+
+    void operator+= (const VectorType &v) {
+        x += v.x;
+        y += v.y;
+    }
+
+    void operator-= (const VectorType &v) {
+        x -= v.x;
+        y -= v.y;
+    }
+
+    void operator*= (const T &scalar) {
+        x *= scalar;
+        y *= scalar;
+    }
+
+    void operator/= (const T &scalar) {
+        x /= scalar;
+        y /= scalar;
+    }
+    
+    /**
+     * XXX: This needs to do some rounding for float-vectors
+     */
+    bool operator== (const VectorType &other) const {
+        return (x == other.x) && (y ==  other.y);
+    }
+
+    /**
+     * XXX: This needs to do some rounding for float-vectors
+     */
+    bool operator!= (const VectorType &other) const {
+        return (x != other.x) || (y !=  other.y);
+    }
+
+    // @}
+    
+    /**
+     * Vector scalar length
+     */
+    T length (void) const {
         return sqrt(sqrLength());
     }
-    T sqrLength() const {
-        return (this->x * this->x) + (this->y * this->y);
+
+    /**
+     * Vector scalar length, squared
+     */
+    T sqrLength (void) const {
+        return (x * x) + (y * y);
     }
-    _Vector roundToInt() const {
-        return _Vector((int)(x), (int)(y));
+    
+    /**
+     * XXX: should be replaced with a working operator== implementation
+     *
+     * This doesn't actually *round*, it *truncates*
+     */
+    VectorType roundToInt (void) const {
+        return VectorType((int)(x), (int)(y));
+    }
+    
+    /**
+     * Test for a zero-length vector.
+     *
+     * XXX: this is currently only used when testing for Vector(0, 0), probably breaks otherwise
+     */
+    bool zero (void) const {
+        return x == 0 && y == 0;
     }
 
-    // test Vectors as booleans
-    // XXX: comparing floats against zero... is a bad idea?
-    bool zero(void) const {
-        return x == 0 && y == 0;
-    }
 };
 
-// Unary operators
-template<typename T>
-_Vector<T> operator*(const T &scalar, const _Vector<T> v) {
+/**
+ * @name Postfix operators
+ * Operators with the vector as the second argument :)
+ *
+ * @{
+ */
+template<typename T> VectorType<T> operator* (const T &scalar, const VectorType<T> &v) {
     return (v * scalar);
 } 
 
-// Comparison operators
-template<typename T>
-bool operator==(const _Vector<T> &v1, const _Vector<T> &v2) {
-    return ((v1.x == v2.x) && (v1.y == v2.y));
-}
-template<typename T>
-bool operator!=(const _Vector<T> &v1, const _Vector<T> &v2) {
-    return !(v1 == v2);
-}
-
-// Output operator
-template<typename T>
-std::ostream& operator<<(std::ostream &s, const _Vector<T> &v) {
-    return s<<"("<<v.x<<", "<<v.y<<")";
+/**
+ * Make vectors printable
+ */
+template<typename T> std::ostream& operator<< (std::ostream &s, const VectorType<T> &v) {
+    return s << "(" << v.x << ", " << v.y << ")";
 }
 
 #endif