# HG changeset patch # User saiam # Date 1227975145 0 # Node ID 81406f8b7535203a763c11695b0115493f5f4371 # Parent b1ae79a2d2f01a65abcad37ac95bb3581751453d New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use operators +, -, * and /. diff -r b1ae79a2d2f0 -r 81406f8b7535 src/proto2/Vector.hh --- a/src/proto2/Vector.hh Fri Nov 28 22:40:11 2008 +0000 +++ b/src/proto2/Vector.hh Sat Nov 29 16:12:25 2008 +0000 @@ -1,29 +1,39 @@ -#ifndef COOR_H -#define COOR_H +#ifndef VECTOR_HH +#define VECTOR_HH #include #include /** - * 2D Vector class. Implements standard vector operations. + * A 2D Vector class. Implements standard vector operations. */ template class _Vector { public: T x; T y; + + /** + * Default constructor. + */ + _Vector() : x(0), y(0) {} - _Vector() : x(0), y(0){} /** - * @param x Initial x-coordinate. - * @param y Initial y-coordinate. + * Constuctor. + * + * @param x Initial x-coordinate + * @param y Initial y-coordinate */ _Vector(T x, T y) : x(x), y(y) {} + /** - * @param v Other vector to be copied. + * 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; @@ -34,39 +44,44 @@ _Vector operator-(const _Vector &v) const { return _Vector(this->x-v.x, this->y-v.y); } - _Vector operator*(const T &d) const { - return _Vector(this->x*d, this->y*d); + _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->x += v.x; - this->y += v.y; + *this = *this + v; } void operator-=(const _Vector &v) { - this->x -= v.x; - this->y -= v.y; + *this = *this - v; } - void operator*=(const T &f) { - this->x *= f; - this->y *= f; + void operator*=(const T &scalar) { + *this = *this * scalar; } - void operator/=(const T &d) { - this->x /= d; - this->y /= d; + void operator/=(const T &scalar) { + *this = *this / scalar; } + + // Other operations T length() const { - return sqrt(x*x+y*y); + return sqrt((this->x * this->x) + (this->y * this->y)); } _Vector roundToInt() const { - return _Vector((int)x, (int)y); + return _Vector(round(x), round(y)); } }; +// Unary operators +template +_Vector operator*(const T &scalar, const _Vector v) { + return (v * scalar); +} + +// Comparison operators template bool operator==(const _Vector &v1, const _Vector &v2) { return ((v1.x == v2.x) && (v1.y == v2.y)); @@ -76,11 +91,13 @@ return !(v1 == v2); } +// Output operator template std::ostream& operator<<(std::ostream &s, const _Vector &v) { return s<<"("< Vector; #endif