-
Notifications
You must be signed in to change notification settings - Fork 4
/
bounds.h
263 lines (216 loc) · 6.7 KB
/
bounds.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#pragma once
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include "vec.h"
#include "SDL_gpu.h"
struct CircleBounds;
struct BoxBounds
{
float left, top;
float width, height;
constexpr BoxBounds(float x, float y, float w, float h) : left(x), top(y), width(w), height(h) { }
consteval BoxBounds() : BoxBounds(-1, -1, 0, 0) { }
constexpr BoxBounds(vec topleft, vec size) : BoxBounds(topleft.x, topleft.y, size.x, size.y) {}
constexpr explicit BoxBounds(vec size) : BoxBounds(0, 0, size.x, size.y) { }
constexpr explicit BoxBounds(vec pos, vec size, vec origin) : BoxBounds(pos.x, pos.y, size.x, size.y) {
left -= origin.x;
top -= origin.y;
}
template<typename T> //Works with GPU_Rect, SDL_Rect and SDL_FRect
constexpr explicit BoxBounds(T rect) : BoxBounds(rect.x, rect.y, rect.w, rect.h) { }
[[nodiscard]] static constexpr BoxBounds FromCenter(vec center, vec size) { return BoxBounds(center - size / 2, size); }
[[nodiscard]] GPU_Rect AsRect() {
return GPU_Rect{ left, top, width, height };
}
//Expands arround the center by a factor
[[nodiscard]] BoxBounds operator*(float f) const
{
vec center = Center();
BoxBounds ret = *this;
ret.width *= f;
ret.height *= f;
ret.SetCenter(center);
return ret;
}
//Expands arround the center by a value
void Grow(float x, float y)
{
left -= x / 2;
top -= y / 2;
width += x;
height += y;
}
[[nodiscard]] BoxBounds Grown(float x, float y) const
{
BoxBounds ret = *this;
ret.Grow(x, y);
return ret;
}
[[nodiscard]] constexpr vec Center() const
{
return vec(left + width/2, top + height/2);
}
[[nodiscard]] constexpr float Area() const
{
return width * height;
}
void SetCenter(float x, float y)
{
left = x - width/2;
top = y - height/2;
}
void SetTopAndBottom(float newTop, float newBottom)
{
top = newTop;
height = newBottom - newTop;
}
void SetCenter(vec center)
{
SetCenter(center.x, center.y);
}
void SetTopLeft(float x, float y)
{
left = x;
top = y;
}
void SetTopLeft(vec pos)
{
SetTopLeft(pos.x, pos.y);
}
void DebugDraw(uint8_t r = 255, uint8_t g = 0, uint8_t b = 0) const
#ifdef _DEBUG
;
#else
{}
#endif
[[nodiscard]] constexpr float Top() const
{
return top;
}
[[nodiscard]] constexpr float Bottom() const
{
return top + height;
}
[[nodiscard]] constexpr float Left() const
{
return left;
}
[[nodiscard]] constexpr float Right() const
{
return left + width;
}
[[nodiscard]] constexpr vec TopLeft() const {
return vec(Left(), Top());
}
[[nodiscard]] constexpr vec TopRight() const {
return vec(Right(), Top());
}
[[nodiscard]] constexpr vec BottomLeft() const {
return vec(Left(), Bottom());
}
[[nodiscard]] constexpr vec BottomRight() const {
return vec(Right(), Bottom());
}
[[nodiscard]] bool Contains(float x, float y) const
{
if (x < left) return false;
if (x >= left + width) return false;
if (y < top) return false;
if (y >= top + height) return false;
return true;
}
[[nodiscard]] bool Contains(vec point) const
{
return Contains(point.x, point.y);
}
[[nodiscard]] bool Contains(const BoxBounds& b) const
{
if (b.left < left) return false;
if (b.left+b.width >= left + width) return false;
if (b.top < top) return false;
if (b.top+ b.height >= top + height) return false;
return true;
}
[[nodiscard]] constexpr vec Size() const {
return vec(width, height);
}
[[nodiscard]] vec ClosestPointInBounds(vec target) const;
[[nodiscard]] float DistanceSq(const BoxBounds& a) const;
[[nodiscard]] float DistanceSq(const CircleBounds& a) const;
[[nodiscard]] float Distance(const BoxBounds& a) const;
[[nodiscard]] float Distance(const CircleBounds& a) const;
};
struct CircleBounds
{
constexpr CircleBounds(vec pos, float radius) : pos(pos), radius(radius) {}
vec pos;
float radius;
void DebugDraw(uint8_t r = 255, uint8_t g = 0, uint8_t b = 0) const
#ifdef _DEBUG
;
#else
{}
#endif
[[nodiscard]] vec Center() const { return pos; }
[[nodiscard]] float DistanceSq(const BoxBounds& a) const { return a.DistanceSq(*this); }
[[nodiscard]] float Distance(const BoxBounds& a) const { return a.Distance(*this); }
[[nodiscard]] float DistanceSq(const CircleBounds& a) const {
return a.pos.DistanceSq(this->pos) - (a.radius + this->radius) * (a.radius + this->radius);
}
[[nodiscard]] float Distance(const CircleBounds& a) const {
return a.pos.Distance(this->pos) - (a.radius + this->radius);
}
[[nodiscard]] bool Contains(float x, float y) const { return Contains(vec(x, y)); }
[[nodiscard]] bool Contains(vec v) const { return (pos.DistanceSq(v) < radius*radius); }
[[nodiscard]] BoxBounds EnclosingBoxBounds() const { return BoxBounds::FromCenter(Center(), vec(radius*2)); }
};
inline float BoxBounds::DistanceSq(const BoxBounds& a) const {
float sqrDist = 0;
if (a.Right() < this->left) {
float d = a.Right() - this->left;
sqrDist += d * d;
} else if (a.left > this->Right()) {
float d = a.left - this->Right();
sqrDist += d * d;
}
if (a.Bottom() < this->top) {
float d = a.Bottom() - this->top;
sqrDist += d * d;
}
else if (a.top > this->Bottom()) {
float d = a.top - this->Bottom();
sqrDist += d * d;
}
return sqrDist;
}
inline vec BoxBounds::ClosestPointInBounds(vec target) const {
vec distance = this->Center() - target;
distance.Clamp(-this->Size() / 2, this->Size() / 2);
vec closestPoint = this->Center() - distance;
return closestPoint;
}
inline float BoxBounds::DistanceSq(const CircleBounds& a) const {
vec closestPoint = ClosestPointInBounds(a.pos);
//closestPoint.DebugDraw();
return closestPoint.DistanceSq(a.pos) - (a.radius * a.radius);
}
inline float BoxBounds::Distance(const BoxBounds& a) const
{
return sqrt(DistanceSq(a));
}
inline float BoxBounds::Distance(const CircleBounds& a) const
{
return sqrt(DistanceSq(a));
}
inline std::ostream& operator<<(std::ostream& os, const BoxBounds& rhs)
{
os << rhs.left << " " << rhs.top << " " << rhs.width << " " << rhs.height;
return os;
}
inline std::ostream& operator<<(std::ostream& os, const CircleBounds& rhs)
{
os << rhs.pos << " r=" << rhs.radius;
return os;
}