-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbounding-box.lua
337 lines (309 loc) · 9.69 KB
/
bounding-box.lua
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
if ... ~= "__flib__.bounding-box" then
return require("__flib__.bounding-box")
end
local position = require("__flib__.position")
--- Utilities for manipulating bounding boxes. All functions support both the shorthand and explicit syntaxes for boxes
--- and positions, and will preserve the syntax that was passed in. Boxes are considered immutable; all functions will
--- return new boxes.
--- ```lua
--- local flib_bounding_box = require("__flib__.bounding-box")
--- ```
--- @class flib_bounding_box
local flib_bounding_box = {}
--- Return a new box expanded to the nearest tile edges.
--- @param box BoundingBox
--- @return BoundingBox
function flib_bounding_box.ceil(box)
if box.left_top then
return {
left_top = { x = math.floor(box.left_top.x), y = math.floor(box.left_top.y) },
right_bottom = { x = math.ceil(box.right_bottom.x), y = math.ceil(box.right_bottom.y) },
}
else
return {
{ math.floor(box[1][1]), math.floor(box[1][2]) },
{ math.ceil(box[2][1]), math.ceil(box[2][2]) },
}
end
end
--- Calculate the centerpoint of the box.
--- @param box BoundingBox
--- @return MapPosition
function flib_bounding_box.center(box)
if box.left_top then
return {
x = (box.left_top.x + box.right_bottom.x) / 2,
y = (box.left_top.y + box.right_bottom.y) / 2,
}
else
return {
(box[1][1] + box[2][1]) / 2,
(box[1][2] + box[2][2]) / 2,
}
end
end
--- Check if the first box contains the second box.
--- @param box1 BoundingBox
--- @param box2 BoundingBox
--- @return boolean
function flib_bounding_box.contains_box(box1, box2)
local box1 = flib_bounding_box.ensure_explicit(box1)
local box2 = flib_bounding_box.ensure_explicit(box2)
return box1.left_top.x <= box2.left_top.x
and box1.left_top.y <= box2.left_top.y
and box1.right_bottom.x >= box2.right_bottom.x
and box1.right_bottom.y >= box2.right_bottom.y
end
--- Check if the given box contains the given position.
--- @param box BoundingBox
--- @param pos MapPosition
--- @return boolean
function flib_bounding_box.contains_position(box, pos)
local box = flib_bounding_box.ensure_explicit(box)
local pos = position.ensure_explicit(pos)
return box.left_top.x <= pos.x
and box.left_top.y <= pos.y
and box.right_bottom.x >= pos.x
and box.right_bottom.y >= pos.y
end
--- Return the box in explicit form.
--- @param box BoundingBox
--- @return BoundingBox
function flib_bounding_box.ensure_explicit(box)
return {
left_top = position.ensure_explicit(box.left_top or box[1]),
right_bottom = position.ensure_explicit(box.right_bottom or box[2]),
}
end
--- Return the box in shorthand form.
--- @param box BoundingBox
--- @return BoundingBox
function flib_bounding_box.ensure_short(box)
return {
position.ensure_short(box.left_top or box[1]),
position.ensure_short(box.right_bottom or box[2]),
}
end
--- Return a new box with initial dimensions box1, expanded to contain box2.
--- @param box1 BoundingBox
--- @param box2 BoundingBox
--- @return BoundingBox
function flib_bounding_box.expand_to_contain_box(box1, box2)
local box2 = flib_bounding_box.ensure_explicit(box2)
if box1.left_top then
return {
left_top = {
x = math.min(box1.left_top.x, box2.left_top.x),
y = math.min(box1.left_top.y, box2.left_top.y),
},
right_bottom = {
x = math.max(box1.right_bottom.x, box2.right_bottom.x),
y = math.max(box1.right_bottom.y, box2.right_bottom.y),
},
}
else
return {
{
math.min(box1[1][1], box2.left_top.x),
math.min(box1[1][2], box2.left_top.y),
},
{
math.max(box1[2][1], box2.right_bottom.x),
math.max(box1[2][2], box2.right_bottom.y),
},
}
end
end
--- Return a new box expanded to contain the given position.
--- @param box BoundingBox
--- @param pos MapPosition
--- @return BoundingBox
function flib_bounding_box.expand_to_contain_position(box, pos)
local pos = position.ensure_explicit(pos)
if box.left_top then
return {
left_top = { x = math.min(box.left_top.x, pos.x), y = math.min(box.left_top.y, pos.y) },
right_bottom = { x = math.max(box.right_bottom.x, pos.x), y = math.max(box.right_bottom.y, pos.y) },
}
else
return {
{ math.min(box[1][1], pos.x), math.min(box[1][2], pos.y) },
{ math.max(box[2][1], pos.x), math.max(box[2][2], pos.y) },
}
end
end
--- Return a new box shrunk to the nearest tile edges.
--- @param box BoundingBox
--- @return BoundingBox
function flib_bounding_box.floor(box)
if box.left_top then
return {
left_top = { x = math.ceil(box.left_top.x), y = math.ceil(box.left_top.y) },
right_bottom = { x = math.floor(box.right_bottom.x), y = math.floor(box.right_bottom.y) },
}
else
return {
{ math.ceil(box[1][1]), math.ceil(box[1][2]) },
{ math.floor(box[2][1]), math.floor(box[2][2]) },
}
end
end
--- Create a new box from a centerpoint and dimensions.
--- @param center MapPosition
--- @param width number
--- @param height number
--- @return BoundingBox
function flib_bounding_box.from_dimensions(center, width, height)
if center.x then
return {
left_top = { x = center.x - width / 2, y = center.y - height / 2 },
right_bottom = { x = center.x + width / 2, y = center.y + height / 2 },
}
else
return {
{ center[1] - width / 2, center[2] - height / 2 },
{ center[1] + width / 2, center[2] + height / 2 },
}
end
end
--- Create a 1x1 box from the given position, optionally snapped to the containing tile edges.
--- @param pos MapPosition
--- @param snap boolean?
--- @return BoundingBox
function flib_bounding_box.from_position(pos, snap)
if snap then
pos = position.floor(pos)
else
pos = position.sub(pos, { 0.5, 0.5 })
end
local x = pos.x or pos[1]
local y = pos.y or pos[2]
if pos.x then
return {
left_top = { x = x, y = y },
right_bottom = { x = x + 1, y = y + 1 },
}
else
return {
{ x, y },
{ x + 1, y + 1 },
}
end
end
--- Calculate the height of the box.
--- @param box BoundingBox
--- @return number
function flib_bounding_box.height(box)
if box.left_top then
return box.right_bottom.y - box.left_top.y
else
return box[2][2] - box[1][2]
end
end
--- Check if the first box intersects (overlaps) the second box.
--- @param box1 BoundingBox
--- @param box2 BoundingBox
--- @return boolean
function flib_bounding_box.intersects_box(box1, box2)
local box1 = flib_bounding_box.ensure_explicit(box1)
local box2 = flib_bounding_box.ensure_explicit(box2)
return box1.left_top.x < box2.right_bottom.x
and box2.left_top.x < box1.right_bottom.x
and box1.left_top.y < box2.right_bottom.y
and box2.left_top.y < box1.right_bottom.y
end
--- Return a new box with the same dimensions, moved by the given delta.
--- @param box BoundingBox
--- @param delta MapPosition
--- @return BoundingBox
function flib_bounding_box.move(box, delta)
local dx = delta.x or delta[1]
local dy = delta.y or delta[2]
if box.left_top then
return {
left_top = { x = box.left_top.x + dx, y = box.left_top.y + dy },
right_bottom = { x = box.right_bottom.x + dx, y = box.right_bottom.y + dy },
}
else
return {
{ box[1][1] + dx, box[1][2] + dy },
{ box[2][1] + dx, box[2][2] + dy },
}
end
end
--- Return a new box with the same dimensions centered on the given position.
--- @param box BoundingBox
--- @param pos MapPosition
--- @return BoundingBox
function flib_bounding_box.recenter_on(box, pos)
local height = flib_bounding_box.height(box)
local width = flib_bounding_box.width(box)
local pos_x = pos.x or pos[1]
local pos_y = pos.y or pos[2]
if box.left_top then
return {
left_top = { x = pos_x - (width / 2), y = pos_y - (height / 2) },
right_bottom = { x = pos_x + (width / 2), y = pos_y + (height / 2) },
}
else
return {
{ pos_x - (width / 2), pos_y - (height / 2) },
{ pos_x + (width / 2), pos_y + (height / 2) },
}
end
end
--- Return a new box grown or shrunk by the given delta. A positive delta will grow the box, a negative delta will
--- shrink it.
--- @param box BoundingBox
--- @param delta number
--- @return BoundingBox
function flib_bounding_box.resize(box, delta)
if box.left_top then
return {
left_top = { x = box.left_top.x - delta, y = box.left_top.y - delta },
right_bottom = { x = box.right_bottom.x + delta, y = box.right_bottom.y + delta },
}
else
return {
{ box[1][1] - delta, box[1][2] - delta },
{ box[2][1] + delta, box[2][2] + delta },
}
end
end
--- Return a new box rotated 90 degrees about its center.
--- @param box BoundingBox
--- @return BoundingBox
function flib_bounding_box.rotate(box)
local center = flib_bounding_box.center(box)
local radius_x = flib_bounding_box.width(box) / 2
local radius_y = flib_bounding_box.height(box) / 2
if box.left_top then
return {
left_top = { x = center.x - radius_y, y = center.y - radius_x },
right_bottom = { x = center.x + radius_y, y = center.y + radius_x },
}
else
return {
{ center.x - radius_y, center.y - radius_x },
{ center.x + radius_y, center.y + radius_x },
}
end
end
--- Return a new box expanded to create a square.
--- @param box BoundingBox
--- @return BoundingBox
function flib_bounding_box.square(box)
local radius = math.max(flib_bounding_box.width(box), flib_bounding_box.height(box))
return flib_bounding_box.from_dimensions(flib_bounding_box.center(box), radius, radius)
end
--- Calculate the width of the box.
--- @param box BoundingBox
--- @return number
function flib_bounding_box.width(box)
if box.left_top then
return box.right_bottom.x - box.left_top.x
else
return box[2][1] - box[1][1]
end
end
return flib_bounding_box