-
Notifications
You must be signed in to change notification settings - Fork 1
/
RC.h
115 lines (99 loc) · 2.4 KB
/
RC.h
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#include <Core/Math.h>
#include <Core/Vec2.h>
#include <string>
struct RC
{
RC() = default;
RC(int rr, int cc) : r(rr), c(cc) {}
int r = -1;
int c = -1;
bool operator==(const RC& pt) const { return r == pt.r && c == pt.c; }
bool operator!=(const RC& pt) const { return !(*this == pt); }
bool operator<(const RC& other) const
{
if (r != other.r)
return r < other.r;
return c < other.c;
}
const RC& operator+() const
{
return *this;
}
RC operator-() const
{
return { -r, -c };
}
RC operator+(const RC& pt) const
{
return { r + pt.r, c + pt.c };
}
RC operator-(const RC& pt) const
{
return { r - pt.r, c - pt.c };
}
RC& operator+=(const RC& pt)
{
this->r += pt.r;
this->c += pt.c;
return *this;
}
RC& operator-=(const RC& pt)
{
this->r -= pt.r;
this->c -= pt.c;
return *this;
}
RC operator*(int v) const
{
return { r * v, c * v };
}
RC operator/(int v) const
{
return { r / v, c / v };
}
RC& operator/=(int v)
{
this->r /= v;
this->c /= v;
return *this;
}
RC abs() const
{
return { static_cast<int>(std::abs(r)), static_cast<int>(std::abs(c)) };
}
RC clamp(int r_min, int r_max, int c_min, int c_max)
{
RC rc;
rc.r = math::clamp<int>(r, r_min, r_max);
rc.c = math::clamp<int>(c, c_min, c_max);
return rc;
}
std::string str() const
{
return "(" + std::to_string(r) + ", " + std::to_string(c) + ")";
}
};
static inline float distance(const RC& ptA, const RC& ptB)
{
return math::distance<float>(static_cast<float>(ptA.r),
static_cast<float>(ptA.c),
static_cast<float>(ptB.r),
static_cast<float>(ptB.c));
}
static inline float distance_squared(const RC& ptA, const RC& ptB)
{
return math::distance_squared<float>(static_cast<float>(ptA.r),
static_cast<float>(ptA.c),
static_cast<float>(ptB.r),
static_cast<float>(ptB.c));
}
Vec2 to_Vec2(const RC& p)
{
Vec2 v;
v.r = static_cast<float>(p.r);
v.c = static_cast<float>(p.c);
return v;
}
RC to_RC_round(const Vec2& v) { return { math::roundI(v.r), math::roundI(v.c) }; }
RC to_RC_floor(const Vec2& v) { return { static_cast<int>(v.r), static_cast<int>(v.c) }; }