diff -r 561892e2a30e -r 25becd2cb026 src/Vector.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Vector.hh Wed Dec 03 19:16:32 2008 +0000 @@ -0,0 +1,106 @@ +#ifndef VECTOR_HH +#define VECTOR_HH + +#include +#include + +/** + * A 2D Vector class. Implements standard vector operations. + */ +template +class _Vector { +public: + T x; + T y; + + /** + * Default constructor. + */ + _Vector() : x(0), y(0) {} + + /** + * Constuctor. + * + * @param x Initial x-coordinate + * @param y Initial y-coordinate + */ + _Vector(T x, 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*(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; + } + + // Other operations + T length() const { + return sqrt(sqrLength()); + } + T sqrLength() const { + return (this->x * this->x) + (this->y * this->y); + } + _Vector roundToInt() const { + return _Vector((int)(x), (int)(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)); +} +template +bool operator!=(const _Vector &v1, const _Vector &v2) { + return !(v1 == v2); +} + +// Output operator +template +std::ostream& operator<<(std::ostream &s, const _Vector &v) { + return s<<"("< Vector; + +#endif