-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo2.hpp
executable file
·181 lines (133 loc) · 2.96 KB
/
geo2.hpp
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
#pragma once
#include "format.hpp"
#include "aga2.hpp"
namespace geo2 {
template <class U>
using v2 = aga2::Mv1<U>;
template <class T>
T limit(T val, T min, T max) {
if (val < min) return min;
if (val > max) return max;
return val;
}
template <class T>
bool in_bound(T val, T pos, T end) {
return pos <= val and val < end;
}
template <class U>
bool in_bounds(v2<U> val, v2<U> pos, v2<U> end) {
return
in_bound(val[0], pos[0], end[0])
and
in_bound(val[1], pos[1], end[1]);
}
template <class T>
struct aabb2 {
v2<T> pos;
v2<T> end;
aabb2() = default;
aabb2(v2<T> pos, v2<T> end):
pos(pos), end(end)
{}
aabb2(T x0, T y0, T x1, T y1):
pos(x0,y0), end(x1,y1)
{}
aabb2(aabb2 const& o):
pos(o.pos), end(o.end)
{}
v2<T> dim() const {
return end - pos;
}
void dim(v2<T> d) {
end = pos + d;
}
template <class U>
explicit aabb2(aabb2<U> other): aabb2(v2<T>(other.pos), v2<T>(other.end)) {
}
};
template <class T>
std::ostream & operator<<(std::ostream & o, aabb2<T> const& a)
{
print(o, "%|| %|| %|| %||", a.pos[0], a.pos[1], a.end[0], a.end[1]);
return o;
}
template <class T>
struct b2 {
v2<T> pos, dim;
b2() = default;
b2(v2<T> pos, v2<T> dim):
pos(pos), dim(dim)
{}
b2(T x, T y, T w, T h):
pos(x,y), dim(w,h)
{}
b2(b2 const& o):
pos(o.pos), dim(o.dim)
{}
v2<T> end() const {
return pos + dim;
}
void end(v2<T> e) const {
dim = e - pos;
}
template <class U>
explicit b2(b2<U> other): b2(v2<T>(other.pos), v2<T>(other.dim)) {
}
};
template <class T>
b2<T> operator*(b2<T> const& b, T s) {
return b2<T>(b.pos * s, b.dim * s);
}
template <class T>
v2<T> vmul(v2<T> a, v2<T> b) {
return v2<T>(a[0]*b[0], a[1]*b[1]);
}
template <class T>
v2<T> vdiv(v2<T> a, v2<T> b) {
return v2<T>(a[0]/b[0], a[1]/b[1]);
}
template <class T>
v2<T> vdiv0(v2<T> a, v2<T> b) {
return v2<T>(
((b[0] != T(0)) ? (a[0]/b[0]) : (T(0))),
((b[1] != T(0)) ? (a[1]/b[1]) : (T(0)))
);
}
using v2s = v2<int16_t>;
using b2s = b2<int16_t>;
using v2f = v2<float>;
inline v2s calc_align(
b2s box, v2s dim,
v2f const& align = v2f(0.5, 0.5)
) {
return box.pos + v2s(vmul(v2f(box.dim - dim), align));
}
//template <class T>
//v2<T> calc_align(b2<T> par, v2<T> dim, v2<float> align = v2<float>(0.5, 0.5)) {
// return v2<T>(v2<float>(par.pos) + vmul(v2<float>(par.dim - dim), align));
//}
template <class T>
bool overlap(b2<T> b, v2<T> pos) {
auto b_end = b.end();
return
b.pos[0] <= pos[0] and
b.pos[1] <= pos[1] and
pos[0] < b_end[0] and
pos[1] < b_end[1];
}
template <class T>
inline std::ostream & operator<<(std::ostream & o, b2<T> const& b) {
o << format("(%|| %||)", b.pos, b.dim);
return o;
}
/*template <class T>
b2<T> intersect(b2<T> a, b2<T> b) {
b2<T> c;
auto b_end = b.end();
return
b.pos[0] <= pos[0] and
b.pos[1] <= pos[1] and
pos[0] <= b_end[0] and
pos[1] <= b_end[1];
}*/
}