def test_ellipse(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x2) p4 = Point(0, 1) p5 = Point(-1, 0) e1 = Ellipse(p1, 1, 1) e2 = Ellipse(p2, half, 1) e3 = Ellipse(p1, y1, y1) c1 = Circle(p1, 1) c2 = Circle(p2,1) c3 = Circle(Point(sqrt(2),sqrt(2)),1) # Test creation with three points cen,rad = Point(3*half, 2), 5*half assert Circle(Point(0,0), Point(3,0), Point(0,4)) == Circle(cen, rad) raises(GeometryError, "Circle(Point(0,0), Point(1,1), Point(2,2))") # Basic Stuff assert e1 == c1 assert e1 != e2 assert p4 in e1 assert p2 not in e2 assert e1.area == pi assert e2.area == pi/2 assert e3.area == pi*(y1**2) assert c1.area == e1.area assert c1.circumference == e1.circumference assert e3.circumference == 2*pi*y1 a = Symbol('a') b = Symbol('b') e5 = Ellipse(p1, a, b) assert e5.circumference == 4*a*C.Integral(((1 - x**2*Abs(b**2 - a**2)/a**2)/(1 - x**2))**(S(1)/2),\ (x, 0, 1)) assert e2.arbitrary_point() in e2 # Foci f1,f2 = Point(sqrt(12), 0), Point(-sqrt(12), 0) ef = Ellipse(Point(0, 0), 4, 2) assert ef.foci in [(f1, f2), (f2, f1)] # Tangents v = sqrt(2) / 2 p1_1 = Point(v, v) p1_2 = p2 + Point(half, 0) p1_3 = p2 + Point(0, 1) assert e1.tangent_line(p4) == c1.tangent_line(p4) assert e2.tangent_line(p1_2) == Line(p1_2, p2 + Point(half, 1)) assert e2.tangent_line(p1_3) == Line(p1_3, p2 + Point(half, 1)) assert c1.tangent_line(p1_1) == Line(p1_1, Point(0, sqrt(2))) assert e2.is_tangent(Line(p1_2, p2 + Point(half, 1))) assert e2.is_tangent(Line(p1_3, p2 + Point(half, 1))) assert c1.is_tangent(Line(p1_1, Point(0, sqrt(2)))) assert e1.is_tangent(Line(Point(0, 0), Point(1, 1))) == False # Intersection l1 = Line(Point(1, -5), Point(1, 5)) l2 = Line(Point(-5, -1), Point(5, -1)) l3 = Line(Point(-1, -1), Point(1, 1)) l4 = Line(Point(-10, 0), Point(0, 10)) pts_c1_l3 = [Point(sqrt(2)/2, sqrt(2)/2), Point(-sqrt(2)/2, -sqrt(2)/2)] assert intersection(e2, l4) == [] assert intersection(c1, Point(1, 0)) == [Point(1, 0)] assert intersection(c1, l1) == [Point(1, 0)] assert intersection(c1, l2) == [Point(0, -1)] assert intersection(c1, l3) in [pts_c1_l3, [pts_c1_l3[1], pts_c1_l3[0]]] assert intersection(c1, c2) in [[(1,0), (0,1)],[(0,1),(1,0)]] assert intersection(c1, c3) == [(sqrt(2)/2, sqrt(2)/2)] # some special case intersections csmall = Circle(p1, 3) cbig = Circle(p1, 5) cout = Circle(Point(5, 5), 1) # one circle inside of another assert csmall.intersection(cbig) == [] # separate circles assert csmall.intersection(cout) == [] # coincident circles assert csmall.intersection(csmall) == csmall v = sqrt(2) t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0)) points = intersection(t1, c1) assert len(points) == 4 assert Point(0, 1) in points assert Point(0, -1) in points assert Point(v/2, v/2) in points assert Point(v/2, -v/2) in points e1 = Circle(Point(0, 0), 5) e2 = Ellipse(Point(0, 0), 5, 20) assert intersection(e1, e2) in \ [[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]] # FAILING ELLIPSE INTERSECTION GOES HERE # Combinations of above assert e3.is_tangent(e3.tangent_line(p1 + Point(y1, 0))) major = 3 minor = 1 e4 = Ellipse(p2, major, minor) assert e4.focus_distance == sqrt(abs(major**2 - minor**2)) ecc = e4.focus_distance / major assert e4.eccentricity == ecc assert e4.periapsis == major*(1 - ecc) assert e4.apoapsis == major*(1 + ecc)
def test_ellipse(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x2) p4 = Point(0, 1) p5 = Point(-1, 0) e1 = Ellipse(p1, 1, 1) e2 = Ellipse(p2, half, 1) e3 = Ellipse(p1, y1, y1) c1 = Circle(p1, 1) c2 = Circle(p2, 1) c3 = Circle(Point(sqrt(2), sqrt(2)), 1) # Test creation with three points cen, rad = Point(3 * half, 2), 5 * half assert Circle(Point(0, 0), Point(3, 0), Point(0, 4)) == Circle(cen, rad) raises(GeometryError, "Circle(Point(0,0), Point(1,1), Point(2,2))") # Basic Stuff assert e1 == c1 assert e1 != e2 assert p4 in e1 assert p2 not in e2 assert e1.area == pi assert e2.area == pi / 2 assert e3.area == pi * (y1**2) assert c1.area == e1.area assert c1.circumference == 2 * pi assert e2.arbitrary_point() in e2 for ind in xrange(0, 5): assert e3.random_point() in e3 # Foci f1, f2 = Point(sqrt(12), 0), Point(-sqrt(12), 0) ef = Ellipse(Point(0, 0), 4, 2) assert ef.foci in [(f1, f2), (f2, f1)] # Tangents v = sqrt(2) / 2 p1_1 = Point(v, v) p1_2 = p2 + Point(half, 0) p1_3 = p2 + Point(0, 1) assert e1.tangent_line(p4) == c1.tangent_line(p4) assert e2.tangent_line(p1_2) == Line(p1_2, p2 + Point(half, 1)) assert e2.tangent_line(p1_3) == Line(p1_3, p2 + Point(half, 1)) assert c1.tangent_line(p1_1) == Line(p1_1, Point(0, sqrt(2))) assert e2.is_tangent(Line(p1_2, p2 + Point(half, 1))) assert e2.is_tangent(Line(p1_3, p2 + Point(half, 1))) assert c1.is_tangent(Line(p1_1, Point(0, sqrt(2)))) assert e1.is_tangent(Line(Point(0, 0), Point(1, 1))) == False # Intersection l1 = Line(Point(1, -5), Point(1, 5)) l2 = Line(Point(-5, -1), Point(5, -1)) l3 = Line(Point(-1, -1), Point(1, 1)) l4 = Line(Point(-10, 0), Point(0, 10)) pts_c1_l3 = [ Point(sqrt(2) / 2, sqrt(2) / 2), Point(-sqrt(2) / 2, -sqrt(2) / 2) ] assert intersection(e2, l4) == [] assert intersection(c1, Point(1, 0)) == [Point(1, 0)] assert intersection(c1, l1) == [Point(1, 0)] assert intersection(c1, l2) == [Point(0, -1)] assert intersection(c1, l3) in [pts_c1_l3, [pts_c1_l3[1], pts_c1_l3[0]]] assert intersection(c1, c2) in [[(1, 0), (0, 1)], [(0, 1), (1, 0)]] assert intersection(c1, c3) == [(sqrt(2) / 2, sqrt(2) / 2)] v = sqrt(2) t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0)) points = intersection(t1, c1) assert len(points) == 4 assert Point(0, 1) in points assert Point(0, -1) in points assert Point(v / 2, v / 2) in points assert Point(v / 2, -v / 2) in points e1 = Circle(Point(0, 0), 5) e2 = Ellipse(Point(0, 0), 5, 20) assert intersection(e1, e2) in \ [[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]] # Combinations of above assert e3.is_tangent(e3.tangent_line(p1 + Point(y1, 0)))
def test_ellipse(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x2) p4 = Point(0, 1) p5 = Point(-1, 0) e1 = Ellipse(p1, 1, 1) e2 = Ellipse(p2, half, 1) e3 = Ellipse(p1, y1, y1) c1 = Circle(p1, 1) c2 = Circle(p2, 1) c3 = Circle(Point(sqrt(2), sqrt(2)), 1) # Test creation with three points cen, rad = Point(3 * half, 2), 5 * half assert Circle(Point(0, 0), Point(3, 0), Point(0, 4)) == Circle(cen, rad) raises(GeometryError, "Circle(Point(0,0), Point(1,1), Point(2,2))") # Basic Stuff assert e1 == c1 assert e1 != e2 assert p4 in e1 assert p2 not in e2 assert e1.area == pi assert e2.area == pi / 2 assert e3.area == pi * (y1**2) assert c1.area == e1.area assert c1.circumference == e1.circumference assert e3.circumference == 2 * pi * y1 a = Symbol('a') b = Symbol('b') e5 = Ellipse(p1, a, b) assert e5.circumference == 4*a*C.Integral(((1 - x**2*Abs(b**2 - a**2)/a**2)/(1 - x**2))**(S(1)/2),\ (x, 0, 1)) assert e2.arbitrary_point() in e2 # Foci f1, f2 = Point(sqrt(12), 0), Point(-sqrt(12), 0) ef = Ellipse(Point(0, 0), 4, 2) assert ef.foci in [(f1, f2), (f2, f1)] # Tangents v = sqrt(2) / 2 p1_1 = Point(v, v) p1_2 = p2 + Point(half, 0) p1_3 = p2 + Point(0, 1) assert e1.tangent_line(p4) == c1.tangent_line(p4) assert e2.tangent_line(p1_2) == Line(p1_2, p2 + Point(half, 1)) assert e2.tangent_line(p1_3) == Line(p1_3, p2 + Point(half, 1)) assert c1.tangent_line(p1_1) == Line(p1_1, Point(0, sqrt(2))) assert e2.is_tangent(Line(p1_2, p2 + Point(half, 1))) assert e2.is_tangent(Line(p1_3, p2 + Point(half, 1))) assert c1.is_tangent(Line(p1_1, Point(0, sqrt(2)))) assert e1.is_tangent(Line(Point(0, 0), Point(1, 1))) == False # Intersection l1 = Line(Point(1, -5), Point(1, 5)) l2 = Line(Point(-5, -1), Point(5, -1)) l3 = Line(Point(-1, -1), Point(1, 1)) l4 = Line(Point(-10, 0), Point(0, 10)) pts_c1_l3 = [ Point(sqrt(2) / 2, sqrt(2) / 2), Point(-sqrt(2) / 2, -sqrt(2) / 2) ] assert intersection(e2, l4) == [] assert intersection(c1, Point(1, 0)) == [Point(1, 0)] assert intersection(c1, l1) == [Point(1, 0)] assert intersection(c1, l2) == [Point(0, -1)] assert intersection(c1, l3) in [pts_c1_l3, [pts_c1_l3[1], pts_c1_l3[0]]] assert intersection(c1, c2) in [[(1, 0), (0, 1)], [(0, 1), (1, 0)]] assert intersection(c1, c3) == [(sqrt(2) / 2, sqrt(2) / 2)] # some special case intersections csmall = Circle(p1, 3) cbig = Circle(p1, 5) cout = Circle(Point(5, 5), 1) # one circle inside of another assert csmall.intersection(cbig) == [] # separate circles assert csmall.intersection(cout) == [] # coincident circles assert csmall.intersection(csmall) == csmall v = sqrt(2) t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0)) points = intersection(t1, c1) assert len(points) == 4 assert Point(0, 1) in points assert Point(0, -1) in points assert Point(v / 2, v / 2) in points assert Point(v / 2, -v / 2) in points e1 = Circle(Point(0, 0), 5) e2 = Ellipse(Point(0, 0), 5, 20) assert intersection(e1, e2) in \ [[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]] # FAILING ELLIPSE INTERSECTION GOES HERE # Combinations of above assert e3.is_tangent(e3.tangent_line(p1 + Point(y1, 0))) major = 3 minor = 1 e4 = Ellipse(p2, major, minor) assert e4.focus_distance == sqrt(abs(major**2 - minor**2)) ecc = e4.focus_distance / major assert e4.eccentricity == ecc assert e4.periapsis == major * (1 - ecc) assert e4.apoapsis == major * (1 + ecc)
def test_ellipse(): p1 = Point(0, 0) p2 = Point(1, 1) p3 = Point(x1, x2) p4 = Point(0, 1) p5 = Point(-1, 0) e1 = Ellipse(p1, 1, 1) e2 = Ellipse(p2, half, 1) e3 = Ellipse(p1, y1, y1) c1 = Circle(p1, 1) c2 = Circle(p2,1) c3 = Circle(Point(sqrt(2),sqrt(2)),1) # Test creation with three points cen,rad = Point(3*half, 2), 5*half assert Circle(Point(0,0), Point(3,0), Point(0,4)) == Circle(cen, rad) raises(GeometryError, "Circle(Point(0,0), Point(1,1), Point(2,2))") # Basic Stuff assert e1 == c1 assert e1 != e2 assert p4 in e1 assert p2 not in e2 assert e1.area == pi assert e2.area == pi/2 assert e3.area == pi*(y1**2) assert c1.area == e1.area assert c1.circumference == 2*pi assert e2.arbitrary_point() in e2 for ind in xrange(0, 5): assert e3.random_point() in e3 # Foci f1,f2 = Point(sqrt(12), 0), Point(-sqrt(12), 0) ef = Ellipse(Point(0, 0), 4, 2) assert ef.foci in [(f1, f2), (f2, f1)] # Tangents v = sqrt(2) / 2 p1_1 = Point(v, v) p1_2 = p2 + Point(half, 0) p1_3 = p2 + Point(0, 1) assert e1.tangent_line(p4) == c1.tangent_line(p4) assert e2.tangent_line(p1_2) == Line(p1_2, p2 + Point(half, 1)) assert e2.tangent_line(p1_3) == Line(p1_3, p2 + Point(half, 1)) assert c1.tangent_line(p1_1) == Line(p1_1, Point(0, sqrt(2))) assert e2.is_tangent(Line(p1_2, p2 + Point(half, 1))) assert e2.is_tangent(Line(p1_3, p2 + Point(half, 1))) assert c1.is_tangent(Line(p1_1, Point(0, sqrt(2)))) assert e1.is_tangent(Line(Point(0, 0), Point(1, 1))) == False # Intersection l1 = Line(Point(1, -5), Point(1, 5)) l2 = Line(Point(-5, -1), Point(5, -1)) l3 = Line(Point(-1, -1), Point(1, 1)) l4 = Line(Point(-10, 0), Point(0, 10)) pts_c1_l3 = [Point(sqrt(2)/2, sqrt(2)/2), Point(-sqrt(2)/2, -sqrt(2)/2)] assert intersection(e2, l4) == [] assert intersection(c1, Point(1, 0)) == [Point(1, 0)] assert intersection(c1, l1) == [Point(1, 0)] assert intersection(c1, l2) == [Point(0, -1)] assert intersection(c1, l3) in [pts_c1_l3, [pts_c1_l3[1], pts_c1_l3[0]]] assert intersection(c1, c2) in [[(1,0), (0,1)],[(0,1),(1,0)]] assert intersection(c1, c3) == [(sqrt(2)/2, sqrt(2)/2)] v = sqrt(2) t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0)) points = intersection(t1, c1) assert len(points) == 4 assert Point(0, 1) in points assert Point(0, -1) in points assert Point(v/2, v/2) in points assert Point(v/2, -v/2) in points e1 = Circle(Point(0, 0), 5) e2 = Ellipse(Point(0, 0), 5, 20) assert intersection(e1, e2) in \ [[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]] # Combinations of above assert e3.is_tangent(e3.tangent_line(p1 + Point(y1, 0)))