peq-quests/lua_modules/aa_box.lua
Michael Cook (mackal) a3ae35e715 Add an add(x, y) function to AA box lua class
This can be used like:
tether_box = box() -- needs to be default in this case
-- calculate minimum AA box from four corners of room
-- can be as many corners as needed to make easy for geometry of room,
-- but at least 3 would be required
tether_box:add(831.11, -16.90)
tether_box:add(752.95, -16.90)
tether_box:add(752.90, -159.11)
tether_box:add(831.10, -159.11)
2018-03-25 18:42:42 -04:00

45 lines
983 B
Lua

-- simple Axis Aligned Bounding Box can be used for rectangular leashing/tethering
local box = {}
box.__index = box
setmetatable(box, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function box.new(top, bottom, right, left)
local self = setmetatable({}, box)
self.top = top or -99999
self.bottom = bottom or 99999
self.right = right or 99999
self.left = left or -99999
return self
end
-- the box contains this point
function box:contains(x, y)
if x <= self.left and x >= self.right and y <= self.top and y >= self.bottom then
return true
else
return false
end
end
-- expands the box to contain provided x/y (used to auto calculate)
function box:add(x, y)
if x < self.right then
self.right = x
end
if x > self.left then
self.left = x
end
if y < self.bottom then
self.bottom = y
end
if y > self.top then
self.top = y
end
end
return box