2022-03-16 22:21:12 +01:00
|
|
|
#pragma once
|
|
|
|
|
2023-08-07 13:35:19 +02:00
|
|
|
#include <cmath>
|
2023-09-20 17:25:03 +02:00
|
|
|
#include <format>
|
2023-09-20 21:26:08 +02:00
|
|
|
#include "../macros.hpp"
|
2024-02-18 16:00:34 +01:00
|
|
|
#include <hyprlang.hpp>
|
2022-03-16 22:21:12 +01:00
|
|
|
|
|
|
|
class Vector2D {
|
2022-12-16 18:17:31 +01:00
|
|
|
public:
|
2022-03-16 22:21:12 +01:00
|
|
|
Vector2D(double, double);
|
|
|
|
Vector2D();
|
|
|
|
~Vector2D();
|
2024-02-18 16:00:34 +01:00
|
|
|
Vector2D(const Hyprlang::VEC2&);
|
2022-03-16 22:21:12 +01:00
|
|
|
|
|
|
|
double x = 0;
|
|
|
|
double y = 0;
|
|
|
|
|
|
|
|
// returns the scale
|
2022-12-16 18:17:31 +01:00
|
|
|
double normalize();
|
2022-03-16 22:21:12 +01:00
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
Vector2D operator+(const Vector2D& a) const {
|
2022-03-16 22:21:12 +01:00
|
|
|
return Vector2D(this->x + a.x, this->y + a.y);
|
|
|
|
}
|
2022-12-16 18:17:31 +01:00
|
|
|
Vector2D operator-(const Vector2D& a) const {
|
2022-03-16 22:21:12 +01:00
|
|
|
return Vector2D(this->x - a.x, this->y - a.y);
|
|
|
|
}
|
2023-11-04 18:03:05 +01:00
|
|
|
Vector2D operator-() const {
|
|
|
|
return Vector2D(-this->x, -this->y);
|
|
|
|
}
|
2023-11-18 22:37:16 +01:00
|
|
|
Vector2D operator*(const double& a) const {
|
2022-03-16 22:21:12 +01:00
|
|
|
return Vector2D(this->x * a, this->y * a);
|
|
|
|
}
|
2023-11-18 22:37:16 +01:00
|
|
|
Vector2D operator/(const double& a) const {
|
2022-03-16 22:21:12 +01:00
|
|
|
return Vector2D(this->x / a, this->y / a);
|
|
|
|
}
|
2022-03-17 15:53:45 +01:00
|
|
|
|
2022-04-23 14:40:51 +02:00
|
|
|
bool operator==(const Vector2D& a) const {
|
2022-03-17 15:53:45 +01:00
|
|
|
return a.x == x && a.y == y;
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:40:51 +02:00
|
|
|
bool operator!=(const Vector2D& a) const {
|
2022-03-17 15:53:45 +01:00
|
|
|
return a.x != x || a.y != y;
|
|
|
|
}
|
|
|
|
|
2022-08-28 10:14:43 +02:00
|
|
|
Vector2D operator*(const Vector2D& a) const {
|
|
|
|
return Vector2D(this->x * a.x, this->y * a.y);
|
|
|
|
}
|
|
|
|
|
2023-01-29 16:58:36 +01:00
|
|
|
Vector2D operator/(const Vector2D& a) const {
|
|
|
|
return Vector2D(this->x / a.x, this->y / a.y);
|
|
|
|
}
|
|
|
|
|
2023-08-14 14:22:06 +02:00
|
|
|
bool operator>(const Vector2D& a) const {
|
|
|
|
return this->x > a.x && this->y > a.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Vector2D& a) const {
|
|
|
|
return this->x < a.x && this->y < a.y;
|
|
|
|
}
|
2023-11-18 22:37:16 +01:00
|
|
|
Vector2D& operator+=(const Vector2D& a) {
|
|
|
|
this->x += a.x;
|
|
|
|
this->y += a.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Vector2D& operator-=(const Vector2D& a) {
|
|
|
|
this->x -= a.x;
|
|
|
|
this->y -= a.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Vector2D& operator*=(const Vector2D& a) {
|
|
|
|
this->x *= a.x;
|
|
|
|
this->y *= a.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Vector2D& operator/=(const Vector2D& a) {
|
|
|
|
this->x /= a.x;
|
|
|
|
this->y /= a.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Vector2D& operator*=(const double& a) {
|
|
|
|
this->x *= a;
|
|
|
|
this->y *= a;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Vector2D& operator/=(const double& a) {
|
|
|
|
this->x /= a;
|
|
|
|
this->y /= a;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-08-14 14:22:06 +02:00
|
|
|
|
2023-06-23 21:14:04 +02:00
|
|
|
double distance(const Vector2D& other) const;
|
2023-11-05 17:18:41 +01:00
|
|
|
double size() const;
|
2024-02-05 23:37:24 +01:00
|
|
|
Vector2D clamp(const Vector2D& min, const Vector2D& max = Vector2D{-1, -1}) const;
|
2023-06-23 21:14:04 +02:00
|
|
|
|
|
|
|
Vector2D floor() const;
|
2023-11-11 15:37:17 +01:00
|
|
|
Vector2D round() const;
|
2024-02-04 16:40:20 +01:00
|
|
|
|
|
|
|
Vector2D getComponentMax(const Vector2D& other) const;
|
2023-02-18 23:35:31 +01:00
|
|
|
};
|
2023-09-20 17:25:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
format specification
|
|
|
|
- 'j', as json array
|
|
|
|
- 'X', same as std::format("{}x{}", vec.x, vec.y)
|
|
|
|
- number, floating point precision, use `0` to format as integer
|
|
|
|
*/
|
|
|
|
template <typename CharT>
|
|
|
|
struct std::formatter<Vector2D, CharT> : std::formatter<CharT> {
|
|
|
|
bool formatJson = false;
|
|
|
|
bool formatX = false;
|
|
|
|
std::string precision = "";
|
|
|
|
FORMAT_PARSE(FORMAT_FLAG('j', formatJson) //
|
|
|
|
FORMAT_FLAG('X', formatX) //
|
|
|
|
FORMAT_NUMBER(precision),
|
|
|
|
Vector2D)
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const Vector2D& vec, FormatContext& ctx) const {
|
|
|
|
std::string formatString = precision.empty() ? "{}" : std::format("{{:.{}f}}", precision);
|
|
|
|
|
|
|
|
if (formatJson)
|
|
|
|
formatString = std::format("[{0}, {0}]", formatString);
|
|
|
|
else if (formatX)
|
|
|
|
formatString = std::format("{0}x{0}", formatString);
|
|
|
|
else
|
|
|
|
formatString = std::format("[Vector2D: x: {0}, y: {0}]", formatString);
|
|
|
|
try {
|
|
|
|
string buf = std::vformat(formatString, std::make_format_args(vec.x, vec.y));
|
|
|
|
return std::format_to(ctx.out(), "{}", buf);
|
|
|
|
} catch (std::format_error& e) { return std::format_to(ctx.out(), "[{}, {}]", vec.x, vec.y); }
|
|
|
|
}
|
|
|
|
};
|