# HG changeset patch # User saiam # Date 1227037321 0 # Node ID a89e021189310863733c74902e6d1f229e57532b # Parent 38f269310f77497d83089cc9a683b175afec8f01 Added some operators. diff -r 38f269310f77 -r a89e02118931 src/proto2/Vector.hh --- a/src/proto2/Vector.hh Tue Nov 18 19:33:31 2008 +0000 +++ b/src/proto2/Vector.hh Tue Nov 18 19:42:01 2008 +0000 @@ -3,6 +3,9 @@ #include +/** + * 2D Vector class. Implements standard vector operations. + */ template class _Vector { public: @@ -10,39 +13,58 @@ T y; _Vector() : x(0), y(0){} + /** + * @param x Initial x-coordinate. + * @param y Initial y-coordinate. + */ _Vector(T x, T y) : x(x), y(y) {} - _Vector(const _Vector& v) : x(v.x), y(v.y) {} + /** + * @param v Other vector to be copied. + */ + _Vector(const _Vector &v) : x(v.x), y(v.y) {} - void operator=(const _Vector& v) { + void operator=(const _Vector &v) { this->x = v.x; this->y = v.y; } - _Vector operator+(const _Vector& v) const { + _Vector operator+(const _Vector &v) const { return _Vector(this->x+v.x, this->y+v.y); } - _Vector operator-(const _Vector& v) const { + _Vector operator-(const _Vector &v) const { return _Vector(this->x-v.x, this->y-v.y); } - _Vector operator*(const T d) const { + _Vector operator*(const T &d) const { return _Vector(this->x*d, this->y*d); } - _Vector operator/(const T d) const { + _Vector operator/(const T &d) const { return _Vector(this->x/d, this->y/d); } - void operator+=(const _Vector& v) { + void operator+=(const _Vector &v) { this->x += v.x; this->y += v.y; } - void operator*=(const T f) { + void operator-=(const _Vector &v) { + this->x -= v.x; + this->y -= v.y; + } + void operator*=(const T &f) { this->x *= f; this->y *= f; } + void operator/=(const T &d) { + this->x /= d; + this->y /= d; + } }; template -bool operator==(const _Vector& v1, const _Vector& v2) { +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); +} template std::ostream& operator<<(std::ostream &s, _Vector &v) {