4 * Created on: Aug 6, 2012
8 #ifndef GEOMETRY_VECTOR_H_
9 #define GEOMETRY_VECTOR_H_
17 /// Basic vector class with emphasis on graphical/computational ease of use
18 /// rather than mathematical accuracy ;) .
19 template<class Scalar>
23 Vector() : x(0), y(0) { }
24 Vector(Scalar x, Scalar y) : x(x), y(y) { }
26 Vector(const Vector<T> &other) : x(other.X()), y(other.Y()) { };
28 Vector(T x, T y) : x(x), y(y) { }
31 Scalar X() const { return x; }
32 Scalar Y() const { return y; }
34 Scalar Index(Scalar lineLength) const { return Y() * lineLength + X(); }
35 static Vector<Scalar> FromIndex(Scalar index, Scalar lineLength) {
36 return Vector<Scalar>(index % lineLength, index / lineLength);
39 void Lock(const Vector<Scalar> &to);
48 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<T> &rhs) {
49 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
51 template<class T, class U>
52 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<U> &rhs) {
53 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
57 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
58 return lhs = lhs + rhs;
60 template<class T, class U>
61 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
62 return lhs = lhs + rhs;
66 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
67 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
69 template<class T, class U>
70 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<U> &rhs) {
71 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
75 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
76 return lhs = lhs - rhs;
78 template<class T, class U>
79 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
80 return lhs = lhs - rhs;
84 inline Vector<T> operator -(const Vector<T> &v) {
85 return Vector<T>(-v.X(), -v.Y());
89 inline Vector<T> operator *(const Vector<T> &v1, const Vector<T> &v2) {
90 return Vector<T>(v1.X() * v2.X(), v1.Y() * v2.Y());
93 inline Vector<T> operator *(const Vector<T> &v, T s) {
94 return Vector<T>(v.X() * s, v.Y() * s);
97 inline Vector<T> operator *(T s, const Vector<T> &v) {
98 return Vector<T>(s * v.X(), s * v.Y());
102 inline Vector<T> operator /(const Vector<T> &v1, const Vector<T> &v2) {
103 return Vector<T>(v1.X() / v2.X(), v1.Y() / v2.Y());
106 inline Vector<T> operator /(const Vector<T> &v, T s) {
107 return Vector<T>(v.X() / s, v.Y() / s);
110 inline Vector<T> operator /(T s, const Vector<T> &v) {
111 return Vector<T>(s / v.X(), s / v.Y());
115 inline Vector<T> operator %(const Vector<T> &v1, const Vector<T> &v2) {
116 return Vector<T>(v1.X() % v2.X(), v1.Y() % v2.Y());
119 inline Vector<float> operator %(const Vector<float> &v1, const Vector<float> &v2) {
120 return Vector<float>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
123 inline Vector<double> operator %(const Vector<double> &v1, const Vector<double> &v2) {
124 return Vector<double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
127 inline Vector<long double> operator %(const Vector<long double> &v1, const Vector<long double> &v2) {
128 return Vector<long double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
131 inline Vector<T> operator %(const Vector<T> &v, T s) {
132 return Vector<T>(v.X() % s, v.Y() % s);
135 inline Vector<float> operator %(const Vector<float> &v, float s) {
136 return Vector<float>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
139 inline Vector<double> operator %(const Vector<double> &v, double s) {
140 return Vector<double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
143 inline Vector<long double> operator %(const Vector<long double> &v, long double s) {
144 return Vector<long double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
148 inline bool operator ==(const Vector<T> &lhs, const Vector<T> &rhs) {
149 return lhs.X() == rhs.X() && lhs.Y() == rhs.Y();
153 inline bool operator !=(const Vector<T> &lhs, const Vector<T> &rhs) {
154 return lhs.X() != rhs.X() || lhs.Y() != rhs.Y();
158 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
159 out << '<' << v.X() << ", " << v.Y() << '>';
164 template <class Scalar>
165 void Vector<Scalar>::Lock(const Vector<Scalar> &to) {
166 Vector<Scalar> half(to / Scalar(2));
167 Vector<Scalar> dist((*this) % to);
169 if (dist.X() > half.X()) {
170 x += (to.X() - dist.X());
175 if (dist.Y() > half.Y()) {
176 y += (to.Y() - dist.Y());
185 #endif /* GEOMETRY_VECTOR_H_ */