2022-03-16 22:21:12 +01:00
|
|
|
#include "Vector2D.hpp"
|
2022-07-02 22:17:17 +02:00
|
|
|
#include <algorithm>
|
2023-05-29 15:12:00 +02:00
|
|
|
#include <cmath>
|
2022-03-16 22:21:12 +01:00
|
|
|
|
|
|
|
Vector2D::Vector2D(double xx, double yy) {
|
|
|
|
x = xx;
|
|
|
|
y = yy;
|
|
|
|
}
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
Vector2D::Vector2D() {
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
|
2024-02-18 16:00:34 +01:00
|
|
|
Vector2D::Vector2D(const Hyprlang::VEC2& ref) {
|
|
|
|
x = ref.x;
|
|
|
|
y = ref.y;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:21:12 +01:00
|
|
|
Vector2D::~Vector2D() {}
|
|
|
|
|
|
|
|
double Vector2D::normalize() {
|
|
|
|
// get max abs
|
2023-05-28 22:50:13 +02:00
|
|
|
const auto max = std::abs(x) > std::abs(y) ? std::abs(x) : std::abs(y);
|
2022-03-16 22:21:12 +01:00
|
|
|
|
|
|
|
x /= max;
|
|
|
|
y /= max;
|
|
|
|
|
|
|
|
return max;
|
2022-03-17 15:53:45 +01:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:14:04 +02:00
|
|
|
Vector2D Vector2D::floor() const {
|
2023-05-28 22:50:13 +02:00
|
|
|
return Vector2D(std::floor(x), std::floor(y));
|
2022-07-02 22:17:17 +02:00
|
|
|
}
|
|
|
|
|
2023-11-11 15:37:17 +01:00
|
|
|
Vector2D Vector2D::round() const {
|
|
|
|
return Vector2D(std::round(x), std::round(y));
|
|
|
|
}
|
|
|
|
|
2023-06-23 21:14:04 +02:00
|
|
|
Vector2D Vector2D::clamp(const Vector2D& min, const Vector2D& max) const {
|
2023-04-16 02:27:14 +02:00
|
|
|
return Vector2D(std::clamp(this->x, min.x, max.x < min.x ? INFINITY : max.x), std::clamp(this->y, min.y, max.y < min.y ? INFINITY : max.y));
|
2023-02-18 23:35:31 +01:00
|
|
|
}
|
|
|
|
|
2023-06-23 21:14:04 +02:00
|
|
|
double Vector2D::distance(const Vector2D& other) const {
|
2023-02-18 23:35:31 +01:00
|
|
|
double dx = x - other.x;
|
|
|
|
double dy = y - other.y;
|
|
|
|
return std::sqrt(dx * dx + dy * dy);
|
|
|
|
}
|
2023-07-11 13:37:25 +02:00
|
|
|
|
2023-11-05 17:18:41 +01:00
|
|
|
double Vector2D::size() const {
|
|
|
|
return std::sqrt(x * x + y * y);
|
|
|
|
}
|
2024-02-04 16:40:20 +01:00
|
|
|
|
|
|
|
Vector2D Vector2D::getComponentMax(const Vector2D& other) const {
|
|
|
|
return Vector2D(std::max(this->x, other.x), std::max(this->y, other.y));
|
|
|
|
}
|