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 &X() { return x; }
35 Scalar &Y() { return y; }
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);
42 void Lock(const Vector<Scalar> &to);
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());
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());
60 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
61 return lhs = lhs + rhs;
63 template<class T, class U>
64 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
65 return lhs = lhs + rhs;
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());
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());
78 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
79 return lhs = lhs - rhs;
81 template<class T, class U>
82 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
83 return lhs = lhs - rhs;
87 inline Vector<T> operator -(const Vector<T> &v) {
88 return Vector<T>(-v.X(), -v.Y());
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());
96 inline Vector<T> operator *(const Vector<T> &v, T s) {
97 return Vector<T>(v.X() * s, v.Y() * s);
100 inline Vector<T> operator *(T s, const Vector<T> &v) {
101 return Vector<T>(s * v.X(), s * v.Y());
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());
109 inline Vector<T> operator /(const Vector<T> &v, T s) {
110 return Vector<T>(v.X() / s, v.Y() / s);
113 inline Vector<T> operator /(T s, const Vector<T> &v) {
114 return Vector<T>(s / v.X(), s / v.Y());
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());
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()));
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()));
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()));
134 inline Vector<T> operator %(const Vector<T> &v, T s) {
135 return Vector<T>(v.X() % s, v.Y() % s);
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));
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));
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));
151 inline bool operator ==(const Vector<T> &lhs, const Vector<T> &rhs) {
152 return lhs.X() == rhs.X() && lhs.Y() == rhs.Y();
156 inline bool operator !=(const Vector<T> &lhs, const Vector<T> &rhs) {
157 return lhs.X() != rhs.X() || lhs.Y() != rhs.Y();
161 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
162 out << '<' << v.X() << ", " << v.Y() << '>';
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);
172 if (dist.X() > half.X()) {
173 x += (to.X() - dist.X());
178 if (dist.Y() > half.Y()) {
179 y += (to.Y() - dist.Y());
188 #endif /* GEOMETRY_VECTOR_H_ */