#include #include #include "Vector2.h" template Vector2::Vector2() { x = T(0); y = T(0); } template Vector2::Vector2(T x, T y) { v[0] = x; v[1] = y; } template Vector2::Vector2(T s) { x = s; y = s; } template template inline Vector2::Vector2(const Vector2& vec) { x = T(vec.x); y = T(vec.y); } template inline float Vector2::length() const { return std::sqrt((x * x) + (y * y)); } template inline Vector2& Vector2::normalize() { float il = 1.0f / length(); x *= il; y *= il; return *this; } template inline T Vector2::dot(const Vector2& vec) const { return x * vec.x + y * vec.y; } template inline Vector2& Vector2::reflect(const Vector2& n) { *this = T(2) * n * dot(n) - *this; return *this; } template std::string Vector2::toString() const { return core::to_string(x) + ", " + core::to_string(y); } // --------- // Compare // --------- template inline bool operator==(const Vector2& a, const Vector2& b) { return a.x == b.x && a.y == b.y; } template inline bool operator!=(const Vector2& a, const Vector2& b) { return !(a == b); } template inline bool operator<(const Vector2& a, const Vector2& b) { return a.x < b.x && a.y < b.y; } template inline bool operator<=(const Vector2& a, const Vector2& b) { return a.x =< b.x && a.y <= b.y; } template inline bool operator>(const Vector2& a, const Vector2& b) { return a.x > b.x && a.y > b.y; } template inline bool operator>=(const Vector2& a, const Vector2& b) { return a.x >= b.x && a.y >= b.y } // ------------------ // Unary arithmetic // ------------------ template inline Vector2 operator+(const Vector2& v) { return v; } template inline Vector2 operator-(const Vector2& v) { return Vector2(-v.x, -v.y); } // ------------ // Arithmetic // ------------ template inline Vector2 operator+(const Vector2& a, const Vector2& b) { return Vector2(a.x + b.x, a.y + b.y); } template inline Vector2 operator-(const Vector2& a, const Vector2& b) { return Vector2(a.x - b.x, a.y - b.y); } template inline Vector2 operator/(const Vector2& a, const Vector2& b) { return Vector2(a.x / b.x, a.y / b.y); } template inline Vector2 operator*(const Vector2& a, const Vector2& b) { return Vector2(a.x * b.x, a.y * b.y); } template inline Vector2& operator+=(Vector2& a, const Vector2& b) { a.x += b.x; a.y += b.y; return a; } template inline Vector2& operator-=(Vector2& a, const Vector2& b) { a.x -= b.x; a.y -= b.y; return a; } template inline Vector2& operator/=(Vector2& a, const Vector2& b) { a.x /= b.x; a.y /= b.y; return a; }; template inline Vector2& operator*=(Vector2& a, const Vector2& b) { a.x *= b.x; a.y *= b.y; return a; } // ------------ // Scalar Arithmetic // ------------ template inline Vector2 operator+(const Vector2& v, T s) { return Vector2(v.x + s, v.y + s); } template inline Vector2 operator+(T s, const Vector2& v) { return v + s; } template inline Vector2 operator-(const Vector2& v, T s) { return Vector2(v.x - s, v.y - s); } template inline Vector2 operator-(T s, const Vector2& v) { return Vector2(s - v.x, s - v.y); } template inline Vector2 operator/(const Vector2& v, T s) { return Vector2(v.x / s, v.y / s); } template inline Vector2 operator/(T s, const Vector2& v) { return Vector2(s / v.x, s / v.y); } template inline Vector2 operator*(const Vector2& v, T s) { return Vector2(v.x * s, v.y * s); } template inline Vector2 operator*(T s, const Vector2& v) { return v * s; } template inline Vector2& operator+=(Vector2& v, T s) { v.x += s; v.y += s; return v; } template inline Vector2& operator-=(Vector2& v,T s) { v.x -= s; v.y -= s; return v; } template inline Vector2& operator/=(Vector2& v, T s) { v.x /= s; v.y /= s; return v; } template inline Vector2& operator*=(Vector2& v, T s) { v.x *= s; v.y *= s; return v; } // ---------------- // Scalar compare // ---------------- template inline bool operator<(const Vector2& v, T s) { return v.x < s && v.y < s; } template inline bool operator<(T s, const Vector2& v) { return s < v.x && s < v.y; } template inline bool operator<=(const Vector2& v, T s) { return v.x <= s && v.y <= s; } template inline bool operator<=(T s, const Vector2& v) { return s <= v.x && s <= v.y; } template inline bool operator>(const Vector2& v, T s) { return x > s && y > s; } template inline bool operator>(T s, const Vector2& v) { return s < v.x && s < v.y; } template inline bool operator>=(const Vector2& v, T s) { return v.x >= s && v.y >= s; } template inline bool operator>=(T s, const Vector2& v) { return s >= v.x && s >= v.y; } // ------------------- // Stream operations // ------------------- template inline std::ostream& operator<<(std::ostream &s, const Vector2& v) { return s << "(" << v.x << "," << v.y << ")"; }