/* * ===================================================================================== * * Filename: Vector2.hpp * * Description: * * Created: 12/05/2015 18:45:17 * * Author: Quentin Bazin, * * ===================================================================================== */ #ifndef VECTOR2_HPP_ #define VECTOR2_HPP_ #include #include #include "Types.hpp" template class Vector2 { public: Vector2() = default; Vector2(T _x, T _y) : x(_x), y(_y) {} template Vector2(const Vector2 &vector2) : x(vector2.x), y(vector2.y) {} Vector2 operator+(const Vector2 &vector2) const { return Vector2{x + vector2.x, y + vector2.y}; } Vector2 operator-(const Vector2 &vector2) const { return Vector2{x - vector2.x, y - vector2.y}; } Vector2 operator*(T n) const { return Vector2{x * n, y * n}; } Vector2 operator/(T n) const { if(n != 0) { return Vector2{x / n, y / n}; } else { throw std::overflow_error("Divide by zero exception"); } } Vector2& operator=(T n) { x = n; y = n; return *this; } Vector2 &operator+=(const Vector2 &vector2) { *this = operator+(vector2); return *this; } Vector2 &operator-=(const Vector2 &vector2) { *this = operator-(vector2); return *this; } Vector2 &operator*=(T n) { *this = operator*(n); return *this; } Vector2 &operator/=(T n) { *this = operator/(n); return *this; } bool operator==(const Vector2 &vector2) const { return x == vector2.x && y == vector2.y; } bool operator!=(const Vector2 &vector2) const { return !operator==(vector2); } // Needed if Vector2 is used as a key in a std::map bool operator<(const Vector2 &vector2) const { return x < vector2.x && y <= vector2.y; } bool operator>(const Vector2 &vector2) const { return x > vector2.x && y >= vector2.y; } T x; T y; }; template Vector2 operator*(T n, Vector2 &vector2) { return vector2.operator*(n); } using Vector2i = Vector2; using Vector2u = Vector2; using Vector2f = Vector2; using Vector2d = Vector2; #endif // VECTOR2_HPP_