1 #ifndef GEOMETRY_VECTOR_H_
2 #define GEOMETRY_VECTOR_H_
10 /// Basic vector class with emphasis on graphical/computational ease of use
11 /// rather than mathematical accuracy ;) .
12 template<class Scalar>
16 Vector() : x(0), y(0) { }
17 Vector(Scalar x, Scalar y) : x(x), y(y) { }
19 Vector(const Vector<T> &other) : x(other.X()), y(other.Y()) { };
21 Vector(T x, T y) : x(x), y(y) { }
24 Scalar X() const { return x; }
25 Scalar Y() const { return y; }
27 Scalar &X() { return x; }
28 Scalar &Y() { return y; }
30 Scalar Index(Scalar lineLength) const { return Y() * lineLength + X(); }
31 static Vector<Scalar> FromIndex(Scalar index, Scalar lineLength) {
32 return Vector<Scalar>(index % lineLength, index / lineLength);
35 void Lock(const Vector<Scalar> &to);
44 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<T> &rhs) {
45 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
47 template<class T, class U>
48 inline Vector<T> operator +(const Vector<T> &lhs, const Vector<U> &rhs) {
49 return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
53 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
54 return lhs = lhs + rhs;
56 template<class T, class U>
57 inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
58 return lhs = lhs + rhs;
62 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
63 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
65 template<class T, class U>
66 inline Vector<T> operator -(const Vector<T> &lhs, const Vector<U> &rhs) {
67 return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
71 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
72 return lhs = lhs - rhs;
74 template<class T, class U>
75 inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
76 return lhs = lhs - rhs;
80 inline Vector<T> operator -(const Vector<T> &v) {
81 return Vector<T>(-v.X(), -v.Y());
85 inline Vector<T> operator *(const Vector<T> &v1, const Vector<T> &v2) {
86 return Vector<T>(v1.X() * v2.X(), v1.Y() * v2.Y());
89 inline Vector<T> operator *(const Vector<T> &v, T s) {
90 return Vector<T>(v.X() * s, v.Y() * s);
93 inline Vector<T> operator *(T s, const Vector<T> &v) {
94 return Vector<T>(s * v.X(), s * v.Y());
98 inline Vector<T> operator /(const Vector<T> &v1, const Vector<T> &v2) {
99 return Vector<T>(v1.X() / v2.X(), v1.Y() / v2.Y());
102 inline Vector<T> operator /(const Vector<T> &v, T s) {
103 return Vector<T>(v.X() / s, v.Y() / s);
106 inline Vector<T> operator /(T s, const Vector<T> &v) {
107 return Vector<T>(s / v.X(), s / v.Y());
111 inline Vector<T> operator %(const Vector<T> &v1, const Vector<T> &v2) {
112 return Vector<T>(v1.X() % v2.X(), v1.Y() % v2.Y());
115 inline Vector<float> operator %(const Vector<float> &v1, const Vector<float> &v2) {
116 return Vector<float>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
119 inline Vector<double> operator %(const Vector<double> &v1, const Vector<double> &v2) {
120 return Vector<double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
123 inline Vector<long double> operator %(const Vector<long double> &v1, const Vector<long double> &v2) {
124 return Vector<long double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
127 inline Vector<T> operator %(const Vector<T> &v, T s) {
128 return Vector<T>(v.X() % s, v.Y() % s);
131 inline Vector<float> operator %(const Vector<float> &v, float s) {
132 return Vector<float>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
135 inline Vector<double> operator %(const Vector<double> &v, double s) {
136 return Vector<double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
139 inline Vector<long double> operator %(const Vector<long double> &v, long double s) {
140 return Vector<long double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
144 inline bool operator ==(const Vector<T> &lhs, const Vector<T> &rhs) {
145 return lhs.X() == rhs.X() && lhs.Y() == rhs.Y();
149 inline bool operator !=(const Vector<T> &lhs, const Vector<T> &rhs) {
150 return lhs.X() != rhs.X() || lhs.Y() != rhs.Y();
154 inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
155 out << '<' << v.X() << ", " << v.Y() << '>';
160 template <class Scalar>
161 void Vector<Scalar>::Lock(const Vector<Scalar> &to) {
162 Vector<Scalar> half(to / Scalar(2));
163 Vector<Scalar> dist((*this) % to);
165 if (dist.X() > half.X()) {
166 x += (to.X() - dist.X());
171 if (dist.Y() > half.Y()) {
172 y += (to.Y() - dist.Y());
181 #endif /* GEOMETRY_VECTOR_H_ */