-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadtree.py
341 lines (295 loc) · 12.4 KB
/
quadtree.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import math
import copy
import pygame
import numpy as np
class QuadTree(object):
"""An implementation of a quad-tree.
Acknowledgements:
[1] http://mu.arete.cc/pcr/syntax/quadtree/1/quadtree.py
"""
def __init__(self, items, depth, bounding_rect=None):
"""Creates a quad-tree.
@param items:
A sequence of items to store in the quad-tree.
@param depth:
The maximum recursion depth.
@param bounding_rect:
The bounding rectangle of all of the items in the quad-tree. For
internal use only.
"""
# The sub-quadrants are empty to start with.
self.nw = self.ne = self.se = self.sw = None
# Find this quadrant's centre.
if bounding_rect:
l, t, r, b = bounding_rect
else:
# If there isn't a bounding rect, then calculate it from the items.
l = min(item.X for item in items)
t = min(item.Y for item in items)
r = max(item.X for item in items)
b = max(item.Y for item in items)
cx = (l + r) * 0.5
cy = (t + b) * 0.5
self.vert_start_pos = (cx, cy + 0.5*(b - t))
self.vert_end_pos = (cx, cy - 0.5*(b - t))
self.horz_start_pos = (cx + 0.5*(r-l), cy)
self.horz_end_pos = (cx - 0.5*(r-l), cy)
# If we've reached the maximum depth then insert all items into this
# quadrant.
depth -= 1
if depth == 0:
self.items = items
return
self.items = []
nw_items = []
ne_items = []
se_items = []
sw_items = []
for item in items:
# Which of the sub-quadrants does the item overlap?
in_nw = item.X <= cx and item.Y <= cy
in_sw = item.X <= cx and item.Y >= cy
in_ne = item.X >= cx and item.Y <= cy
in_se = item.X >= cx and item.Y >= cy
# If it overlaps all 4 quadrants then insert it at the current
# depth, otherwise append it to a list to be inserted under every
# quadrant that it overlaps.
if in_nw and in_ne and in_se and in_sw:
self.items.append(item)
else:
if in_nw:
nw_items.append(item)
if in_ne:
ne_items.append(item)
if in_se:
se_items.append(item)
if in_sw:
sw_items.append(item)
# Create the sub-quadrants, recursively.
if nw_items:
self.nw = QuadTree(nw_items, depth, (l, t, cx, cy))
if ne_items:
self.ne = QuadTree(ne_items, depth, (cx, t, r, cy))
if se_items:
self.se = QuadTree(se_items, depth, (cx, cy, r, b))
if sw_items:
self.sw = QuadTree(sw_items, depth, (l, cy, cx, b))
def plotTree(self, node):
drawSquare(node, (0, 0, 255))
if node.ne is not None:
self.plotTree(node.ne)
if node.nw is not None:
self.plotTree(node.nw)
if node.se is not None:
self.plotTree(node.se)
if node.sw is not None:
self.plotTree(node.sw)
def rectangle(width, height, center, theta, WIDTH, HEIGHT):
points = []
for i in np.linspace(0, 2*math.pi, 300):
x = width/2*(abs(np.cos(i))*np.cos(i) + abs(np.sin(i))*np.sin(i))
y = height/2*(abs(np.cos(i))*np.cos(i) - abs(np.sin(i))*np.sin(i))
rot = np.array([[np.cos(theta), np.sin(theta)],
[-np.sin(theta), np.cos(theta)]])
rotated = np.dot(rot, np.array([[x], [y]]))
rotated[0] += center[0]
rotated[1] += center[1]
if (rotated[0] > 0) and (rotated[0] < WIDTH) and\
(rotated[1] > 0) and (rotated[1] < HEIGHT):
points.append(Point(rotated[0], rotated[1]))
return points
# The class that we're storing in the quad-tree. It possesses the necessary
# attributes of left, top, right and bottom, and it is hashable.
class Point(object):
def __init__(self, X, Y):
self.X = X
self.Y = Y
def draw(self):
pygame.draw.circle(screen, (255, 255, 255), [self.X, self.Y], 0)
def drawSquare(node, color):
pygame.draw.line(screen, color, node.vert_start_pos, node.vert_end_pos)
pygame.draw.line(screen, color, node.horz_start_pos, node.horz_end_pos)
def drawRecursive(node, color):
drawSquare(node, color)
if node.ne is not None:
drawRecursive(node.ne, color)
if node.nw is not None:
drawRecursive(node.nw, color)
if node.se is not None:
drawRecursive(node.se, color)
if node.sw is not None:
drawRecursive(node.sw, color)
def compareTreesRecursive(tree1, tree2):
# see if any nodes were removed or added
if tree1.ne is not None and tree2.ne is None:
drawRecursive(tree1.ne, (255, 0, 0))
elif tree1.ne is None and tree2.ne is not None:
drawRecursive(tree2.ne, (0, 255, 0))
elif tree1.ne is not None and tree2.ne is not None:
compareTreesRecursive(tree1.ne, tree2.ne)
if tree1.nw is not None and tree2.nw is None:
drawRecursive(tree1.nw, (255, 0, 0))
elif tree1.nw is None and tree2.nw is not None:
drawRecursive(tree2.nw, (0, 255, 0))
elif tree1.nw is not None and tree2.nw is not None:
compareTreesRecursive(tree1.nw, tree2.nw)
if tree1.se is not None and tree2.se is None:
drawRecursive(tree1.se, (255, 0, 0))
elif tree1.se is None and tree2.se is not None:
drawRecursive(tree2.se, (0, 255, 0))
elif tree1.se is not None and tree2.se is not None:
compareTreesRecursive(tree1.se, tree2.se)
if tree1.sw is not None and tree2.sw is None:
drawRecursive(tree1.sw, (255, 0, 0))
elif tree1.sw is None and tree2.sw is not None:
drawRecursive(tree2.sw, (0, 255, 0))
elif tree1.sw is not None and tree2.sw is not None:
compareTreesRecursive(tree1.sw, tree2.sw)
def drawLeaf(node, color):
nodeIsEmpty = True
if node.ne is not None:
drawLeaf(node.ne, color)
nodeIsEmpty = False
if node.nw is not None:
drawLeaf(node.nw, color)
nodeIsEmpty = False
if node.se is not None:
drawLeaf(node.se, color)
nodeIsEmpty = False
if node.sw is not None:
drawLeaf(node.sw, color)
nodeIsEmpty = False
if nodeIsEmpty:
drawSquare(node, color)
def compareTreeLeaves(tree1, tree2):
# see if any nodes were removed or added
if tree1.ne is not None and tree2.ne is None:
drawLeaf(tree1.ne, (255, 0, 0))
elif tree1.ne is None and tree2.ne is not None:
drawLeaf(tree2.ne, (0, 255, 0))
elif tree1.ne is not None and tree2.ne is not None:
compareTreeLeaves(tree1.ne, tree2.ne)
if tree1.nw is not None and tree2.nw is None:
drawLeaf(tree1.nw, (255, 0, 0))
elif tree1.nw is None and tree2.nw is not None:
drawLeaf(tree2.nw, (0, 255, 0))
elif tree1.nw is not None and tree2.nw is not None:
compareTreeLeaves(tree1.nw, tree2.nw)
if tree1.se is not None and tree2.se is None:
drawLeaf(tree1.se, (255, 0, 0))
elif tree1.se is None and tree2.se is not None:
drawLeaf(tree2.se, (0, 255, 0))
elif tree1.se is not None and tree2.se is not None:
compareTreeLeaves(tree1.se, tree2.se)
if tree1.sw is not None and tree2.sw is None:
drawLeaf(tree1.sw, (255, 0, 0))
elif tree1.sw is None and tree2.sw is not None:
drawLeaf(tree2.sw, (0, 255, 0))
elif tree1.sw is not None and tree2.sw is not None:
compareTreeLeaves(tree1.sw, tree2.sw)
def plotShape(shape):
for item in shape:
item.draw()
if __name__ == '__main__':
print("Move shape with directional arrows\n\
Rotate shape:\nZ counterclockwise\nX clockwise\n\n\
Resize shape:\nA narrower\nD wider \nW taller\nS shorter\n\n\
Change tree depth:\n+ Increase\n- Decrease\n\n\
Q Hide/Show Quadtree\n\
E Hide/Show Changetree\n\
C Hide/Show Points\n\
R Display Changetree recursively or just the leaf nodes")
HEIGHT = 900 # HEIGHT of quadtree
WIDTH = HEIGHT # WIDTH of quadtree
tree_depth = 9
width = 50 # width of rectangle
height = 100 # height of rectangle
angle = 0 # rotation of rectangle
cent = np.array([WIDTH/2.0, HEIGHT/2.0])
shape = rectangle(width, height, (cent), angle, WIDTH, HEIGHT)
screen = pygame.display.set_mode((int(WIDTH), int(HEIGHT)),
pygame.DOUBLEBUF)
screen.fill((0, 0, 0))
# Put them into a quad-tree.
tree = QuadTree(shape, tree_depth, (0, 0, WIDTH, HEIGHT))
tree.plotTree(tree)
plotShape(shape)
pygame.display.flip()
pygame.key.set_repeat(100)
stop = False
showPoints = True
showQuadtree = True
showChangetree = True
doRecursiveChangeTreeDisp = False
while not stop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
stop = True
elif event.type == pygame.KEYDOWN:
screen.fill((0, 0, 0))
items = []
if event.key == pygame.K_ESCAPE:
stop = True
elif event.key == pygame.K_UP:
cent[1] -= 10
print("center = " + repr(cent))
elif event.key == pygame.K_DOWN:
cent[1] += 10
print("center = " + repr(cent))
elif event.key == pygame.K_RIGHT:
cent[0] += 10
print("center = " + repr(cent))
elif event.key == pygame.K_LEFT:
cent[0] -= 10
print("center = " + repr(cent))
elif event.key == pygame.K_a:
width -= 10
print("size = " + repr(height) + " x " + repr(width))
elif event.key == pygame.K_d:
width += 10
print("size = " + repr(height) + " x " + repr(width))
elif event.key == pygame.K_w:
height += 10
print("size = " + repr(height) + " x " + repr(width))
elif event.key == pygame.K_s:
height -= 10
print("size = " + repr(height) + " x " + repr(width))
elif event.key == pygame.K_KP_PLUS:
tree_depth += 1
print("depth = " + repr(tree_depth))
elif event.key == pygame.K_KP_MINUS:
if tree_depth > 1:
tree_depth -= 1
print("depth = " + repr(tree_depth))
elif event.key == pygame.K_z:
angle += 0.05
print("angle = " + repr(angle))
elif event.key == pygame.K_x:
angle -= 0.05
print("angle = " + repr(angle))
elif event.key == pygame.K_q:
showQuadtree = not showQuadtree
print("Toggled display of quadtree")
elif event.key == pygame.K_e:
showChangetree = not showChangetree
print("Toggled display of changetree")
elif event.key == pygame.K_c:
showPoints = not showPoints
print("Toggled display of points")
elif event.key == pygame.K_r:
doRecursiveChangeTreeDisp = not doRecursiveChangeTreeDisp
print("Toggled how change tree is displayed")
shape = rectangle(width, height, cent, angle, WIDTH, HEIGHT)
oldTree = copy.copy(tree)
tree = QuadTree(shape, tree_depth, (0, 0, WIDTH, HEIGHT))
if showQuadtree:
tree.plotTree(tree)
if showChangetree:
if doRecursiveChangeTreeDisp:
compareTreesRecursive(oldTree, tree)
else:
compareTreeLeaves(oldTree, tree)
if showPoints:
plotShape(shape)
pygame.display.flip()
pygame.quit()