def test_draw_basic_union(self): """Test union in basic.py.""" first = basic.rectangle(0.5, 1.5, 2.1, 3.2) second = basic.rectangle(0.1, 1.3, 1.9, 2.2) expected = [[(1.85, 2.45), (2.35, 2.45), (2.35, 3.95), (1.85, 3.95), (1.85, 2.45)], [(1.85, 2.85), (1.85, 3.95), (2.35, 3.95), (2.35, 2.45), (1.95, 2.45), (1.95, 1.55), (1.85, 1.55), (1.85, 2.85), (1.85, 2.85)]] union_results_1 = basic.union(first) union_results_2 = basic.union(first, second) actual = [ list(union_results_1.exterior.coords), list(union_results_2.exterior.coords) ] for x in range(2): self.assertEqual(len(actual[x]), len(expected[x])) # the shape resulting from a union could use as origin point any of the vertices. for idx, point in enumerate(expected[x]): if actual[x][0] == point: offset = idx my_range = len(actual[x]) - 1 #last element repeats first. Ignore. for i in range(my_range): exp_i = (i + offset) % (my_range) for j in range(2): self.assertAlmostEqualRel(actual[x][i][j], expected[x][exp_i][j], rel_tol=1e-3)
def test_draw_basic_union(self): """ Test union in basic.py. """ first = basic.rectangle(0.5, 1.5, 2.1, 3.2) second = basic.rectangle(0.1, 1.3, 1.9, 2.2) expected = [[(1.85, 2.45), (2.35, 2.45), (2.35, 3.95), (1.85, 3.95), (1.85, 2.45)], [(1.85, 2.85), (1.85, 3.95), (2.35, 3.95), (2.35, 2.45), (1.95, 2.45), (1.95, 1.55), (1.85, 1.55), (1.85, 2.85), (1.85, 2.85)]] union_results_1 = basic.union(first) union_results_2 = basic.union(first, second) actual = [ list(union_results_1.exterior.coords), list(union_results_2.exterior.coords) ] for x in range(2): self.assertEqual(len(actual[x]), len(expected[x])) for i in range(len(actual[x])): for j in range(2): self.assertAlmostEqualRel(actual[x][i][j], expected[x][i][j], rel_tol=1e-3)
def test_draw_basic_subtract(self): """Test subtract in basic.py.""" first = basic.rectangle(0.5, 1.5, 2.1, 3.2) second = basic.rectangle(0.1, 1.3, 1.9, 2.2) subtract_results = basic.subtract(first, second) actual_subtract = list(subtract_results.exterior.coords) expected_subtract = [(1.85, 2.85), (1.85, 3.95), (2.35, 3.95), (2.35, 2.45), (1.95, 2.45), (1.95, 2.85), (1.85, 2.85)] self.assertEqual(len(actual_subtract), len(expected_subtract)) # the shape resulting from a subtract could use as origin point any of the vertices. for idx, point in enumerate(expected_subtract): if actual_subtract[0] == point: offset = idx my_range = len(actual_subtract ) - 1 #first and last elements are the same. Ignore. for i in range(my_range): exp_i = (i + offset) % (my_range) for j in range(2): self.assertEqual(actual_subtract[i][j], expected_subtract[exp_i][j])
def test_draw_basic_is_rectangle(self): """Test is_rectangle in basic.py.""" my_rectangle = basic.rectangle(0.5, 1.5, 2.1, 3.2) my_integer = 7 self.assertEqual(basic.is_rectangle(my_rectangle), True) self.assertEqual(basic.is_rectangle(my_integer), False)
def test_draw_basic_subtract(self): """Test subtract in basic.py.""" first = basic.rectangle(0.5, 1.5, 2.1, 3.2) second = basic.rectangle(0.1, 1.3, 1.9, 2.2) subtract_results = basic.subtract(first, second) actual_subtract = list(subtract_results.exterior.coords) expected_subtract = [(1.85, 2.85), (1.85, 3.95), (2.35, 3.95), (2.35, 2.45), (1.95, 2.45), (1.95, 2.85), (1.85, 2.85)] self.assertEqual(len(actual_subtract), len(expected_subtract)) my_range = len(actual_subtract) for i in range(my_range): for j in range(2): self.assertEqual(actual_subtract[i][j], expected_subtract[i][j])
def test_draw_basic_rectangle(self): """Test rectangle in basic.py.""" polygon_actual = basic.rectangle(0.5, 1.5, 2.1, 3.2) polygon_expected = Polygon([[1.85, 2.45], [2.35, 2.45], [2.35, 3.95], [1.85, 3.95], [1.85, 2.45]]) coords_actual = list(polygon_actual.exterior.coords) coords_expected = list(polygon_expected.exterior.coords) self.assertEqual(len(coords_actual), len(coords_expected)) my_range = len(coords_actual) for i in range(my_range): for j in range(2): self.assertEqual(coords_actual[i][j], coords_expected[i][j])
def test_draw_basic_is_rectangle(self): """Test is_rectangle in basic.py.""" my_rectangle = basic.rectangle(0.5, 1.5, 2.1, 3.2) self.assertTrue(basic.is_rectangle(my_rectangle)) self.assertFalse(basic.is_rectangle(7))