def test_get_intersection_area_same_as_piece_if_fully_submerged(self): board = Board(Polygon([[0, 0], [0, 1000], [1000, 1000], [1000, 0]]), [0, 0]) piece = PolygonPiece(Polygon([[0, 0], [100, 100], [100, 0]]), [50, 50]) actual_area = PolygonIntersector().intersection_area( board.get_points_in_plane(), piece.get_points_in_plane()) self.assertEqual(piece.get_polygon().area(), actual_area)
def test_intersection_half_of_square(self): board = Board(Polygon([[0, 0], [100, 100], [100, 0]]), [0, 0]) piece = PolygonPiece(Polygon([[0, 0], [0, 100], [100, 100], [100, 0]]), [0, 0]) actual_area = PolygonIntersector().intersection_area( board.get_points_in_plane(), piece.get_points_in_plane()) self.assertEqual(piece.get_polygon().area() / 2, actual_area)
def test_get_intersection_zero_if_poly_on_a_different_planet(self): board = Board(Polygon([[0, 0], [0, 1000], [1000, 0], [1000, 1000]]), [0, 0]) piece = PolygonPiece(Polygon([[0, 0], [100, 100], [100, 0]]), [5000, 5000]) actual_area = PolygonIntersector().intersection_area( board.get_points_in_plane(), piece.get_points_in_plane()) self.assertEqual(0, actual_area)
def test_get_center_distance_from_doesnt_return_0_if_centroid_in_different_point( self): polygon = Polygon([[0, 0], [3, 3], [0, 3]]) polygon.get_centroid_point = lambda: [51, 33] piece = PolygonPiece(polygon, [0, 0]) distance = piece.get_center_distance_from(1337, 1337) self.assertNotEqual(0, distance[0]) self.assertNotEqual(0, distance[1])
def test_get_pieces_returns_pieces(self): board = Board(Polygon([[0, 0], [100, 100], [100, 0]]), [0, 0]) pieces = [ PolygonPiece(Polygon(random_polygon_points(1)[0]), [0, 0]), PolygonPiece(Polygon(random_polygon_points(1)[0]), [0, 0]), PolygonPiece(Polygon(random_polygon_points(1)[0]), [0, 0]) ] intersector = PolygonIntersector() level = Level(board, pieces, intersector) self.assertEqual(pieces, level.get_pieces())
def test_get_center_distance_from_returns_0_if_centroid_in_same_point( self): polygon = Polygon([[0, 0], [3, 3], [0, 3]]) polygon.get_centroid_point = lambda: [1337, 1337] piece = PolygonPiece(polygon, [0, 0]) distance = piece.get_center_distance_from(1337, 1337) self.assertEqual(0, distance[0]) self.assertEqual(0, distance[1])
def test_two_fully_overlapping_pieces_serve_same_board_coverage( self, *points): piece = PolygonPiece(Polygon(list(points)), [0, 0]) board = Board(Polygon([[0, 0], [100, 100], [100, 0]]), [0, 0]) level = Level(board, [piece], PolygonIntersector()) coverage_with_one_piece = level.get_completion_percentage() level = Level(board, [piece, piece], PolygonIntersector()) coverage_with_two_pieces = level.get_completion_percentage() self.assertAlmostEqual(coverage_with_one_piece, coverage_with_two_pieces, delta=1)
def test_get_distance_from_returns_positive_vals(self): polygon = Polygon([[0, 0], [3, 3], [0, 3]]) piece1 = PolygonPiece(polygon, [1233, 12312]) self.assertTrue( all(a > 0 for a in piece1.get_center_distance_from(0, 0))) piece2 = PolygonPiece(polygon, [-1233, -12312]) self.assertTrue( all(a > 0 for a in piece2.get_center_distance_from(0, 0)))
def test_get_center_distance_from_is_affected_by_polygon_piece_position( self): polygon = Polygon([[0, 0], [3, 3], [0, 3]]) piece1 = PolygonPiece(polygon, [0, 0]) piece2 = PolygonPiece(polygon, [15, 66]) p1pts = piece1.get_center_distance_from(0, 0) p2pts = piece2.get_center_distance_from(0, 0) self.assertFalse(all(a == b for a, b in zip(p1pts, p2pts)))
def test_adding_a_piece_does_not_decrease_coverage(self, *points): piece = PolygonPiece(Polygon(list(points)), [0, 0]) piece1 = PolygonPiece(Polygon(random_polygon_points(1)[0]), [0, 0]) board = Board(Polygon([[0, 0], [100, 100], [100, 0]]), [0, 0]) intersector = PolygonIntersector() level = Level(board, [piece], intersector) coverage_with_one_piece = level.get_completion_percentage() level = Level(board, [piece, piece1], intersector) coverage_with_two_pieces = level.get_completion_percentage() if len( intersector.intersection_polygons( piece.get_points_in_plane(), piece1.get_points_in_plane())) == 0: self.assertGreaterEqual(coverage_with_two_pieces, coverage_with_one_piece) else: coverage_almost_equal = abs(coverage_with_two_pieces - coverage_with_one_piece) <= 1 two_piece_coverage_greater_equal = coverage_with_two_pieces >= coverage_with_one_piece self.assertTrue(two_piece_coverage_greater_equal or coverage_almost_equal)
def test_construct(self): PolygonPiece(Polygon([[0, 0], [0, 10], [10, 0], [10, 10]]), [0, 0])
def test_board_coverage_not_negative(self, *points): piece = PolygonPiece(Polygon(list(points)), random_position()) board = Board(Polygon([[0, 0], [100, 100], [100, 0]]), [0, 0]) level = Level(board, [piece], PolygonIntersector()) actual_coverage = level.get_completion_percentage() self.assertGreaterEqual(0, actual_coverage)
def test_board_coverage_with_piece_same_size_as_board_is_100_percent(self): board = Board(Polygon([[0, 0], [100, 100], [100, 0]]), [0, 0]) piece = PolygonPiece(Polygon([[0, 0], [100, 100], [100, 0]]), [0, 0]) level = Level(board, [piece], PolygonIntersector()) self.assertEqual(100, level.get_completion_percentage())
def test_polygon_used(self): polygon = Polygon([[0, 0], [0, 10], [10, 0], [10, 10]]) piece = PolygonPiece(polygon, [1, 2]) self.assertEqual(polygon, piece.get_polygon())
def test_points_same_as_poly_if_position_zero(self, *points): points = list(points) polygon = Polygon(points.copy()) piece = PolygonPiece(polygon, [0, 0]) self.assertEqual(polygon.get_points().tolist(), piece.get_points_in_plane().tolist())
def test_points_not_same_as_poly_if_position_not_zero(self, *position): position = list(position) polygon = Polygon([[0, 0], [0, 10], [10, 0], [10, 10]]) piece = PolygonPiece(polygon, position) self.assertNotEqual(polygon.get_points().tolist(), piece.get_points_in_plane().tolist())
def test_get_position_returns_value_passed_in_construct(self): position = [12, 543] piece = PolygonPiece(Polygon([[0, 0], [0, 10], [10, 0], [10, 10]]), position.copy()) self.assertEqual(position, piece.get_position())
def test_get_center_distance_from_returns_array_with_two_numbers(self): piece = PolygonPiece(Polygon([[0, 0], [3, 3], [0, 3]]), [125, 30]) distance = piece.get_center_distance_from(250, 300) self.assertEqual(2, len(distance))
def test_move_zeroes_doesnt_change_position(self): starting_position = [125, 123] piece = PolygonPiece(Polygon([[0, 0], [3, 3], [0, 3]]), starting_position.copy()) piece.move(0, 0) self.assertEqual(starting_position, piece.get_position())
def test_move_negative_amount_doesnt_increase_position(self, x, y): starting_position = [125, 123] piece = PolygonPiece(Polygon([[0, 0], [3, 3], [0, 3]]), starting_position.copy()) piece.move(x, y) self.assertLessEqual(piece.get_position(), starting_position)
def test_points_dont_increase_with_positive_position(self, *position): position = list(position) polygon = Polygon([[0, 0], [0, 10], [10, 0], [10, 10]]) piece = PolygonPiece(polygon, position) self.assertLessEqual(piece.get_points_in_plane().sum(), polygon.get_points().sum())