示例#1
0
    def testMinTriangle(self):
        points = randomConvexPolygon(10, k=20).points
        interior = minTriangle(points)
        exterior = boundingTriangle(points)

        # Plot points
        plotPoints(points, style='ro')

        # Plot bounding triangle
        plot(interior, style='go-')
        show(exterior, style='bo-')
示例#2
0
    def testExterior(self):
        n = 100
        k = 1000
        poly = randomConvexPolygon(n, k=50)
        points = [poly.exteriorPoint() for i in range(k)]
        for point in points:
            self.assertTrue(not poly.contains(point))

        if self.ANIMATE:
            plot(poly)
            showPoints(points, style='ro')
示例#3
0
    def runLocator(self, regions):
        # Pre-process regions
        l = Locator(regions)

        if self.ANIMATE:
            show(regions)
            plot(l.boundary, style='g--')
            show(regions)

        # Ensure resulting DAG is acyclic
        self.assertTrue(l.dag.acyclic())

        n = 50
        # Ensure correctness
        for region in regions:
            # Test n random interior points per region
            for k in range(n):
                target = region.smartInteriorPoint()
                target_region = l.locate(target)
                self.assertEqual(region, target_region)
                self.assertTrue(target_region.contains(target))

            # Animate one interior point
            if self.ANIMATE:
                plot(l.regions)
                plot(target_region, style='ro-')
                showPoints(target, style='bo')

            # Test n random exterior points per region
            for k in range(n):
                target = region.exteriorPoint()
                target_region, is_valid = l.annotatedLocate(target)
                self.assertTrue(region != target_region)

                # Point may be outside all regions
                if not is_valid:
                    for region in regions:
                        self.assertTrue(not region.contains(target))

            # Animate one exterior point
            if self.ANIMATE and not is_valid and target_region:
                plot(l.regions)
                plot(target_region, style='ro--')
                showPoints(target, style='bx')
示例#4
0
            x = A.x + factor * bisector[0]
            y = A.y + factor * bisector[1]

            def absRound(n):
                if n < 0:
                    return floor(n)
                return ceil(n)

            x = absRound(x)
            y = absRound(y)

            return Point(x, y)

        def adjust(i):
            A = poly.points[i % poly.n]
            B = poly.points[(i - 1) % poly.n]
            C = poly.points[(i + 1) % poly.n]
            return bisect(A, B, C)

        expanded_points = [adjust(i) for i in range(poly.n)]
        return Polygon(expanded_points)

    return expand(minTriangle(Polygon(points)))

if __name__ == "__main__":
    poly = randomConvexPolygon(10)
    triangle = minTriangle(poly)
    plot(poly)
    show(triangle, style='r--')
示例#5
0
            x = A.x + factor * bisector[0]
            y = A.y + factor * bisector[1]

            def absRound(n):
                if n < 0:
                    return floor(n)
                return ceil(n)

            x = absRound(x)
            y = absRound(y)

            return Point(x, y)

        def adjust(i):
            A = poly.points[i % poly.n]
            B = poly.points[(i - 1) % poly.n]
            C = poly.points[(i + 1) % poly.n]
            return bisect(A, B, C)

        expanded_points = [adjust(i) for i in range(poly.n)]
        return Polygon(expanded_points)

    return expand(minTriangle(Polygon(points)))


if __name__ == "__main__":
    poly = randomConvexPolygon(10)
    triangle = minTriangle(poly)
    plot(poly)
    show(triangle, style='r--')