summaryrefslogtreecommitdiff
path: root/src/helpers/Vector2D.cpp
blob: 6f96d686190635c012c06a696120483aaea91492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Vector2D.hpp"
#include <algorithm>
#include <cmath>

Vector2D::Vector2D(double xx, double yy) {
    x = xx;
    y = yy;
}

Vector2D::Vector2D() {
    x = 0;
    y = 0;
}

Vector2D::Vector2D(const Hyprlang::VEC2& ref) {
    x = ref.x;
    y = ref.y;
}

Vector2D::~Vector2D() {}

double Vector2D::normalize() {
    // get max abs
    const auto max = std::abs(x) > std::abs(y) ? std::abs(x) : std::abs(y);

    x /= max;
    y /= max;

    return max;
}

Vector2D Vector2D::floor() const {
    return Vector2D(std::floor(x), std::floor(y));
}

Vector2D Vector2D::round() const {
    return Vector2D(std::round(x), std::round(y));
}

Vector2D Vector2D::clamp(const Vector2D& min, const Vector2D& max) const {
    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));
}

double Vector2D::distance(const Vector2D& other) const {
    return std::sqrt(distanceSq(other));
}

double Vector2D::distanceSq(const Vector2D& other) const {
    return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y);
}

double Vector2D::size() const {
    return std::sqrt(x * x + y * y);
}

Vector2D Vector2D::getComponentMax(const Vector2D& other) const {
    return Vector2D(std::max(this->x, other.x), std::max(this->y, other.y));
}