]> git.localhorst.tv Git - l2e.git/blobdiff - src/math/Vector.h
renamed namespace geometry -> math
[l2e.git] / src / math / Vector.h
diff --git a/src/math/Vector.h b/src/math/Vector.h
new file mode 100644 (file)
index 0000000..fb07d33
--- /dev/null
@@ -0,0 +1,181 @@
+#ifndef MATH_VECTOR_H_
+#define MATH_VECTOR_H_
+
+#include <cmath>
+#include <limits>
+#include <ostream>
+
+namespace math {
+
+/// Basic vector class with emphasis on graphical/computational ease of use
+/// rather than mathematical accuracy ;) .
+template<class Scalar>
+class Vector {
+
+public:
+       Vector() : x(0), y(0) { }
+       Vector(Scalar x, Scalar y) : x(x), y(y) { }
+       template<class T>
+       Vector(const Vector<T> &other) : x(other.X()), y(other.Y()) { };
+       template<class T>
+       Vector(T x, T y) : x(x), y(y) { }
+
+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_ */