Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SpecialQuadtree():
- def insert(self, point,associated_data):
- cell = self.putIntoQuadtree( point, associated_data )
- min_depth = associated_data.getMinDepth()
- while cell.getDepth() < min_depth:
- cell.subdivide()
- self.enforceInvariant()
- def enforceInvariant(self):
- """Ensure that every cell has no more than two neighbors along any edge"""
- changed = True
- while changed:
- changed = False
- for cell in self.quadtree:
- if (
- len( cell.neighborsToLeft() ) > 2 or
- len( cell.neighborsToRight() ) > 2 or
- len( cell.neighborsToBottom() ) > 2 or
- len( cell.neighborsToTop() ) > 2
- ):
- cell.subdivide()
- changed = True
- def my_code():
- quadtree = SpecialQuadtree()
- for i in range(1000000):
- point = generate_random_point()
- associated_data = long_computation( point )
- quadtree.insert(point,associated_data)
- while quadtree.hasEmptyCells():
- empty_cell = quadtree.getAnyEmptyCell()
- point = generate_random_point_inside( empty_cell.getBounds() )
- associated_data = long_computation( point )
- quadtree.insert(point,associated_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement