def test_expanded_to_expands_only_x_axis_if_applicable(self): """ We want it to keep as much of the original coordinates as possible """ other_rect = Rectangle(top_left=Point(6, 11), bottom_right=Point(7, 4)) new_rect = RectangleResizer.rectangle_expanded_to( other_rect, self.rectangle) self.assertEqual(new_rect.top_left.y, 11) self.assertEqual(new_rect.bottom_right.y, 4)
def test_expanded_to_doesnt_modify_original_rectangles(self): other_rect = Rectangle(Point(11, 9), Point(15, 7)) orig_rect = deepcopy(self.rectangle) orig_other_rect = deepcopy(other_rect) new_rect = RectangleResizer.rectangle_expanded_to( other_rect, self.rectangle) self.assertEqual(orig_rect, self.rectangle) self.assertEqual(orig_other_rect, other_rect)
def test_expanded_to_expands_rectangle_and_retains_top_left_point_if_it_already_covers( self): expected_tl = Point(9, 11) other_rect = Rectangle(top_left=Point(9, 11), bottom_right=Point(19, 9)) new_rect = RectangleResizer.rectangle_expanded_to( other_rect, self.rectangle) self.assertTrue(new_rect.is_bounding(self.rectangle)) self.assertEqual(expected_tl, new_rect.top_left)
def find_min_expansion_node(rt_nodes: ['RTreeNode'], entry: Entry) -> 'RTreeNode': """ Given N RTreeNodes and one Entry, find the RTreeNode which requires the least expansion to accommodate the Entry """ expansions = [] for node in rt_nodes: if node.mbr.is_bounding(entry.mbr): expanded = 0 else: expanded = RectangleResizer.rectangle_expanded_to(node.mbr, entry.mbr).area - node.mbr.area expansions.append({'node': node, 'expanded': expanded}) return min(expansions, key=lambda x: x['expanded'])['node']
def test_expanded_to_raises_error_if_rectangle_already_contains_other_rect( self): other_rect = Rectangle(top_left=Point(9, 11), bottom_right=Point(21, 4)) with self.assertRaises(RectangleResizer.ResizeError): RectangleResizer.rectangle_expanded_to(other_rect, self.rectangle)
def test_expanded_to_expands_rectangle(self): other_rect = Rectangle(Point(11, 9), Point(15, 7)) new_rect = RectangleResizer.rectangle_expanded_to( other_rect, self.rectangle) self.assertTrue(new_rect.is_bounding(self.rectangle))