def initialize_rectangle(evt, hsm): x, y = evt.data bounds = (x, y, x + 1, y + 1) rect = Rectangle(*bounds) rect.canvas_id = canvas.create_rectangle(bounds, outline='blue') hsm.data.canvas_temp_data = (x, y, rect) quad.insert(rect) update_grid()
def test_overlap_each(self): # overlap at the corner where 4 rectangles meet for x in range(10, 1000, 10): for y in range(10, 1000, 10): exp = sorted([ Rectangle(x - 10, y - 10, x, y), Rectangle(x - 10, y, x, y + 10), Rectangle(x, y, x + 10, y + 10), Rectangle(x, y - 10, x + 10, y), ], key=key) got = sorted(self.overlap(x - 1, y - 1, x + 1, y + 1), key=key) assert got == exp
def setup_class(self): self.qt = Node(0, 0, 90, 90) # put a 9x9 tile on every 3 horizontal and vertical units [ self.qt.insert(Rectangle(x, y, x + 9, y + 9)) for x in range(0, 80, 3) for y in range(0, 80, 3) ] assert len(self.qt.get_children()) == 27 * 27
def setup_class(self): self.qt = Node(0, 0, 1000, 1000) # fill with 10x10 squares [ self.qt.insert(Rectangle(x, y, x + 10, y + 10)) for x in range(0, 1000, 10) for y in range(0, 1000, 10) ] assert len(self.qt.get_children()) == 10000
def test_item_crossing_border(self, max_depth, coords): # adding item that cross borders shouldn't trigger nested subdivisions qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*coords) qt.insert(rect) assert qt.get_children() == [rect] assert qt._get_depth() == 1 assert qt._get_number_of_nodes() == 5
def test_insert_two_at_different_locations(self): qt = Node(0, 0, 1000, 1000) rect1 = Rectangle(10, 10, 20, 20) rect2 = Rectangle(800, 800, 900, 900) qt.insert(rect1) qt.insert(rect2) assert qt.get_children() == [rect1, rect2] qt.remove(rect1) assert qt.get_children() == [rect2] qt.insert(rect1) assert qt.get_children() == [rect1, rect2] qt.remove(rect2) assert qt.get_children() == [rect1] qt.remove(rect1) assert qt.get_children() == [] assert qt.direct_children == []
def test_insert_small_rect_in_corner(self, max_depth, coords): # inserting small rectangle in top left corner should make the # quadtree perform maximum number of subdivisions qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*coords) qt.insert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 qt.remove(rect) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1
def test_reinsert_into_another_quadrant(self, max_depth): bounds_1 = (0.01, 0.01, 0.02, 0.02) bounds_2 = (1023.998, 1023.998, 1023.999, 1023.999) qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*bounds_1) qt.insert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 # make sure that top-left quadrant is the only one subdivided depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [max_depth - 1, 0, 0, 0] # change coords and reinsert rect.bounds = bounds_2 qt.reinsert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 # make sure that bottom-right quadrant is the only one subdivided depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [0, 0, 0, max_depth - 1]
def test_reinsert_into_same_quadrant(self, max_depth): bounds = (0.001, 0.001, 0.002, 0.002) qt = Node(0, 0, 1024, 1024, max_depth) assert qt._get_depth() == 0 assert qt._get_number_of_nodes() == 1 rect = Rectangle(*bounds) qt.insert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 # make sure that top-left quadrant is the only one subdivided depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [max_depth - 1, 0, 0, 0] # change coords and reinsert into same place qt.reinsert(rect) assert qt._get_depth() == max_depth assert qt._get_number_of_nodes() == 1 + max_depth * 4 depths = [qt.quadrants[i]._get_depth() for i in range(4)] assert depths == [max_depth - 1, 0, 0, 0]
def test_enclose_some(self): got = sorted(self.enclose(377, 499, 401, 601), key=key) exp = sorted([ Rectangle(380, 500, 390, 510), Rectangle(390, 500, 400, 510), Rectangle(380, 510, 390, 520), Rectangle(390, 510, 400, 520), Rectangle(380, 520, 390, 530), Rectangle(390, 520, 400, 530), Rectangle(380, 530, 390, 540), Rectangle(390, 530, 400, 540), Rectangle(380, 540, 390, 550), Rectangle(390, 540, 400, 550), Rectangle(380, 550, 390, 560), Rectangle(390, 550, 400, 560), Rectangle(380, 560, 390, 570), Rectangle(390, 560, 400, 570), Rectangle(380, 570, 390, 580), Rectangle(390, 570, 400, 580), Rectangle(380, 580, 390, 590), Rectangle(390, 580, 400, 590), Rectangle(380, 590, 390, 600), Rectangle(390, 590, 400, 600), ], key=key) assert got == exp
def test_enclose_each(self): for x in range(0, 1000, 10): for y in range(0, 1000, 10): exp = Rectangle(x, y, x + 10, y + 10) assert self.enclose(x - 1, y - 1, x + 11, y + 11) == [exp]
def test_under_point_each(self): for x in range(10, 1000, 10): for y in range(10, 1000, 10): exp = [Rectangle(x, y, x + 10, y + 10)] got = self.under(x + randint(1, 9), y + randint(1, 9)) assert got == exp
def __init__(self, a, b, c, d): Rectangle.__init__(self, a, b, c, d) self.data = None
def setup_class(self): self.qt = Node(0, 0, 1000, 1000) self.rect = Rectangle(20, 20, 40, 40)
def test_overlap_some(self): got = sorted(self.overlap(387, 501, 391, 591), key=key) exp = sorted([ Rectangle(380, 500, 390, 510), Rectangle(390, 500, 400, 510), Rectangle(380, 510, 390, 520), Rectangle(390, 510, 400, 520), Rectangle(380, 520, 390, 530), Rectangle(390, 520, 400, 530), Rectangle(380, 530, 390, 540), Rectangle(390, 530, 400, 540), Rectangle(380, 540, 390, 550), Rectangle(390, 540, 400, 550), Rectangle(380, 550, 390, 560), Rectangle(390, 550, 400, 560), Rectangle(380, 560, 390, 570), Rectangle(390, 560, 400, 570), Rectangle(380, 570, 390, 580), Rectangle(390, 570, 400, 580), Rectangle(380, 580, 390, 590), Rectangle(390, 580, 400, 590), Rectangle(380, 590, 390, 600), Rectangle(390, 590, 400, 600), ], key=key) assert got == exp