#pragma once #include namespace mlx { template constexpr Vec2::Vec2(T X, T Y) : x(X), y(Y) {} template constexpr Vec2::Vec2(T scale) : x(scale), y(scale) {} template template constexpr Vec2::Vec2(const Vec2& vec) : x(static_cast(vec.x)), y(static_cast(vec.y)) {} template constexpr Vec2::Vec2(const Vec3& vec) : x(vec.x), y(vec.y) {} template constexpr Vec2::Vec2(const Vec4& vec) : x(vec.x), y(vec.y) {} template T Vec2::AbsDotProduct(const Vec2& vec) const { return std::abs(x * vec.x) + std::abs(y * vec.y); } template constexpr bool Vec2::ApproxEqual(const Vec2& vec, T maxDifference) const { return NumberEquals(x, vec.x, maxDifference) && NumberEquals(y, vec.y, maxDifference); } template template U Vec2::Distance(const Vec2& vec) const { return static_cast(std::sqrt(SquaredDistance(vec))); } template constexpr T Vec2::DotProduct(const Vec2& vec) const { return x * vec.x + y * vec.y; } template template T Vec2::GetLength() const { return static_cast(std::sqrt(static_cast(GetSquaredLength()))); } template Vec2 Vec2::GetNormal(T* length) const { Vec2 vec(*this); vec.Normalize(length); return vec; } template constexpr T Vec2::GetSquaredLength() const { return x * x + y * y; } template constexpr Vec2& Vec2::Maximize(const Vec2& vec) { if(vec.x > x) x = vec.x; if(vec.y > y) y = vec.y; return *this; } template constexpr Vec2& Vec2::Minimize(const Vec2& vec) { if(vec.x < x) x = vec.x; if(vec.y < y) y = vec.y; return *this; } template Vec2& Vec2::Normalize(T* length) { T norm = GetLength(); if(norm > T(0.0)) { T invNorm = T(1.0) / norm; x *= invNorm; y *= invNorm; } if(length) *length = norm; return *this; } template constexpr T Vec2::SquaredDistance(const Vec2& vec) const { return (*this - vec).GetSquaredLength(); } template std::string Vec2::ToString() const { return "Vec2(" + std::to_string(x) + ", " + std::to_string(y) + ')'; } template constexpr T& Vec2::operator[](std::size_t i) { mlx::Assert(i < 2, "index out of range"); return *(&x + i); } template constexpr T Vec2::operator[](std::size_t i) const { mlx::Assert(i < 2, "index out of range"); return *(&x + i); } template constexpr const Vec2& Vec2::operator+() const { return *this; } template constexpr Vec2 Vec2::operator-() const { return Vec2(-x, -y); } template constexpr Vec2 Vec2::operator+(const Vec2& vec) const { return Vec2(x + vec.x, y + vec.y); } template constexpr Vec2 Vec2::operator-(const Vec2& vec) const { return Vec2(x - vec.x, y - vec.y); } template constexpr Vec2 Vec2::operator*(const Vec2& vec) const { return Vec2(x * vec.x, y * vec.y); } template constexpr Vec2 Vec2::operator*(T scale) const { return Vec2(x * scale, y * scale); } template constexpr Vec2 Vec2::operator/(const Vec2& vec) const { return Vec2(x / vec.x, y / vec.y); } template constexpr Vec2 Vec2::operator/(T scale) const { return Vec2(x / scale, y / scale); } template constexpr Vec2 Vec2::operator%(const Vec2& vec) const { return Vec2(Mod(x, vec.x), Mod(y, vec.y)); } template constexpr Vec2 Vec2::operator%(T mod) const { return Vec2(Mod(x, mod), Mod(y, mod)); } template constexpr Vec2& Vec2::operator+=(const Vec2& vec) { x += vec.x; y += vec.y; return *this; } template constexpr Vec2& Vec2::operator-=(const Vec2& vec) { x -= vec.x; y -= vec.y; return *this; } template constexpr Vec2& Vec2::operator*=(const Vec2& vec) { x *= vec.x; y *= vec.y; return *this; } template constexpr Vec2& Vec2::operator*=(T scale) { x *= scale; y *= scale; return *this; } template constexpr Vec2& Vec2::operator/=(const Vec2& vec) { x /= vec.x; y /= vec.y; return *this; } template constexpr Vec2& Vec2::operator/=(T scale) { x /= scale; y /= scale; return *this; } template constexpr Vec2& Vec2::operator%=(const Vec2& vec) { x = Mod(x, vec.x); y = Mod(y, vec.y); return *this; } template constexpr Vec2& Vec2::operator%=(T value) { x = Mod(x, value); y = Mod(y, value); return *this; } template constexpr bool Vec2::operator==(const Vec2& vec) const { return x == vec.x && y == vec.y; } template constexpr bool Vec2::operator!=(const Vec2& vec) const { return !operator==(vec); } template constexpr bool Vec2::operator<(const Vec2& vec) const { if (x != vec.x) return x < vec.x; return y < vec.y; } template constexpr bool Vec2::operator<=(const Vec2& vec) const { if (x != vec.x) return x < vec.x; return y <= vec.y; } template constexpr bool Vec2::operator>(const Vec2& vec) const { if (x != vec.x) return x > vec.x; return y > vec.y; } template constexpr bool Vec2::operator>=(const Vec2& vec) const { if (x != vec.x) return x > vec.x; return y >= vec.y; } template constexpr Vec2 Vec2::Apply(T(*func)(T), const Vec2& vec) { return Vec2(func(vec.x), func(vec.y)); } template constexpr bool Vec2::ApproxEqual(const Vec2& lhs, const Vec2& rhs, T maxDifference) { return lhs.ApproxEqual(rhs, maxDifference); } template template U Vec2::Distance(const Vec2& vec1, const Vec2& vec2) { return vec1.Distance(vec2); } template constexpr T Vec2::DotProduct(const Vec2& vec1, const Vec2& vec2) { return vec1.DotProduct(vec2); } template Vec2 Vec2::Normalize(const Vec2& vec) { return vec.GetNormal(); } template constexpr Vec2 Vec2::Unit() { return Vec2(1, 1); } template constexpr Vec2 Vec2::UnitX() { return Vec2(1, 0); } template constexpr Vec2 Vec2::UnitY() { return Vec2(0, 1); } template constexpr Vec2 Vec2::Zero() { return Vec2(0, 0); } template std::ostream& operator<<(std::ostream& out, const Vec2& vec) { return out << "Vec2(" << vec.x << ", " << vec.y << ')'; } template constexpr Vec2 operator*(T scale, const Vec2& vec) { return Vec2(scale * vec.x, scale * vec.y); } template constexpr Vec2 operator/(T scale, const Vec2& vec) { return Vec2(scale / vec.x, scale / vec.y); } template constexpr Vec2 operator%(T mod, const Vec2& vec) { return Vec2(Mod(mod, vec.x), Mod(mod, vec.y)); } }