-
Notifications
You must be signed in to change notification settings - Fork 0
/
QTree.py
80 lines (62 loc) · 2.02 KB
/
QTree.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
from Misc import *
import pygame
class QTree:
max = 16
def __init__(self, rect = None):
self.children = []
self.predecessorNodes = []
self.expanded = False
if not isinstance(rect, pygame.Rect):
raise ValueError('Rect input to this QuadTree-implementation has to be a pygame.Rect')
self.rect = rect
def insert(child):
if not (self.contains(child)):
return
if(len(children) < max):
self.children.append(child)
return
else:
if not expanded:
self.expand()
for node in self.predecessorNodes:
node.insert(child)
def remove(possibleChild):
for child in children:
if(child == possibleChild):
self.children.remove(child)
return
for node in predecessorNodes:
node.remove(possibleChild)
def contains(child, rect = None):
return rect.contains(child.getRect())
def expand(self):
if self.expanded:
er("QTree-Node already expanded")
# Top-left
self.predecessorNodes[0] = QTree(pygame.Rect(self.rect.left, self.rect.top,
self.rect.width/2, self.rect.height/2))
# Top-right
self.predecessorNodes[1] = QTree(pygame.Rect(self.rect.left + self.rect.width/2,
self.rect.top, self.rect.width/2,
self.rect.height/2))
# Bottom-left
self.predecessorNodes[2] = QTree(pygame.Rect(self.rect.left, self.rect.top + self.rect.height/2,
self.rect.width/2, self.rect.height/2))
# Bottom-right
self.predecessorNodes[3] = QTree(pygame.Rect(self.rect.left + self.rect.width/2,
self.rect.top + self.rect.height/2,
self.rect.width/2, self.rect.height/2))
self.expanded = True
def getChildrenInRegion(region):
validChildren = []
for child in self.children:
if(region.contains(child.getRect())):
validChildren.append(child)
for node in predecessorNodes:
validChildren.extend(node.getChildrenInRegion(region))
def getAllChildren():
validChildren = []
validChildren.extend(self.children)
for node in predecessorNodes:
validChildren.extend(node.getAllChildren())
return validChildren