def test_tangents_containing_circle(self):
        "test circle tangents when one circle contains another"
        circle1 = Circle(Vec2D(), 10)
        circle2 = Circle(Vec2D(), 5)

        self.assertTrue(len(circle1.tangent_circle(circle2)) == 0)
        self.assertTrue(len(circle2.tangent_circle(circle1)) == 0)
    def test_circumference_simple(self):
        p1 = Circle()
        p2 = Circle(Vec2D(3, 0), 1)

        poly = Polygon([p1, p2])

        self.assertAlmostEqual(poly.circumference(), 6 + 2 * pi)
    def test_intersection_equal_circles(self):
        "test intersection of equal circles"

        circ1 = Circle()
        circ2 = Circle()

        self.assertTrue(circ1.intersects(circ2))
        self.assertTrue(circ2.intersects(circ1))
    def test_containment_equal_circles(self):
        "if two circles are equal one contains the other and vice versa"

        circ1 = Circle()
        circ2 = Circle()

        self.assertTrue(circ1.contains_circle(circ2))
        self.assertTrue(circ2.contains_circle(circ1))
    def test_circle_circle_intersection(self):
        "test intersection of intersecting circles"

        circ1 = Circle()
        circ2 = Circle(Vec2D(0.5, 0))

        self.assertTrue(circ1.intersects(circ2))
        self.assertTrue(circ2.intersects(circ1))
    def test_not_intersecting_circles(self):
        "test intersection of not intersecting circles"

        circ1 = Circle()
        circ2 = Circle(Vec2D(10, 0))

        self.assertFalse(circ1.intersects(circ2))
        self.assertFalse(circ2.intersects(circ1))
    def test_self_tangent(self):
        "test if tangent is part of circle"
        circle1 = Circle()
        circle2 = Circle(Vec2D(0, 2), 1)
        tan = Tangent(Vec2D(0, 1), circle1, None, Vec2D(0, 2), circle2, None)

        self.assertFalse(circle1.intersects_tangent(tan))
        self.assertFalse(circle2.intersects_tangent(tan))
    def test_touching_intersection(self):
        "test intersection of tangent circles"

        circ1 = Circle()
        circ2 = Circle(Vec2D(1, 0))

        self.assertTrue(circ1.intersects(circ2))
        self.assertTrue(circ2.intersects(circ1))
    def test_containing_circles(self):
        "test circle containment"

        circ1 = Circle()
        circ2 = Circle(Vec2D(), 0.5)

        self.assertTrue(circ1.contains_circle(circ2))
        self.assertFalse(circ2.contains_circle(circ1))
    def test_contains_impl_intersects(self):
        "containment implies intersection"

        circ1 = Circle()
        circ2 = Circle(Vec2D(), 0.5)

        self.assertTrue(circ1.contains_circle(circ2))
        self.assertTrue(circ1.intersects(circ2))
        self.assertTrue(circ2.intersects(circ1))
def get_start_circles(start_pos, start_heading, radius):
    "two circles for which the line defined by start_pos and start_heading \
     is tangent"

    pos_center = start_pos + start_heading.rotate(pi / 2).normalized() * radius
    neg_center = start_pos + start_heading.rotate(
        -pi / 2).normalized() * radius

    return [Circle(pos_center, radius), Circle(neg_center, radius)]
    def test_translate_zero(self):
        "test that tangent equals itself after translation of (0, 0)"

        circ1 = Circle()
        circ2 = Circle(Vec2D(4, 0))

        tan = Tangent(Vec2D(0, 1), circ1, 1, Vec2D(4, 1), circ2, -1)

        translated = tan + Vec2D()

        self.assertTrue(tan == translated)
        self.assertTrue(translated == tan)
    def test_equality(self):
        "test equality (and implicitely non-equality) of cirlces"

        circle1 = Circle(Vec2D(1, 0), 1.0)
        circle2 = Circle(Vec2D(1, 0.9 * Vec2D.EPSILON),
                         1.0 + 0.9 * Vec2D.EPSILON)

        self.assertTrue(circle1 == circle2)
        self.assertTrue(circle2 == circle1)
        # equality is symmetric
        self.assertFalse(circle1 != circle2)
        self.assertFalse(circle2 != circle1)
    def test_start_circles(self):
        "test generation of starting circles"

        pos = Vec2D()
        direction = Vec2D(0, 1)

        pos_circ = Circle(Vec2D(-1, 0), 1)
        neg_circ = Circle(Vec2D(1, 0), 1)

        (test_pos, test_neg) = pp.get_start_circles(pos, direction, 1)

        self.assertTrue(test_pos == pos_circ)
        self.assertTrue(test_neg == neg_circ)
    def test_all_tangents_simple1(self):
        "test that all_tangents returns all tangents of two circles \
         when only those 2 circles are present"

        circ1 = Circle(Vec2D(-2, 0), 1)
        circ2 = Circle(Vec2D(2, 0), 1)

        tangents = circ1.tangent_circle(circ2)

        to_test = pp.all_tangents([circ1, circ2], [])

        verificator = all(test in tangents for test in to_test)

        self.assertTrue(verificator)
    def test_tangents_intersec_circles(self):
        "test circle tangents of intersecting circles"
        circle1 = Circle(Vec2D(), 2)
        circle2 = Circle(Vec2D(3, 0), 2)

        tans = circle1.tangent_circle(circle2)

        self.assertTrue(len(tans) == 2)

        tan1 = Tangent(Vec2D(0, 2), circle1, 1, Vec2D(3, 2), circle2, -1)
        tan2 = Tangent(Vec2D(0, -2), circle1, -1, Vec2D(3, -2), circle2, 1)

        verify = all(tan == tan1 or tan == tan2 for tan in tans)

        self.assertTrue(verify)
    def test_polycircle_intersection(self):
        "test intersection between polygon and circle"

        dummy = dummy_polygon()

        circ = Circle(Vec2D(4, 4))

        tan1 = Tangent(Vec2D(4, 5), Circle(Vec2D(4, 4)), -1, Vec2D(0, 5),
                       Circle(Vec2D(0, 4)), 1)
        tan2 = Tangent(Vec2D(5, 4), Circle(Vec2D(4, 4)), 1, Vec2D(5, 0),
                       Circle(Vec2D(4, 0)), -1)

        tans = dummy.tangent_circle(circ)

        self.assertTrue(tan1 in tans and tan2 in tans)
    def test_contains_tangent(self):
        "test start and end inside circle"
        circle = Circle()
        tangent = Tangent(Vec2D(0, -0.5), None, None, Vec2D(0, 0.5), None,
                          None)

        self.assertFalse(circle.intersects_tangent(tangent))
    def test_default_constructor(self):
        "default constructor should return unit circle"

        circle = Circle()

        self.assertTrue(circle.pos == Vec2D())
        self.assertAlmostEqual(circle.radius, 1.0)
    def test_sort_points_singleelement(self):
        "test sort_points_on_circle one element list"

        sorted_list = pp.sort_points_on_circle([Vec2D(1, 0)], Circle())

        self.assertTrue(len(sorted_list) == 1)
        self.assertTrue(sorted_list[0] == Vec2D(1, 0))
    def test_point_containment(self):
        "test point inside circle"

        point = Vec2D()
        circle = Circle()

        self.assertTrue(circle.contains_point(point))
    def test_point_on_border(self):
        "test point exactly on circle border"

        point = Vec2D(1, 0)
        circle = Circle()

        self.assertTrue(circle.contains_point(point))
    def test_tangent_vector(self):
        "vector tangent to unitcircle"

        circ = Circle()
        pos = Vec2D(1, 0)

        self.assertTrue(circ.tangent_vector(pos, 1) == Vec2D(0, 1))
    def test_point_not_inside(self):
        "test point not inside circle"

        point = Vec2D(2, 0)
        circle = Circle()

        self.assertFalse(circle.contains_point(point))
    def test_radial_acc(self):
        "test radial_acc method"

        circle = Circle()
        seg = CircleSegment(Vec2D(1, 0), Vec2D(0, 1), circle, 1)

        self.assertTrue(seg.radial_acc(Vec2D(1, 0), 1) == Vec2D(-1, 0))
def dummy_polygon():
    "return a dummy triangular polygon"

    circ1 = Circle()
    circ2 = Circle(Vec2D(4, 0))
    circ3 = Circle(Vec2D(0, 4))

    side2_helper = Vec2D(sqrt(2.0) / 2.0, sqrt(2.0) / 2.0)
    side2_pos1 = circ2.pos + side2_helper
    side2_pos2 = circ3.pos + side2_helper

    side1 = Tangent(Vec2D(0, -1), circ1, -1, Vec2D(4, -1), circ2, 1)
    side2 = Tangent(side2_pos1, circ2, -1, side2_pos2, circ3, 1)
    side3 = Tangent(Vec2D(-1, 4), circ3, -1, Vec2D(-1, 0), circ1, 1)

    return Polygon([circ1, circ2, circ3], [side1, side2, side3])
    def test_next_pos(self):
        "test next_pos method"

        circle = Circle()

        seg = CircleSegment(Vec2D(1, 0), Vec2D(0, 1), circle, 1)

        self.assertTrue(seg.next_pos(Vec2D(1, 0), pi * 0.5) == Vec2D(0, 1))
    def test_constructor(self):
        "test constructor"
        start_pos = Vec2D(0, 1)
        start_circ = Circle()
        start_orient = 1
        end_pos = Vec2D(2, 1)
        end_circ = Circle(Vec2D(2, 0))
        end_orient = -1

        tan = Tangent(start_pos, start_circ, start_orient, end_pos, end_circ,
                      end_orient)
        self.assertTrue(tan.start_pos == start_pos)
        self.assertTrue(tan.start_pos == start_pos)
        self.assertTrue(tan.start_circle == start_circ)
        self.assertTrue(tan.end_orient == end_orient)
        self.assertTrue(tan.end_circle == end_circ)
        self.assertTrue(tan.end_orient == end_orient)
Exemplo n.º 29
0
def main():
    "draw loop"

    start = Vec2D(0.1, 0.1)
    end = Vec2D(2.1, 1.1)
    direction = Vec2D(0, 0.1)

    obs1 = Circle(Vec2D(1.5, 0.75), 0.2)
    #obs2 = Circle(Vec2D(0.9, 0.6), 0.2)

    corner1 = Circle(Vec2D(), 0.1)
    corner2 = Circle(Vec2D(0.5, 0), 0.1)
    side1 = Tangent(Vec2D(0, -0.1), corner1, -1, Vec2D(0.5, -0.1), corner2, 1)
    side2 = Tangent(Vec2D(0, 0.1), corner1, 1, Vec2D(0.5, 0.1), corner2, -1)

    poly1 = Polygon([corner1, corner2], [side1, side2]) + Vec2D(0.15, 0.3)

    circs = [obs1]
    polys = [poly1]

    paused = False
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    paused = not paused

        if not paused:
            SCREEN.fill(BLACK)
            #SCRREN.blit(BACKGROUND, (0, 0))

            path = get_path(SETTINGS, polys, circs, start, direction, 0, end)

            draw_pos(start)
            draw_pos(end)

            draw_env(SETTINGS, circs, polys)

            draw_path(path)

            pygame.display.update()
    def test_tan(self):
        "test tan method"

        circle = Circle()

        seg1 = CircleSegment(Vec2D(1, 0), Vec2D(0, 1), circle, 1)
        seg2 = CircleSegment(Vec2D(1, 0), Vec2D(0, 1), circle, -1)

        self.assertTrue(seg1.tan(Vec2D(1, 0)) == Vec2D(0, 1))
        self.assertTrue(seg2.tan(Vec2D(1, 0)) == Vec2D(0, -1))