2022-03-16 22:21:12 +01:00
|
|
|
#include "Vector2D.hpp"
|
2022-07-02 22:17:17 +02:00
|
|
|
#include <algorithm>
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:21:12 +01:00
|
|
|
Vector2D::~Vector2D() {}
|
|
|
|
|
|
|
|
double Vector2D::normalize() {
|
|
|
|
// get max abs
|
|
|
|
const auto max = abs(x) > abs(y) ? abs(x) : abs(y);
|
|
|
|
|
|
|
|
x /= max;
|
|
|
|
y /= max;
|
|
|
|
|
|
|
|
return max;
|
2022-03-17 15:53:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D Vector2D::floor() {
|
|
|
|
return Vector2D((int)x, (int)y);
|
2022-07-02 22:17:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D Vector2D::clamp(const Vector2D& min, const Vector2D& max) {
|
2022-12-16 18:17:31 +01:00
|
|
|
return Vector2D(std::clamp(this->x, min.x, max.x == 0 ? INFINITY : max.x), std::clamp(this->y, min.y, max.y == 0 ? INFINITY : max.y));
|
2023-02-18 23:35:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double Vector2D::distance(const Vector2D& other) {
|
|
|
|
double dx = x - other.x;
|
|
|
|
double dy = y - other.y;
|
|
|
|
return std::sqrt(dx * dx + dy * dy);
|
|
|
|
}
|