4 * Created on: Aug 6, 2012
8 #ifndef GEOMETRY_VECTOR_H_
9 #define GEOMETRY_VECTOR_H_
15 template<class Scalar>
19 Vector() : x(0), y(0) { }
20 Vector(Scalar x, Scalar y) : x(x), y(y) { }
22 Vector(const Vector<T> &other) : x(other.X()), y(other.Y()) { };
24 Vector(T x, T y) : x(x), y(y) { }
27 Scalar X() const { return x; }
28 Scalar Y() const { return y; }
37 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<T> &rhs) {
38 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
40 template<class T, class U>
41 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<U> &rhs) {
42 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
46 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
47 return lhs = lhs + rhs;
49 template<class T, class U>
50 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
51 return lhs = lhs + rhs;
55 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
56 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
58 template<class T, class U>
59 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<U> &rhs) {
60 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
64 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
65 return lhs = lhs - rhs;
67 template<class T, class U>
68 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
69 return lhs = lhs - rhs;
73 inline Vector<T> operator -(const Vector<T> &v) {
74 return Vector<T>(-v.X(), -v.Y());
78 inline Vector<T> operator *(const Vector<T> &v, T s) {
79 return Vector<T>(v.X() * s, v.Y() * s);
82 inline Vector<T> operator *(T s, const Vector<T> &v) {
83 return Vector<T>(s * v.X(), s * v.Y());
87 inline Vector<T> operator /(const Vector<T> &v, T s) {
88 return Vector<T>(v.X() / s, v.Y() / s);
92 inline bool operator ==(const Vector<T> &lhs, const Vector<T> &rhs) {
93 return lhs.X() == rhs.X() && lhs.Y() == rhs.Y();
97 inline bool operator !=(const Vector<T> &lhs, const Vector<T> &rhs) {
98 return lhs.X() != rhs.X() || lhs.Y() != rhs.Y();
102 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
103 out << '<' << v.X() << ", " << v.Y() << '>';
109 #endif /* GEOMETRY_VECTOR_H_ */