示例#1
0
    def test_simplification(self):

        # Points are clockwise oriented
        points = Vector2D.from_coordinates(
            (  0.1, 10.1), # 0
            ( -0.1, 10.9), # 1
            (  0.1, 20.1), # 2
            (  0  , 30  ), # 3 *
            ( 15.1, 30.1), # 4
            ( 30  , 30  ), # 5 *
            ( 29.9, 10.0), # 6
            ( 30  ,  0  ), # 7 *
            ( 15.1,  0.1), # 8
            (  0  ,  0  ), # 9 *
        )
        polygon = Polygon2D(*points)
        simplified_polygon = polygon.simplify(threshold=1.)
        simplified_polygon_truth = Polygon2D(*[points[i] for i in (3, 5, 7, 9)])
        self.assertEqual(simplified_polygon, simplified_polygon_truth)

        points = Vector2D.from_coordinates(
            (  0  ,  0  ),
            (  0.1, 10.1),
            (  0.2, 20.1),
        )
        polygon = Polygon2D(*points)
        simplified_polygon = polygon.simplify(threshold=1.)
        self.assertIsNone(simplified_polygon)
示例#2
0
    def test_convex_hull(self):

        # 6           *
        # 5     *
        # 4       +
        # 3   +     +   +   *
        # 2 *       +       *
        # 1       *       *
        # 0   *
        #   - 0 1 2 3 4 5 6 7

        # Points are clockwise oriented
        points = Vector2D.from_coordinates(
            (0, 0),  #  0 p0
            (-1, 2), #  1 *
            (0, 3),  #  2
            (2, 4),  #  3
            (1, 5),  #  4 *
            (4, 6),  #  5 *
            (3, 3),  #  6
            (5, 3),  #  7
            (7, 3),  #  8 *
            (7, 2),  #  9 *
            (6, 1),  # 10 *
            (3, 2),  # 11
            (2, 1),  # 12
        )

        convex_hull_points = [points[i] for i in (0, 10, 9, 8, 5, 4, 1)] # ccw

        polygon = Polygon2D(*points)
        convex_hull_truth = Polygon2D(*convex_hull_points)

        self.assertTrue(polygon.is_simple)
        self.assertFalse(polygon.is_convex)
        self.assertTrue(polygon.is_concave)
        self.assertTrue(polygon.is_clockwise)
        self.assertFalse(polygon.is_counterclockwise)

        convex_hull = polygon.convex_hull()
        self.assertListEqual(list(convex_hull.points), convex_hull_points)
        self.assertEqual(convex_hull, convex_hull_truth)
        self.assertFalse(convex_hull.is_clockwise)
        self.assertTrue(convex_hull.is_counterclockwise)

        reversed_polygon = Polygon2D(*reversed(points))
        self.assertFalse(reversed_polygon.is_clockwise)
        self.assertTrue(reversed_polygon.is_counterclockwise)
        convex_hull2 = reversed_polygon.convex_hull()
        self.assertEqual(convex_hull, convex_hull2)
        self.assertFalse(convex_hull2.is_clockwise)
        self.assertTrue(convex_hull2.is_counterclockwise)

        # Simplify
        simplified_polygon = convex_hull.simplify(threshold=.1)
        self.assertEqual(simplified_polygon, convex_hull_truth)
示例#3
0
    def _make_polygon(self):

        points = Vector2D.from_coordinates(
            (0, 0),
            (-1, 2),
            (0, 3),
            (2, 4),
            (1, 5),
            (4, 6),
            (3, 3),
            (5, 3),
            (7, 3),
            (7, 2),
            (6, 1),
            (3, 2),
            (2, 1),
        )

        polygon = Polygon2D(*points)
        return polygon