X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=src%2Fgeometry%2FVector.h;fp=src%2Fgeometry%2FVector.h;h=0000000000000000000000000000000000000000;hb=a3ba4dc677ad7c92eeb78b20b642241563605c9d;hp=8822b1be5be769c1a792e6b93398fcd10d9e26b1;hpb=ef2496b3cb7ce66b7f831278be66261834b732e5;p=l2e.git diff --git a/src/geometry/Vector.h b/src/geometry/Vector.h deleted file mode 100644 index 8822b1b..0000000 --- a/src/geometry/Vector.h +++ /dev/null @@ -1,181 +0,0 @@ -#ifndef GEOMETRY_VECTOR_H_ -#define GEOMETRY_VECTOR_H_ - -#include -#include -#include - -namespace geometry { - -/// Basic vector class with emphasis on graphical/computational ease of use -/// rather than mathematical accuracy ;) . -template -class Vector { - -public: - Vector() : x(0), y(0) { } - Vector(Scalar x, Scalar y) : x(x), y(y) { } - template - Vector(const Vector &other) : x(other.X()), y(other.Y()) { }; - template - 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 FromIndex(Scalar index, Scalar lineLength) { - return Vector(index % lineLength, index / lineLength); - } - - void Lock(const Vector &to); - -private: - Scalar x, y; - -}; - - -template -inline Vector operator +(const Vector &lhs, const Vector &rhs) { - return Vector(lhs.X() + rhs.X(), lhs.Y() + rhs.Y()); -} -template -inline Vector operator +(const Vector &lhs, const Vector &rhs) { - return Vector(lhs.X() + rhs.X(), lhs.Y() + rhs.Y()); -} - -template -inline Vector &operator +=(Vector &lhs, const Vector &rhs) { - return lhs = lhs + rhs; -} -template -inline Vector &operator +=(Vector &lhs, const Vector &rhs) { - return lhs = lhs + rhs; -} - -template -inline Vector operator -(const Vector &lhs, const Vector &rhs) { - return Vector(lhs.X() - rhs.X(), lhs.Y() - rhs.Y()); -} -template -inline Vector operator -(const Vector &lhs, const Vector &rhs) { - return Vector(lhs.X() - rhs.X(), lhs.Y() - rhs.Y()); -} - -template -inline Vector &operator -=(Vector &lhs, const Vector &rhs) { - return lhs = lhs - rhs; -} -template -inline Vector &operator -=(Vector &lhs, const Vector &rhs) { - return lhs = lhs - rhs; -} - -template -inline Vector operator -(const Vector &v) { - return Vector(-v.X(), -v.Y()); -} - -template -inline Vector operator *(const Vector &v1, const Vector &v2) { - return Vector(v1.X() * v2.X(), v1.Y() * v2.Y()); -} -template -inline Vector operator *(const Vector &v, T s) { - return Vector(v.X() * s, v.Y() * s); -} -template -inline Vector operator *(T s, const Vector &v) { - return Vector(s * v.X(), s * v.Y()); -} - -template -inline Vector operator /(const Vector &v1, const Vector &v2) { - return Vector(v1.X() / v2.X(), v1.Y() / v2.Y()); -} -template -inline Vector operator /(const Vector &v, T s) { - return Vector(v.X() / s, v.Y() / s); -} -template -inline Vector operator /(T s, const Vector &v) { - return Vector(s / v.X(), s / v.Y()); -} - -template -inline Vector operator %(const Vector &v1, const Vector &v2) { - return Vector(v1.X() % v2.X(), v1.Y() % v2.Y()); -} -template<> -inline Vector operator %(const Vector &v1, const Vector &v2) { - return Vector(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y())); -} -template<> -inline Vector operator %(const Vector &v1, const Vector &v2) { - return Vector(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y())); -} -template<> -inline Vector operator %(const Vector &v1, const Vector &v2) { - return Vector(std::fmod(v1.X(), v2.X()), std::fmod(v1.Y(), v2.Y())); -} -template -inline Vector operator %(const Vector &v, T s) { - return Vector(v.X() % s, v.Y() % s); -} -template<> -inline Vector operator %(const Vector &v, float s) { - return Vector(std::fmod(v.X(), s), std::fmod(v.Y(), s)); -} -template<> -inline Vector operator %(const Vector &v, double s) { - return Vector(std::fmod(v.X(), s), std::fmod(v.Y(), s)); -} -template<> -inline Vector operator %(const Vector &v, long double s) { - return Vector(std::fmod(v.X(), s), std::fmod(v.Y(), s)); -} - -template -inline bool operator ==(const Vector &lhs, const Vector &rhs) { - return lhs.X() == rhs.X() && lhs.Y() == rhs.Y(); -} - -template -inline bool operator !=(const Vector &lhs, const Vector &rhs) { - return lhs.X() != rhs.X() || lhs.Y() != rhs.Y(); -} - -template -inline std::ostream &operator <<(std::ostream &out, const Vector &v) { - out << '<' << v.X() << ", " << v.Y() << '>'; - return out; -} - - -template -void Vector::Lock(const Vector &to) { - Vector half(to / Scalar(2)); - Vector 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_ */