X-Git-Url: http://git.localhorst.tv/?p=space.git;a=blobdiff_plain;f=src%2Fgraphics%2FVector.h;fp=src%2Fgraphics%2FVector.h;h=992ba1eef6a8bbdc323b8f612eef14eb2471f7d7;hp=34d829713e5dea735d339b0ed56cafdb3a409d11;hb=699437a474de8b87ccb6749d44adf740e680d620;hpb=1129b8ac89f1e614f69793227ccec90157708aea diff --git a/src/graphics/Vector.h b/src/graphics/Vector.h index 34d8297..992ba1e 100644 --- a/src/graphics/Vector.h +++ b/src/graphics/Vector.h @@ -2,6 +2,7 @@ #define SPACE_VECTOR_H_ #include +#include #include #include @@ -18,6 +19,12 @@ public: template constexpr Vector(Vector other) : x(other.x), y(other.y) { } + static Vector FromPolar(Scalar rad, Scalar az) { + return Vector(rad * std::cos(az), rad * std::sin(az)); + } + + static constexpr Vector unit45 = Vector(-0.7071, 0.7071); + public: Vector &operator +=(Vector other) { x += other.x; @@ -43,6 +50,9 @@ public: }; +template +constexpr Vector Vector::unit45; + /// specialization with same layout as SDL_Point template<> class Vector @@ -132,6 +142,29 @@ constexpr bool operator !=(Vector lhs, Vector rhs) { } +template +constexpr Scalar Dot(Vector lhs, Vector rhs) { + return (lhs.x * rhs.x) + (lhs.y * rhs.y); +} +template +constexpr Scalar Length(Vector v) { + return std::sqrt(Dot(v, v)); +} + +template +constexpr Vector Rotate90(Vector v) { + return Vector(-v.y, v.x); +} +template +constexpr Vector Rotate180(Vector v) { + return -v; +} +template +constexpr Vector Rotate270(Vector v) { + return Vector(v.y, -v.x); +} + + template inline std::ostream &operator <<(std::ostream &out, Vector v) { return out << '<' << v.x << ',' << v.y << '>';