]> git.localhorst.tv Git - l2e.git/blobdiff - src/geometry/Vector.h
removed stupid file headers that eclipse put in
[l2e.git] / src / geometry / Vector.h
index d7d0f3d69139ecb84249d258535f7bd75972fa60..8822b1be5be769c1a792e6b93398fcd10d9e26b1 100644 (file)
@@ -1,15 +1,14 @@
-/*
- * Vector.h
- *
- *  Created on: Aug 6, 2012
- *      Author: holy
- */
-
 #ifndef GEOMETRY_VECTOR_H_
 #define GEOMETRY_VECTOR_H_
 
+#include <cmath>
+#include <limits>
+#include <ostream>
+
 namespace geometry {
 
+/// Basic vector class with emphasis on graphical/computational ease of use
+/// rather than mathematical accuracy ;) .
 template<class Scalar>
 class Vector {
 
@@ -25,11 +24,158 @@ public:
        Scalar X() const { return x; }
        Scalar Y() const { return y; }
 
+       Scalar &X() { return x; }
+       Scalar &Y() { return y; }
+
+       Scalar Index(Scalar lineLength) const { return Y() * lineLength + X(); }
+       static Vector<Scalar> FromIndex(Scalar index, Scalar lineLength) {
+               return Vector<Scalar>(index % lineLength, index / lineLength);
+       }
+
+       void Lock(const Vector<Scalar> &to);
+
 private:
        Scalar x, y;
 
 };
 
+
+template<class T>
+inline Vector<T> operator +(const Vector<T> &lhs, const Vector<T> &rhs) {
+       return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
+}
+template<class T, class U>
+inline Vector<T> operator +(const Vector<T> &lhs, const Vector<U> &rhs) {
+       return Vector<T>(lhs.X() + rhs.X(), lhs.Y() + rhs.Y());
+}
+
+template<class T>
+inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<T> &rhs) {
+       return lhs = lhs + rhs;
+}
+template<class T, class U>
+inline Vector<T> &operator +=(Vector<T> &lhs, const Vector<U> &rhs) {
+       return lhs = lhs + rhs;
+}
+
+template<class T>
+inline Vector<T> operator -(const Vector<T> &lhs, const Vector<T> &rhs) {
+       return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
+}
+template<class T, class U>
+inline Vector<T> operator -(const Vector<T> &lhs, const Vector<U> &rhs) {
+       return Vector<T>(lhs.X() - rhs.X(), lhs.Y() - rhs.Y());
+}
+
+template<class T>
+inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<T> &rhs) {
+       return lhs = lhs - rhs;
+}
+template<class T, class U>
+inline Vector<T> &operator -=(Vector<T> &lhs, const Vector<U> &rhs) {
+       return lhs = lhs - rhs;
+}
+
+template<class T>
+inline Vector<T> operator -(const Vector<T> &v) {
+       return Vector<T>(-v.X(), -v.Y());
+}
+
+template<class T>
+inline Vector<T> operator *(const Vector<T> &v1, const Vector<T> &v2) {
+       return Vector<T>(v1.X() * v2.X(), v1.Y() * v2.Y());
+}
+template<class T>
+inline Vector<T> operator *(const Vector<T> &v, T s) {
+       return Vector<T>(v.X() * s, v.Y() * s);
+}
+template<class T>
+inline Vector<T> operator *(T s, const Vector<T> &v) {
+       return Vector<T>(s * v.X(), s * v.Y());
+}
+
+template<class T>
+inline Vector<T> operator /(const Vector<T> &v1, const Vector<T> &v2) {
+       return Vector<T>(v1.X() / v2.X(), v1.Y() / v2.Y());
+}
+template<class T>
+inline Vector<T> operator /(const Vector<T> &v, T s) {
+       return Vector<T>(v.X() / s, v.Y() / s);
+}
+template<class T>
+inline Vector<T> operator /(T s, const Vector<T> &v) {
+       return Vector<T>(s / v.X(), s / v.Y());
+}
+
+template<class T>
+inline Vector<T> operator %(const Vector<T> &v1, const Vector<T> &v2) {
+       return Vector<T>(v1.X() % v2.X(), v1.Y() % v2.Y());
+}
+template<>
+inline Vector<float> operator %(const Vector<float> &v1, const Vector<float> &v2) {
+       return Vector<float>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
+}
+template<>
+inline Vector<double> operator %(const Vector<double> &v1, const Vector<double> &v2) {
+       return Vector<double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
+}
+template<>
+inline Vector<long double> operator %(const Vector<long double> &v1, const Vector<long double> &v2) {
+       return Vector<long double>(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y()));
+}
+template<class T>
+inline Vector<T> operator %(const Vector<T> &v, T s) {
+       return Vector<T>(v.X() % s, v.Y() % s);
+}
+template<>
+inline Vector<float> operator %(const Vector<float> &v, float s) {
+       return Vector<float>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
+}
+template<>
+inline Vector<double> operator %(const Vector<double> &v, double s) {
+       return Vector<double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
+}
+template<>
+inline Vector<long double> operator %(const Vector<long double> &v, long double s) {
+       return Vector<long double>(std::fmod(v.X(), s), std::fmod(v.Y(), s));
+}
+
+template<class T>
+inline bool operator ==(const Vector<T> &lhs, const Vector<T> &rhs) {
+       return lhs.X() == rhs.X() && lhs.Y() == rhs.Y();
+}
+
+template<class T>
+inline bool operator !=(const Vector<T> &lhs, const Vector<T> &rhs) {
+       return lhs.X() != rhs.X() || lhs.Y() != rhs.Y();
+}
+
+template<class T>
+inline std::ostream &operator <<(std::ostream &out, const Vector<T> &v) {
+       out << '<' << v.X() << ", " << v.Y() << '>';
+       return out;
+}
+
+
+template <class Scalar>
+void Vector<Scalar>::Lock(const Vector<Scalar> &to) {
+       Vector<Scalar> half(to / Scalar(2));
+       Vector<Scalar> dist((*this) % to);
+
+       if (dist.X() > half.X()) {
+               x += (to.X() - dist.X());
+       } else {
+               x -= dist.X();
+       }
+
+       if (dist.Y() > half.Y()) {
+               y += (to.Y() - dist.Y());
+       } else {
+               y -= dist.Y();
+       }
+}
+
+
 }
 
 #endif /* GEOMETRY_VECTOR_H_ */