-
Notifications
You must be signed in to change notification settings - Fork 5
/
kruskal.gd
54 lines (44 loc) · 1.36 KB
/
kruskal.gd
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
var vertices_tree_idx = {}
var vertices = []
var edges_dic = {}
var edges = []
var tree_id_counter = 0
class KeySorter:
static func sort(a, b):
# print(a, b)
if a.x < b.x:
return true
elif a.x == b.x and a.y < a.y:
return true
return false
class EdgeSorter:
static func sort(a, b):
if a["distance"] < b["distance"]:
return true
return false
func start(points): # Receive a dictionary of points {vec2:{vec2:true, vec2:true...} ...}
var ret_edges = []
vertices = points
# First, get an array of edges {distance, v1, v2}, in distance order
for p in points.keys():
vertices_tree_idx[p] = tree_id_counter
tree_id_counter += 1
for ps in points[p].keys():
var key = [p,ps]
key.sort_custom(KeySorter, "sort")
edges_dic[key] = {"v1": p, "v2": ps, "distance": p.distance_to(ps)}
edges = edges_dic.values()
edges.sort_custom(EdgeSorter, "sort")
# For each edge, check if it can connect vertices
for edge in edges:
if vertices_tree_idx[edge["v1"]] != vertices_tree_idx[edge["v2"]]:
change_tree_idx(vertices_tree_idx[edge["v1"]], vertices_tree_idx[edge["v2"]])
ret_edges.push_back([edge["v1"], edge["v2"]])
print(ret_edges)
return ret_edges
func change_tree_idx(id_from, id_to):
for vertice in vertices:
if vertices_tree_idx[vertice] == id_from:
vertices_tree_idx[vertice] = id_to
#func _ready():
# edges.sort_custom(EdgeSorter, "sort")