// DR. ROBOTNIK'S RING RACERS //----------------------------------------------------------------------------- // Copyright (C) 2025 by James Robert Roman // Copyright (C) 2025 by Kart Krew // // This program is free software distributed under the // terms of the GNU General Public License, version 2. // See the 'LICENSE' file for more details. //----------------------------------------------------------------------------- #ifndef math_vec_hpp #define math_vec_hpp #include #include "traits.hpp" namespace srb2::math { template struct Vec2 { T x, y; constexpr Vec2() : x{}, y{} {} constexpr Vec2(T x_, T y_) : x(x_), y(y_) {} constexpr Vec2(T z) : x(z), y(z) {} template Vec2(const Vec2& b) : Vec2(b.x, b.y) {} T magnitude() const { return Traits::hypot(x, y); } Vec2 normal() const { return {-y, x}; } #define X(op) \ Vec2& operator op##=(const Vec2& b) \ { \ x op##= b.x; \ y op##= b.y; \ return *this; \ } \ Vec2 operator op(const Vec2& b) const { return Vec2(x op b.x, y op b.y); } \ X(+) X(-) X(*) X(/) #undef X Vec2 operator-() const { return Vec2(-x, -y); } }; template struct is_vec2 : std::false_type {}; template struct is_vec2> : std::true_type {}; template inline constexpr bool is_vec2_v = is_vec2::value; #define X(op) \ template , bool> = true> \ Vec2 operator op(const T& a, const Vec2& b) \ { \ return Vec2 {a} op Vec2 {b}; \ } \ template , bool> = true> \ Vec2 operator op(const Vec2& a, const U& b) \ { \ return Vec2 {a} op Vec2 {b}; \ } \ X(+) X(-) X(*) X(/) #undef X }; // namespace srb2::math #endif/*math_vec_hpp*/