/// @file core/vec2.h /// @brief Two-dimensional fixed-point vector for the simulation. #pragma once #include "core/fixed.h" namespace kumo { /// @brief A 2D position, offset, or velocity in 16.16 fixed point. struct Vec2 { Fixed x; Fixed y; /// @brief The zero vector (0, 0). static constexpr Vec2 zero() { return {}; } /// @brief A vector from whole integer coordinates. static constexpr Vec2 from_ints(int x, int y) { return {Fixed::from_int(x), Fixed::from_int(y)}; } constexpr Vec2 operator-() const { return {-x, -y}; } constexpr Vec2 operator+(Vec2 other) const { return {x + other.x, y + other.y}; } constexpr Vec2 operator-(Vec2 other) const { return {x - other.x, y - other.y}; } constexpr Vec2 operator*(Fixed scalar) const { return {x * scalar, y * scalar}; } constexpr Vec2& operator+=(Vec2 other) { x += other.x; y += other.y; return *this; } constexpr Vec2& operator-=(Vec2 other) { x -= other.x; y -= other.y; return *this; } constexpr Vec2& operator*=(Fixed scalar) { x = x * scalar; y = y * scalar; return *this; } constexpr bool operator==(const Vec2&) const = default; }; constexpr Vec2 operator*(Fixed scalar, Vec2 vector) { return vector * scalar; } /// @brief Component-wise linear interpolation from @p a to @p b at @p t. constexpr Vec2 lerp(Vec2 a, Vec2 b, Fixed t) { return {lerp(a.x, b.x, t), lerp(a.y, b.y, t)}; } } // namespace kumo