]> git.localhorst.tv Git - l2e.git/blob - src/geometry/Vector.h
c0b22e51d9b93d154d6dcb3ca1234d8fb7c3ae00
[l2e.git] / src / geometry / Vector.h
1 /*
2  * Vector.h
3  *
4  *  Created on: Aug 6, 2012
5  *      Author: holy
6  */
7
8 #ifndef GEOMETRY_VECTOR_H_
9 #define GEOMETRY_VECTOR_H_
10
11 #include <cmath>
12 #include <limits>
13 #include <ostream>
14
15 namespace geometry {
16
17 /// Basic vector class with emphasis on graphical/computational ease of use
18 /// rather than mathematical accuracy ;) .
19 template<class Scalar>
20 class Vector {
21
22 public:
23         Vector() : x(0), y(0) { }
24         Vector(Scalar x, Scalar y) : x(x), y(y) { }
25         template<class T>
26         Vector(const Vector<T> &other) : x(other.X()), y(other.Y()) { };
27         template<class T>
28         Vector(T x, T y) : x(x), y(y) { }
29
30 public:
31         Scalar X() const { return x; }
32         Scalar Y() const { return y; }
33
34         Scalar &X() { return x; }
35         Scalar &Y() { return y; }
36
37         Scalar Index(Scalar lineLength) const { return Y() * lineLength + X(); }
38         static Vector<Scalar> FromIndex(Scalar index, Scalar lineLength) {
39                 return Vector<Scalar>(index % lineLength, index / lineLength);
40         }
41
42         void Lock(const Vector<Scalar> &to);
43
44 private:
45         Scalar x, y;
46
47 };
48
49
50 template<class T>
51 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<T> &rhs) {
52         return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
53 }
54 template<class T, class U>
55 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<U> &rhs) {
56         return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
57 }
58
59 template<class T>
60 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
61         return lhs = lhs + rhs;
62 }
63 template<class T, class U>
64 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
65         return lhs = lhs + rhs;
66 }
67
68 template<class T>
69 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
70         return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
71 }
72 template<class T, class U>
73 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<U> &rhs) {
74         return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
75 }
76
77 template<class T>
78 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
79         return lhs = lhs - rhs;
80 }
81 template<class T, class U>
82 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
83         return lhs = lhs - rhs;
84 }
85
86 template<class T>
87 inline Vector<T> operator -(const Vector<T> &v) {
88         return Vector<T>(-v.X(), -v.Y());
89 }
90
91 template<class T>
92 inline Vector<T> operator *(const Vector<T> &v1, const Vector<T> &v2) {
93         return Vector<T>(v1.X() * v2.X(), v1.Y() * v2.Y());
94 }
95 template<class T>
96 inline Vector<T> operator *(const Vector<T> &v, T s) {
97         return Vector<T>(v.X() * s, v.Y() * s);
98 }
99 template<class T>
100 inline Vector<T> operator *(T s, const Vector<T> &v) {
101         return Vector<T>(s * v.X(), s * v.Y());
102 }
103
104 template<class T>
105 inline Vector<T> operator /(const Vector<T> &v1, const Vector<T> &v2) {
106         return Vector<T>(v1.X() / v2.X(), v1.Y() / v2.Y());
107 }
108 template<class T>
109 inline Vector<T> operator /(const Vector<T> &v, T s) {
110         return Vector<T>(v.X() / s, v.Y() / s);
111 }
112 template<class T>
113 inline Vector<T> operator /(T s, const Vector<T> &v) {
114         return Vector<T>(s / v.X(), s / v.Y());
115 }
116
117 template<class T>
118 inline Vector<T> operator %(const Vector<T> &v1, const Vector<T> &v2) {
119         return Vector<T>(v1.X() % v2.X(), v1.Y() % v2.Y());
120 }
121 template<>
122 inline Vector<float> operator %(const Vector<float> &v1, const Vector<float> &v2) {
123         return Vector<float>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
124 }
125 template<>
126 inline Vector<double> operator %(const Vector<double> &v1, const Vector<double> &v2) {
127         return Vector<double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
128 }
129 template<>
130 inline Vector<long double> operator %(const Vector<long double> &v1, const Vector<long double> &v2) {
131         return Vector<long double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
132 }
133 template<class T>
134 inline Vector<T> operator %(const Vector<T> &v, T s) {
135         return Vector<T>(v.X() % s, v.Y() % s);
136 }
137 template<>
138 inline Vector<float> operator %(const Vector<float> &v, float s) {
139         return Vector<float>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
140 }
141 template<>
142 inline Vector<double> operator %(const Vector<double> &v, double s) {
143         return Vector<double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
144 }
145 template<>
146 inline Vector<long double> operator %(const Vector<long double> &v, long double s) {
147         return Vector<long double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
148 }
149
150 template<class T>
151 inline bool operator ==(const Vector<T> &lhs, const Vector<T> &rhs) {
152         return lhs.X() == rhs.X() && lhs.Y() == rhs.Y();
153 }
154
155 template<class T>
156 inline bool operator !=(const Vector<T> &lhs, const Vector<T> &rhs) {
157         return lhs.X() != rhs.X() || lhs.Y() != rhs.Y();
158 }
159
160 template<class T>
161 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
162         out << '<' << v.X() << ", " << v.Y() << '>';
163         return out;
164 }
165
166
167 template <class Scalar>
168 void Vector<Scalar>::Lock(const Vector<Scalar> &to) {
169         Vector<Scalar> half(to / Scalar(2));
170         Vector<Scalar> dist((*this) % to);
171
172         if (dist.X() > half.X()) {
173                 x += (to.X() - dist.X());
174         } else {
175                 x -= dist.X();
176         }
177
178         if (dist.Y() > half.Y()) {
179                 y += (to.Y() - dist.Y());
180         } else {
181                 y -= dist.Y();
182         }
183 }
184
185
186 }
187
188 #endif /* GEOMETRY_VECTOR_H_ */