X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2FVector.h;h=8822b1be5be769c1a792e6b93398fcd10d9e26b1;hb=ef2496b3cb7ce66b7f831278be66261834b732e5;hp=7c698ff4af0a11d9b707cd0b51503142e016f475;hpb=64c0880d31cddc599b30331f9485d65eeaf705ee;p=l2e.git diff --git a/src/geometry/Vector.h b/src/geometry/Vector.h index 7c698ff..8822b1b 100644 --- a/src/geometry/Vector.h +++ b/src/geometry/Vector.h @@ -1,17 +1,14 @@ -/* - * Vector.h - * - * Created on: Aug 6, 2012 - * Author: holy - */ - #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 { @@ -27,6 +24,16 @@ 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; @@ -74,6 +81,10 @@ 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); @@ -83,10 +94,51 @@ 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) { @@ -104,6 +156,26 @@ inline std::ostream &operator <<(std::ostream &out, const Vector &v) { 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_ */