X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmath%2FVector.h;fp=src%2Fmath%2FVector.h;h=fb07d338986fa2f4b9c0373bc808b911efd18ec0;hb=a3ba4dc677ad7c92eeb78b20b642241563605c9d;hp=0000000000000000000000000000000000000000;hpb=ef2496b3cb7ce66b7f831278be66261834b732e5;p=l2e.git diff --git a/src/math/Vector.h b/src/math/Vector.h new file mode 100644 index 0000000..fb07d33 --- /dev/null +++ b/src/math/Vector.h @@ -0,0 +1,181 @@ +#ifndef MATH_VECTOR_H_ +#define MATH_VECTOR_H_ + +#include +#include +#include + +namespace math { + +/// 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_ */