-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathneighbors.py
171 lines (138 loc) · 6.41 KB
/
neighbors.py
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
#!/usr/bin/env python
from enum import *
from tkinter import *
class QuadTree:
"Quadtree"
class Tag(Enum):
NONE = 0
SELECTED = 1
NEIGHBOR = 2
class Child(IntEnum):
NW = 0
NE = 1
SW = 2
SE = 3
class Direction(Enum):
NW = 0
NE = 1
SW = 2
SE = 3
N = 4
S = 5
W = 6
E = 7
Colors = {Tag.NONE : "#808080", Tag.NEIGHBOR : "#ee3030", Tag.SELECTED : "#30ee30"}
def __init__(self, children=None):
self.parent = None
self.children = []
self.tag = self.Tag.NONE
if children is not None:
for child in children:
self.add_child(child)
def add_child(self, child):
assert isinstance(child, QuadTree)
child.parent = self
self.children.append(child)
def is_leaf(self):
return not self.children
def get_neighbor_of_greater_or_equal_size(self, direction):
if direction == self.Direction.N:
if self.parent is None:
return None
if self.parent.children[self.Child.SW] == self: # Is 'self' SW child?
return self.parent.children[self.Child.NW]
if self.parent.children[self.Child.SE] == self: # Is 'self' SE child?
return self.parent.children[self.Child.NE]
node = self.parent.get_neighbor_of_greater_or_equal_size(direction)
if node is None or node.is_leaf():
return node
# 'self' is guaranteed to be a north child
return (node.children[self.Child.SW]
if self.parent.children[self.Child.NW] == self # Is 'self' NW child?
else node.children[self.Child.SE])
else:
# TODO: implement other directions symmetric to NORTH case
assert False
return []
def find_neighbors_of_smaller_size(self, neighbor, direction):
candidates = [] if neighbor is None else [neighbor]
neighbors = []
if direction == self.Direction.N:
while len(candidates) > 0:
if candidates[0].is_leaf():
neighbors.append(candidates[0])
else:
candidates.append(candidates[0].children[self.Child.SW])
candidates.append(candidates[0].children[self.Child.SE])
candidates.remove(candidates[0])
return neighbors
else:
# TODO: implement other directions symmetric to NORTH case
assert False
def get_neighbors(self, direction):
neighbor = self.get_neighbor_of_greater_or_equal_size(direction)
neighbors = self.find_neighbors_of_smaller_size(neighbor, direction)
return neighbors
def draw(self, canvas, x0, y0, x1, y1):
canvas.create_rectangle(x0, y0, x1, y1, fill = self.Colors[self.tag])
if len(self.children) == 4:
hw = (x1-x0)/2
hh = (y1-y0)/2
assert hw > 0
assert hh > 0
self.children[self.Child.NW].draw(canvas, x0, y0, x0+hw, y0+hh)
self.children[self.Child.NE].draw(canvas, x0+hw, y0, x1, y0+hh)
self.children[self.Child.SW].draw(canvas, x0, y0+hh, x0+hw, y1)
self.children[self.Child.SE].draw(canvas, x0+hw, y0+hh, x1, y1)
posX = 10
posY = 50
def clear_tags_in_quadtree(tree):
tree.tag = tree.Tag.NONE
if tree.children is not None:
for child in tree.children:
clear_tags_in_quadtree(child)
def test_neighbor_finding(tree, selected, direction, canvas):
clear_tags_in_quadtree(tree)
selected.tag = tree.Tag.SELECTED
neighbors = selected.get_neighbors(direction)
for neighbor in neighbors:
assert neighbor.tag == tree.Tag.NONE
neighbor.tag = tree.Tag.NEIGHBOR
global posX, posY
tree.draw(canvas, posX, posY, posX+100, posY+100)
posX += 10+100;
def test_simple_cases():
tree = QuadTree([QuadTree(),
QuadTree([QuadTree(), QuadTree(), QuadTree(), QuadTree()]),
QuadTree([QuadTree(), QuadTree(), QuadTree(), QuadTree()]),
QuadTree()])
test_neighbor_finding(tree, tree.children[tree.Child.NW], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.NE].children[tree.Child.NW], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.NE].children[tree.Child.NE], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.SW].children[tree.Child.SW], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.SW].children[tree.Child.SE], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.SW].children[tree.Child.NW], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.SW].children[tree.Child.NE], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.SE], tree.Direction.N, canvas)
global posX, posY
posX = 10
posY += 10+100;
def test_complex_cases():
tree = QuadTree([QuadTree([QuadTree(), QuadTree(), QuadTree(), QuadTree()]),
QuadTree([QuadTree(), QuadTree(), QuadTree([QuadTree(), QuadTree(), QuadTree(), QuadTree()]), QuadTree()]),
QuadTree([QuadTree(), QuadTree([QuadTree(), QuadTree(), QuadTree(), QuadTree()]), QuadTree(), QuadTree()]),
QuadTree()])
test_neighbor_finding(tree, tree.children[tree.Child.SW].children[tree.Child.NW], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.SE], tree.Direction.N, canvas)
test_neighbor_finding(tree, tree.children[tree.Child.SW].children[tree.Child.NE].children[tree.Child.NW], tree.Direction.N, canvas)
return
# Main starts here
master = Tk()
master.title("Find neighbors in a Quadtree")
canvas = Canvas(master, width=900, height=600)
canvas.pack()
canvas.create_text(10, 5, text="Selected", fill=QuadTree.Colors[QuadTree.Tag.SELECTED], anchor=NW)
canvas.create_text(10, 20, text="Neighbors", fill=QuadTree.Colors[QuadTree.Tag.NEIGHBOR], anchor=NW)
test_simple_cases()
test_complex_cases()
mainloop()