X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgeometry%2FVector.h;h=8822b1be5be769c1a792e6b93398fcd10d9e26b1;hb=cc3d698b8c1ad09d7a3f9e3f28bc84e0ac1735ea;hp=bf647136ddfaa101ee8406b0f8fa36cdccf2a4b4;hpb=0542849dfccfec1ce1477265fa0fee2401a8fb23;p=l2e.git diff --git a/src/geometry/Vector.h b/src/geometry/Vector.h index bf64713..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; @@ -37,27 +44,47 @@ 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); @@ -67,10 +94,61 @@ 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) { @@ -78,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_ */