-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtree.lua
More file actions
32 lines (29 loc) · 687 Bytes
/
rtree.lua
File metadata and controls
32 lines (29 loc) · 687 Bytes
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
require 'class'
require 'nodes'
RTree = class(function(r, maxChildren)
if (maxChildren == nil) then
r.maxChildren = 9
else
r.maxChildren = maxChildren
end
r.children = { RTreeNode() }
end)
function RTree:insert(data, bbox)
local child = self:findBestChild(bbox)
child:insert(data, bbox)
end
function RTree:findBestChild(bbox)
if #self.children == 1 then
return self.children[1]
end
local bestChild = nil
local bestChildOverlap = math.huge * -1
for i, v in pairs(self.children) do
local overlap = bbox.overlap(v.bbox)
if overlap > bestChildOverlap then
bestChild = v
bestChildOverlap = overlap
end
end
return bestChild
end