4 * Created on: Aug 6, 2012
8 #ifndef GEOMETRY_VECTOR_H_
9 #define GEOMETRY_VECTOR_H_
17 template<class Scalar>
21 Vector() : x(0), y(0) { }
22 Vector(Scalar x, Scalar y) : x(x), y(y) { }
24 Vector(const Vector<T> &other) : x(other.X()), y(other.Y()) { };
26 Vector(T x, T y) : x(x), y(y) { }
29 Scalar X() const { return x; }
30 Scalar Y() const { return y; }
32 Scalar Index(Scalar lineLength) const { return Y() * lineLength + X(); }
33 static Vector<Scalar> FromIndex(Scalar index, Scalar lineLength) {
34 return Vector<Scalar>(index % lineLength, index / lineLength);
37 void Lock(const Vector<Scalar> &to);
46 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<T> &rhs) {
47 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
49 template<class T, class U>
50 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<U> &rhs) {
51 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
55 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
56 return lhs = lhs + rhs;
58 template<class T, class U>
59 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
60 return lhs = lhs + rhs;
64 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
65 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
67 template<class T, class U>
68 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<U> &rhs) {
69 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
73 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
74 return lhs = lhs - rhs;
76 template<class T, class U>
77 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
78 return lhs = lhs - rhs;
82 inline Vector<T> operator -(const Vector<T> &v) {
83 return Vector<T>(-v.X(), -v.Y());
87 inline Vector<T> operator *(const Vector<T> &v1, const Vector<T> &v2) {
88 return Vector<T>(v1.X() * v2.X(), v1.Y() * v2.Y());
91 inline Vector<T> operator *(const Vector<T> &v, T s) {
92 return Vector<T>(v.X() * s, v.Y() * s);
95 inline Vector<T> operator *(T s, const Vector<T> &v) {
96 return Vector<T>(s * v.X(), s * v.Y());
100 inline Vector<T> operator /(const Vector<T> &v1, const Vector<T> &v2) {
101 return Vector<T>(v1.X() / v2.X(), v1.Y() / v2.Y());
104 inline Vector<T> operator /(const Vector<T> &v, T s) {
105 return Vector<T>(v.X() / s, v.Y() / s);
108 inline Vector<T> operator /(T s, const Vector<T> &v) {
109 return Vector<T>(s / v.X(), s / v.Y());
113 inline Vector<T> operator %(const Vector<T> &v1, const Vector<T> &v2) {
114 return Vector<T>(v1.X() % v2.X(), v1.Y() % v2.Y());
117 inline Vector<float> operator %(const Vector<float> &v1, const Vector<float> &v2) {
118 return Vector<float>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
121 inline Vector<double> operator %(const Vector<double> &v1, const Vector<double> &v2) {
122 return Vector<double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
125 inline Vector<long double> operator %(const Vector<long double> &v1, const Vector<long double> &v2) {
126 return Vector<long double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
129 inline Vector<T> operator %(const Vector<T> &v, T s) {
130 return Vector<T>(v.X() % s, v.Y() % s);
133 inline Vector<float> operator %(const Vector<float> &v, float s) {
134 return Vector<float>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
137 inline Vector<double> operator %(const Vector<double> &v, double s) {
138 return Vector<double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
141 inline Vector<long double> operator %(const Vector<long double> &v, long double s) {
142 return Vector<long double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
146 inline bool operator ==(const Vector<T> &lhs, const Vector<T> &rhs) {
147 return lhs.X() == rhs.X() && lhs.Y() == rhs.Y();
151 inline bool operator !=(const Vector<T> &lhs, const Vector<T> &rhs) {
152 return lhs.X() != rhs.X() || lhs.Y() != rhs.Y();
156 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
157 out << '<' << v.X() << ", " << v.Y() << '>';
162 template <class Scalar>
163 void Vector<Scalar>::Lock(const Vector<Scalar> &to) {
164 Vector<Scalar> half(to / Scalar(2));
165 Vector<Scalar> dist((*this) % to);
167 if (dist.X() > half.X()) {
168 x += (to.X() - dist.X());
173 if (dist.Y() > half.Y()) {
174 y += (to.Y() - dist.Y());
183 #endif /* GEOMETRY_VECTOR_H_ */