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_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_circle_tangents_full(self): "test tangents between two disjoint circles" circ1 = Circle() circ2 = Circle(Vec2D(4, 0)) midpoint = Vec2D(2, 0) hypothenuse = (midpoint - circ1.pos).length() opp_side = circ1.radius tan_len = sqrt(hypothenuse * hypothenuse - opp_side * opp_side) angle = asin(opp_side / hypothenuse) start1 = (circ1.pos - midpoint).rotate(angle).normalized() * tan_len start1 = start1 + midpoint end1 = (circ2.pos - midpoint).rotate(angle).normalized() * tan_len end1 = end1 + midpoint start2 = (circ1.pos - midpoint).rotate(-angle).normalized() * tan_len start2 = start2 + midpoint end2 = (circ2.pos - midpoint).rotate(-angle).normalized() * tan_len end2 = end2 + midpoint r_tans = [] r_tans.append(Tangent(start1, circ1, -1, end1, circ2, -1)) r_tans.append(Tangent(start2, circ1, 1, end2, circ2, 1)) r_tans.append(Tangent(Vec2D(0, 1), circ1, 1, Vec2D(4, 1), circ2, -1)) r_tans.append(Tangent(Vec2D(0, -1), circ1, -1, Vec2D(4, -1), circ2, 1)) tans = circ1.tangent_circle(circ2) self.assertTrue(len(tans) == 4) valid = all(t in r_tans for t in tans) self.assertTrue(valid)