Sub topic 8
Inserting Points and Subdividing Full Cells
This chapter assumes you know recursion, Big-O notation, and basic Python classes and tuples. It also assumes you have read the previous chapter on quadtree structure. Here you will implement insertion for a capacity-based point quadtree and see when a cell splits.
The insertion rule
Each quadtree node covers a square cell. A leaf stores up to capacity points. An internal node stores no points and holds four children, one per quadrant. Insertion follows three rules:
- If the point lies outside the cell, reject it.
- If the node is a leaf with room, store the point there.
- If the leaf is full, subdivide it into four quadrants, move its points into them, and then insert the new point into the matching child.
The code
The cell is the half-open square [cx-half, cx+half) x [cy-half, cy+half). Half-open means a point on a shared edge belongs to exactly one cell.
class Quadtree:
def __init__(self, cx, cy, half, capacity=2, max_depth=8):
self.cx, self.cy, self.half = cx, cy, half
self.capacity = capacity
self.max_depth = max_depth
self.points = []
self.children = None
def contains(self, p):
return (self.cx - self.half <= p[0] < self.cx + self.half and
self.cy - self.half <= p[1] < self.cy + self.half)
def _subdivide(self):
h = self.half / 2
self.children = [
Quadtree(self.cx + dx * h, self.cy + dy * h, h,
self.capacity, self.max_depth - 1)
for dx in (-1, 1) for dy in (-1, 1)
]
old, self.points = self.points, []
for p in old:
any(c.insert(p) for c in self.children)
def insert(self, p):
if not self.contains(p):
return False
if self.children is None:
if len(self.points) < self.capacity or self.max_depth == 0:
self.points.append(p)
return True
self._subdivide()
return any(c.insert(p) for c in self.children)
def leaves(node):
if node.children is None:
return [node]
return [leaf for c in node.children for leaf in leaves(c)]
qt = Quadtree(50, 50, 50)
for p in [(10, 10), (12, 11), (80, 80)]:
qt.insert(p)
print(len(leaves(qt))) # 4
print([len(l.points) for l in leaves(qt)]) # [2, 0, 0, 1]The first two points fit in the root. The third finds the root full, so the root splits into four children of half-size 25. The two old points move into the lower-left child, and (80, 80) goes into the upper-right child. The root now holds no points.
Details that matter
- Children order. The list comprehension produces centers in the order (low x, low y), (low x, high y), (high x, low y), (high x, high y). The order is arbitrary, but queries must not depend on it.
anyand short-circuiting.anystops at the first child that accepts the point. This is correct because the half-open cells never overlap, so at most one child accepts.- Cascading splits. Subdividing does not guarantee the points separate. If all of them land in one child, that child is full too, and the next insert there splits it again. Tightly clustered points can produce deep, narrow branches.
max_depth. Duplicate points can never be separated, so without a depth limit they would cause infinite subdivision. At depth 0 a leaf accepts points beyond its capacity.- Outside points.
insertreturnsFalsefor points outside the root square. Check the return value, or the point is silently lost.
Exercise
Create Quadtree(50, 50, 50) with capacity 2 and insert (10, 10), (12, 11), (11, 12) in that order. Does the root subdivide? Then insert (13, 9). Does the root subdivide now, and why or why not?
Show answer
After the third insert, the root is full (two points), so it subdivides into four children of half-size 25. All three points fall in the lower-left child, which holds two, so it subdivides as well and the cascade continues until the points separate or max depth is reached. Inserting (13, 9) later does not split the root again, because the root is already an internal node. The point is routed to the child covering [0, 50) x [0, 50), and from there to the leaf cell covering it, which may split if full.