forked from loganfechner/ap-performance-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmst.lua
104 lines (90 loc) · 2.26 KB
/
mst.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
local tilesize = require "tilesize"
local Delaunay = require "delaunay"
local Point = Delaunay.Point
local Edge = Delaunay.Edge
local Kruskals = require "kruskals"
local MST = { points = {}, edges = {}, tree = {} }
local function compare(a, b)
if a:length() < b:length() then return a end
end
local function clearTable(t)
local remove = table.remove
for i = #t, 1, -1 do
remove(t, i)
end
end
function MST:initialize(rooms)
if #self.points > 0 then
clearTable(self.points)
end
if #self.edges > 0 then
clearTable(self.edges)
end
if #self.tree > 0 then
clearTable(self.tree)
end
-- Get the points
local floor = math.floor
for i = #rooms, 1, -1 do
local x = floor(rooms[i].x + rooms[i].width / 2)
local y = floor(rooms[i].y + rooms[i].height / 2)
self.points[i] = Point(x, y)
end
-- Get the graph
local triangles = Delaunay.triangulate(unpack(self.points))
for i = 1, #triangles do
local p1 = triangles[i].p1
local p2 = triangles[i].p2
local p3 = triangles[i].p3
if #self.edges > 1 then
local a = Edge(p1, p2)
local b = Edge(p2, p3)
local c = Edge(p1, p3)
if not self:edgeAdded(self.edges, a) then
self.edges[#self.edges+1] = a
end
if not self:edgeAdded(self.edges, b) then
self.edges[#self.edges+1] = b
end
if not self:edgeAdded(self.edges, c) then
self.edges[#self.edges+1] = c
end
else
self.edges[#self.edges+1] = Edge(p1, p2)
self.edges[#self.edges+1] = Edge(p2, p3)
self.edges[#self.edges+1] = Edge(p1, p3)
end
end
-- Sort edges least to greatest
local sort = table.sort
sort(self.edges, compare)
-- Create the minimum spanning tree!
self.tree = Kruskals(self.points, self.edges)
end
function MST:edgeAdded(edges, edge)
for i = 1, #edges do
local temp = self.edges[i]
if temp:same(edge) then
return true
end
end
return false
end
function MST:getTree()
return self.tree
end
function MST:draw()
for i = 1, #self.tree do
local p1 = self.tree[i].p1
local p2 = self.tree[i].p2
local x1 = p1.x * tilesize
local x2 = p2.x * tilesize
local y1 = p1.y * tilesize
local y2 = p2.y * tilesize
love.graphics.setColor(0,255,0)
love.graphics.line(x1 - 1, y1 - 1, x2 - 1, y2 - 1)
love.graphics.line(x1, y1, x2, y2)
love.graphics.line(x1 + 1, y1 + 1, x2 + 1, y2 + 1)
end
end
return MST