test_ellipse.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. from sympy.core import expand
  2. from sympy.core.numbers import (Rational, oo, pi)
  3. from sympy.core.relational import Eq
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import (Symbol, symbols)
  6. from sympy.functions.elementary.complexes import Abs
  7. from sympy.functions.elementary.miscellaneous import sqrt
  8. from sympy.functions.elementary.trigonometric import sec
  9. from sympy.geometry.line import Segment2D
  10. from sympy.geometry.point import Point2D
  11. from sympy.geometry import (Circle, Ellipse, GeometryError, Line, Point,
  12. Polygon, Ray, RegularPolygon, Segment,
  13. Triangle, intersection)
  14. from sympy.testing.pytest import raises, slow
  15. from sympy.integrals.integrals import integrate
  16. from sympy.functions.special.elliptic_integrals import elliptic_e
  17. from sympy.functions.elementary.miscellaneous import Max
  18. def test_ellipse_equation_using_slope():
  19. from sympy.abc import x, y
  20. e1 = Ellipse(Point(1, 0), 3, 2)
  21. assert str(e1.equation(_slope=1)) == str((-x + y + 1)**2/8 + (x + y - 1)**2/18 - 1)
  22. e2 = Ellipse(Point(0, 0), 4, 1)
  23. assert str(e2.equation(_slope=1)) == str((-x + y)**2/2 + (x + y)**2/32 - 1)
  24. e3 = Ellipse(Point(1, 5), 6, 2)
  25. assert str(e3.equation(_slope=2)) == str((-2*x + y - 3)**2/20 + (x + 2*y - 11)**2/180 - 1)
  26. def test_object_from_equation():
  27. from sympy.abc import x, y, a, b, c, d, e
  28. assert Circle(x**2 + y**2 + 3*x + 4*y - 8) == Circle(Point2D(S(-3) / 2, -2), sqrt(57) / 2)
  29. assert Circle(x**2 + y**2 + 6*x + 8*y + 25) == Circle(Point2D(-3, -4), 0)
  30. assert Circle(a**2 + b**2 + 6*a + 8*b + 25, x='a', y='b') == Circle(Point2D(-3, -4), 0)
  31. assert Circle(x**2 + y**2 - 25) == Circle(Point2D(0, 0), 5)
  32. assert Circle(x**2 + y**2) == Circle(Point2D(0, 0), 0)
  33. assert Circle(a**2 + b**2, x='a', y='b') == Circle(Point2D(0, 0), 0)
  34. assert Circle(x**2 + y**2 + 6*x + 8) == Circle(Point2D(-3, 0), 1)
  35. assert Circle(x**2 + y**2 + 6*y + 8) == Circle(Point2D(0, -3), 1)
  36. assert Circle((x - 1)**2 + y**2 - 9) == Circle(Point2D(1, 0), 3)
  37. assert Circle(6*(x**2) + 6*(y**2) + 6*x + 8*y - 25) == Circle(Point2D(Rational(-1, 2), Rational(-2, 3)), 5*sqrt(7)/6)
  38. assert Circle(Eq(a**2 + b**2, 25), x='a', y=b) == Circle(Point2D(0, 0), 5)
  39. raises(GeometryError, lambda: Circle(x**2 + y**2 + 3*x + 4*y + 26))
  40. raises(GeometryError, lambda: Circle(x**2 + y**2 + 25))
  41. raises(GeometryError, lambda: Circle(a**2 + b**2 + 25, x='a', y='b'))
  42. raises(GeometryError, lambda: Circle(x**2 + 6*y + 8))
  43. raises(GeometryError, lambda: Circle(6*(x ** 2) + 4*(y**2) + 6*x + 8*y + 25))
  44. raises(ValueError, lambda: Circle(a**2 + b**2 + 3*a + 4*b - 8))
  45. # .equation() adds 'real=True' assumption; '==' would fail if assumptions differed
  46. x, y = symbols('x y', real=True)
  47. eq = a*x**2 + a*y**2 + c*x + d*y + e
  48. assert expand(Circle(eq).equation()*a) == eq
  49. @slow
  50. def test_ellipse_geom():
  51. x = Symbol('x', real=True)
  52. y = Symbol('y', real=True)
  53. t = Symbol('t', real=True)
  54. y1 = Symbol('y1', real=True)
  55. half = S.Half
  56. p1 = Point(0, 0)
  57. p2 = Point(1, 1)
  58. p4 = Point(0, 1)
  59. e1 = Ellipse(p1, 1, 1)
  60. e2 = Ellipse(p2, half, 1)
  61. e3 = Ellipse(p1, y1, y1)
  62. c1 = Circle(p1, 1)
  63. c2 = Circle(p2, 1)
  64. c3 = Circle(Point(sqrt(2), sqrt(2)), 1)
  65. l1 = Line(p1, p2)
  66. # Test creation with three points
  67. cen, rad = Point(3*half, 2), 5*half
  68. assert Circle(Point(0, 0), Point(3, 0), Point(0, 4)) == Circle(cen, rad)
  69. assert Circle(Point(0, 0), Point(1, 1), Point(2, 2)) == Segment2D(Point2D(0, 0), Point2D(2, 2))
  70. raises(ValueError, lambda: Ellipse(None, None, None, 1))
  71. raises(ValueError, lambda: Ellipse())
  72. raises(GeometryError, lambda: Circle(Point(0, 0)))
  73. raises(GeometryError, lambda: Circle(Symbol('x')*Symbol('y')))
  74. # Basic Stuff
  75. assert Ellipse(None, 1, 1).center == Point(0, 0)
  76. assert e1 == c1
  77. assert e1 != e2
  78. assert e1 != l1
  79. assert p4 in e1
  80. assert e1 in e1
  81. assert e2 in e2
  82. assert 1 not in e2
  83. assert p2 not in e2
  84. assert e1.area == pi
  85. assert e2.area == pi/2
  86. assert e3.area == pi*y1*abs(y1)
  87. assert c1.area == e1.area
  88. assert c1.circumference == e1.circumference
  89. assert e3.circumference == 2*pi*y1
  90. assert e1.plot_interval() == e2.plot_interval() == [t, -pi, pi]
  91. assert e1.plot_interval(x) == e2.plot_interval(x) == [x, -pi, pi]
  92. assert c1.minor == 1
  93. assert c1.major == 1
  94. assert c1.hradius == 1
  95. assert c1.vradius == 1
  96. assert Ellipse((1, 1), 0, 0) == Point(1, 1)
  97. assert Ellipse((1, 1), 1, 0) == Segment(Point(0, 1), Point(2, 1))
  98. assert Ellipse((1, 1), 0, 1) == Segment(Point(1, 0), Point(1, 2))
  99. # Private Functions
  100. assert hash(c1) == hash(Circle(Point(1, 0), Point(0, 1), Point(0, -1)))
  101. assert c1 in e1
  102. assert (Line(p1, p2) in e1) is False
  103. assert e1.__cmp__(e1) == 0
  104. assert e1.__cmp__(Point(0, 0)) > 0
  105. # Encloses
  106. assert e1.encloses(Segment(Point(-0.5, -0.5), Point(0.5, 0.5))) is True
  107. assert e1.encloses(Line(p1, p2)) is False
  108. assert e1.encloses(Ray(p1, p2)) is False
  109. assert e1.encloses(e1) is False
  110. assert e1.encloses(
  111. Polygon(Point(-0.5, -0.5), Point(-0.5, 0.5), Point(0.5, 0.5))) is True
  112. assert e1.encloses(RegularPolygon(p1, 0.5, 3)) is True
  113. assert e1.encloses(RegularPolygon(p1, 5, 3)) is False
  114. assert e1.encloses(RegularPolygon(p2, 5, 3)) is False
  115. assert e2.arbitrary_point() in e2
  116. raises(ValueError, lambda: Ellipse(Point(x, y), 1, 1).arbitrary_point(parameter='x'))
  117. # Foci
  118. f1, f2 = Point(sqrt(12), 0), Point(-sqrt(12), 0)
  119. ef = Ellipse(Point(0, 0), 4, 2)
  120. assert ef.foci in [(f1, f2), (f2, f1)]
  121. # Tangents
  122. v = sqrt(2) / 2
  123. p1_1 = Point(v, v)
  124. p1_2 = p2 + Point(half, 0)
  125. p1_3 = p2 + Point(0, 1)
  126. assert e1.tangent_lines(p4) == c1.tangent_lines(p4)
  127. assert e2.tangent_lines(p1_2) == [Line(Point(Rational(3, 2), 1), Point(Rational(3, 2), S.Half))]
  128. assert e2.tangent_lines(p1_3) == [Line(Point(1, 2), Point(Rational(5, 4), 2))]
  129. assert c1.tangent_lines(p1_1) != [Line(p1_1, Point(0, sqrt(2)))]
  130. assert c1.tangent_lines(p1) == []
  131. assert e2.is_tangent(Line(p1_2, p2 + Point(half, 1)))
  132. assert e2.is_tangent(Line(p1_3, p2 + Point(half, 1)))
  133. assert c1.is_tangent(Line(p1_1, Point(0, sqrt(2))))
  134. assert e1.is_tangent(Line(Point(0, 0), Point(1, 1))) is False
  135. assert c1.is_tangent(e1) is True
  136. assert c1.is_tangent(Ellipse(Point(2, 0), 1, 1)) is True
  137. assert c1.is_tangent(
  138. Polygon(Point(1, 1), Point(1, -1), Point(2, 0))) is True
  139. assert c1.is_tangent(
  140. Polygon(Point(1, 1), Point(1, 0), Point(2, 0))) is False
  141. assert Circle(Point(5, 5), 3).is_tangent(Circle(Point(0, 5), 1)) is False
  142. assert Ellipse(Point(5, 5), 2, 1).tangent_lines(Point(0, 0)) == \
  143. [Line(Point(0, 0), Point(Rational(77, 25), Rational(132, 25))),
  144. Line(Point(0, 0), Point(Rational(33, 5), Rational(22, 5)))]
  145. assert Ellipse(Point(5, 5), 2, 1).tangent_lines(Point(3, 4)) == \
  146. [Line(Point(3, 4), Point(4, 4)), Line(Point(3, 4), Point(3, 5))]
  147. assert Circle(Point(5, 5), 2).tangent_lines(Point(3, 3)) == \
  148. [Line(Point(3, 3), Point(4, 3)), Line(Point(3, 3), Point(3, 4))]
  149. assert Circle(Point(5, 5), 2).tangent_lines(Point(5 - 2*sqrt(2), 5)) == \
  150. [Line(Point(5 - 2*sqrt(2), 5), Point(5 - sqrt(2), 5 - sqrt(2))),
  151. Line(Point(5 - 2*sqrt(2), 5), Point(5 - sqrt(2), 5 + sqrt(2))), ]
  152. assert Circle(Point(5, 5), 5).tangent_lines(Point(4, 0)) == \
  153. [Line(Point(4, 0), Point(Rational(40, 13), Rational(5, 13))),
  154. Line(Point(4, 0), Point(5, 0))]
  155. assert Circle(Point(5, 5), 5).tangent_lines(Point(0, 6)) == \
  156. [Line(Point(0, 6), Point(0, 7)),
  157. Line(Point(0, 6), Point(Rational(5, 13), Rational(90, 13)))]
  158. # for numerical calculations, we shouldn't demand exact equality,
  159. # so only test up to the desired precision
  160. def lines_close(l1, l2, prec):
  161. """ tests whether l1 and 12 are within 10**(-prec)
  162. of each other """
  163. return abs(l1.p1 - l2.p1) < 10**(-prec) and abs(l1.p2 - l2.p2) < 10**(-prec)
  164. def line_list_close(ll1, ll2, prec):
  165. return all(lines_close(l1, l2, prec) for l1, l2 in zip(ll1, ll2))
  166. e = Ellipse(Point(0, 0), 2, 1)
  167. assert e.normal_lines(Point(0, 0)) == \
  168. [Line(Point(0, 0), Point(0, 1)), Line(Point(0, 0), Point(1, 0))]
  169. assert e.normal_lines(Point(1, 0)) == \
  170. [Line(Point(0, 0), Point(1, 0))]
  171. assert e.normal_lines((0, 1)) == \
  172. [Line(Point(0, 0), Point(0, 1))]
  173. assert line_list_close(e.normal_lines(Point(1, 1), 2), [
  174. Line(Point(Rational(-51, 26), Rational(-1, 5)), Point(Rational(-25, 26), Rational(17, 83))),
  175. Line(Point(Rational(28, 29), Rational(-7, 8)), Point(Rational(57, 29), Rational(-9, 2)))], 2)
  176. # test the failure of Poly.intervals and checks a point on the boundary
  177. p = Point(sqrt(3), S.Half)
  178. assert p in e
  179. assert line_list_close(e.normal_lines(p, 2), [
  180. Line(Point(Rational(-341, 171), Rational(-1, 13)), Point(Rational(-170, 171), Rational(5, 64))),
  181. Line(Point(Rational(26, 15), Rational(-1, 2)), Point(Rational(41, 15), Rational(-43, 26)))], 2)
  182. # be sure to use the slope that isn't undefined on boundary
  183. e = Ellipse((0, 0), 2, 2*sqrt(3)/3)
  184. assert line_list_close(e.normal_lines((1, 1), 2), [
  185. Line(Point(Rational(-64, 33), Rational(-20, 71)), Point(Rational(-31, 33), Rational(2, 13))),
  186. Line(Point(1, -1), Point(2, -4))], 2)
  187. # general ellipse fails except under certain conditions
  188. e = Ellipse((0, 0), x, 1)
  189. assert e.normal_lines((x + 1, 0)) == [Line(Point(0, 0), Point(1, 0))]
  190. raises(NotImplementedError, lambda: e.normal_lines((x + 1, 1)))
  191. # Properties
  192. major = 3
  193. minor = 1
  194. e4 = Ellipse(p2, minor, major)
  195. assert e4.focus_distance == sqrt(major**2 - minor**2)
  196. ecc = e4.focus_distance / major
  197. assert e4.eccentricity == ecc
  198. assert e4.periapsis == major*(1 - ecc)
  199. assert e4.apoapsis == major*(1 + ecc)
  200. assert e4.semilatus_rectum == major*(1 - ecc ** 2)
  201. # independent of orientation
  202. e4 = Ellipse(p2, major, minor)
  203. assert e4.focus_distance == sqrt(major**2 - minor**2)
  204. ecc = e4.focus_distance / major
  205. assert e4.eccentricity == ecc
  206. assert e4.periapsis == major*(1 - ecc)
  207. assert e4.apoapsis == major*(1 + ecc)
  208. # Intersection
  209. l1 = Line(Point(1, -5), Point(1, 5))
  210. l2 = Line(Point(-5, -1), Point(5, -1))
  211. l3 = Line(Point(-1, -1), Point(1, 1))
  212. l4 = Line(Point(-10, 0), Point(0, 10))
  213. pts_c1_l3 = [Point(sqrt(2)/2, sqrt(2)/2), Point(-sqrt(2)/2, -sqrt(2)/2)]
  214. assert intersection(e2, l4) == []
  215. assert intersection(c1, Point(1, 0)) == [Point(1, 0)]
  216. assert intersection(c1, l1) == [Point(1, 0)]
  217. assert intersection(c1, l2) == [Point(0, -1)]
  218. assert intersection(c1, l3) in [pts_c1_l3, [pts_c1_l3[1], pts_c1_l3[0]]]
  219. assert intersection(c1, c2) == [Point(0, 1), Point(1, 0)]
  220. assert intersection(c1, c3) == [Point(sqrt(2)/2, sqrt(2)/2)]
  221. assert e1.intersection(l1) == [Point(1, 0)]
  222. assert e2.intersection(l4) == []
  223. assert e1.intersection(Circle(Point(0, 2), 1)) == [Point(0, 1)]
  224. assert e1.intersection(Circle(Point(5, 0), 1)) == []
  225. assert e1.intersection(Ellipse(Point(2, 0), 1, 1)) == [Point(1, 0)]
  226. assert e1.intersection(Ellipse(Point(5, 0), 1, 1)) == []
  227. assert e1.intersection(Point(2, 0)) == []
  228. assert e1.intersection(e1) == e1
  229. assert intersection(Ellipse(Point(0, 0), 2, 1), Ellipse(Point(3, 0), 1, 2)) == [Point(2, 0)]
  230. assert intersection(Circle(Point(0, 0), 2), Circle(Point(3, 0), 1)) == [Point(2, 0)]
  231. assert intersection(Circle(Point(0, 0), 2), Circle(Point(7, 0), 1)) == []
  232. assert intersection(Ellipse(Point(0, 0), 5, 17), Ellipse(Point(4, 0), 1, 0.2)) == [Point(5, 0)]
  233. assert intersection(Ellipse(Point(0, 0), 5, 17), Ellipse(Point(4, 0), 0.999, 0.2)) == []
  234. assert Circle((0, 0), S.Half).intersection(
  235. Triangle((-1, 0), (1, 0), (0, 1))) == [
  236. Point(Rational(-1, 2), 0), Point(S.Half, 0)]
  237. raises(TypeError, lambda: intersection(e2, Line((0, 0, 0), (0, 0, 1))))
  238. raises(TypeError, lambda: intersection(e2, Rational(12)))
  239. raises(TypeError, lambda: Ellipse.intersection(e2, 1))
  240. # some special case intersections
  241. csmall = Circle(p1, 3)
  242. cbig = Circle(p1, 5)
  243. cout = Circle(Point(5, 5), 1)
  244. # one circle inside of another
  245. assert csmall.intersection(cbig) == []
  246. # separate circles
  247. assert csmall.intersection(cout) == []
  248. # coincident circles
  249. assert csmall.intersection(csmall) == csmall
  250. v = sqrt(2)
  251. t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0))
  252. points = intersection(t1, c1)
  253. assert len(points) == 4
  254. assert Point(0, 1) in points
  255. assert Point(0, -1) in points
  256. assert Point(v/2, v/2) in points
  257. assert Point(v/2, -v/2) in points
  258. circ = Circle(Point(0, 0), 5)
  259. elip = Ellipse(Point(0, 0), 5, 20)
  260. assert intersection(circ, elip) in \
  261. [[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]]
  262. assert elip.tangent_lines(Point(0, 0)) == []
  263. elip = Ellipse(Point(0, 0), 3, 2)
  264. assert elip.tangent_lines(Point(3, 0)) == \
  265. [Line(Point(3, 0), Point(3, -12))]
  266. e1 = Ellipse(Point(0, 0), 5, 10)
  267. e2 = Ellipse(Point(2, 1), 4, 8)
  268. a = Rational(53, 17)
  269. c = 2*sqrt(3991)/17
  270. ans = [Point(a - c/8, a/2 + c), Point(a + c/8, a/2 - c)]
  271. assert e1.intersection(e2) == ans
  272. e2 = Ellipse(Point(x, y), 4, 8)
  273. c = sqrt(3991)
  274. ans = [Point(-c/68 + a, c*Rational(2, 17) + a/2), Point(c/68 + a, c*Rational(-2, 17) + a/2)]
  275. assert [p.subs({x: 2, y:1}) for p in e1.intersection(e2)] == ans
  276. # Combinations of above
  277. assert e3.is_tangent(e3.tangent_lines(p1 + Point(y1, 0))[0])
  278. e = Ellipse((1, 2), 3, 2)
  279. assert e.tangent_lines(Point(10, 0)) == \
  280. [Line(Point(10, 0), Point(1, 0)),
  281. Line(Point(10, 0), Point(Rational(14, 5), Rational(18, 5)))]
  282. # encloses_point
  283. e = Ellipse((0, 0), 1, 2)
  284. assert e.encloses_point(e.center)
  285. assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
  286. assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
  287. assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
  288. assert e.encloses_point(
  289. e.center + Point(e.hradius + Rational(1, 10), 0)) is False
  290. e = Ellipse((0, 0), 2, 1)
  291. assert e.encloses_point(e.center)
  292. assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
  293. assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
  294. assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
  295. assert e.encloses_point(
  296. e.center + Point(e.hradius + Rational(1, 10), 0)) is False
  297. assert c1.encloses_point(Point(1, 0)) is False
  298. assert c1.encloses_point(Point(0.3, 0.4)) is True
  299. assert e.scale(2, 3) == Ellipse((0, 0), 4, 3)
  300. assert e.scale(3, 6) == Ellipse((0, 0), 6, 6)
  301. assert e.rotate(pi) == e
  302. assert e.rotate(pi, (1, 2)) == Ellipse(Point(2, 4), 2, 1)
  303. raises(NotImplementedError, lambda: e.rotate(pi/3))
  304. # Circle rotation tests (Issue #11743)
  305. # Link - https://github.com/sympy/sympy/issues/11743
  306. cir = Circle(Point(1, 0), 1)
  307. assert cir.rotate(pi/2) == Circle(Point(0, 1), 1)
  308. assert cir.rotate(pi/3) == Circle(Point(S.Half, sqrt(3)/2), 1)
  309. assert cir.rotate(pi/3, Point(1, 0)) == Circle(Point(1, 0), 1)
  310. assert cir.rotate(pi/3, Point(0, 1)) == Circle(Point(S.Half + sqrt(3)/2, S.Half + sqrt(3)/2), 1)
  311. def test_construction():
  312. e1 = Ellipse(hradius=2, vradius=1, eccentricity=None)
  313. assert e1.eccentricity == sqrt(3)/2
  314. e2 = Ellipse(hradius=2, vradius=None, eccentricity=sqrt(3)/2)
  315. assert e2.vradius == 1
  316. e3 = Ellipse(hradius=None, vradius=1, eccentricity=sqrt(3)/2)
  317. assert e3.hradius == 2
  318. # filter(None, iterator) filters out anything falsey, including 0
  319. # eccentricity would be filtered out in this case and the constructor would throw an error
  320. e4 = Ellipse(Point(0, 0), hradius=1, eccentricity=0)
  321. assert e4.vradius == 1
  322. #tests for eccentricity > 1
  323. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = S(3)/2))
  324. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity=sec(5)))
  325. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity=S.Pi-S(2)))
  326. #tests for eccentricity = 1
  327. #if vradius is not defined
  328. assert Ellipse(None, 1, None, 1).length == 2
  329. #if hradius is not defined
  330. raises(GeometryError, lambda: Ellipse(None, None, 1, eccentricity = 1))
  331. #tests for eccentricity < 0
  332. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = -3))
  333. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = -0.5))
  334. def test_ellipse_random_point():
  335. y1 = Symbol('y1', real=True)
  336. e3 = Ellipse(Point(0, 0), y1, y1)
  337. rx, ry = Symbol('rx'), Symbol('ry')
  338. for ind in range(0, 5):
  339. r = e3.random_point()
  340. # substitution should give zero*y1**2
  341. assert e3.equation(rx, ry).subs(zip((rx, ry), r.args)).equals(0)
  342. # test for the case with seed
  343. r = e3.random_point(seed=1)
  344. assert e3.equation(rx, ry).subs(zip((rx, ry), r.args)).equals(0)
  345. def test_repr():
  346. assert repr(Circle((0, 1), 2)) == 'Circle(Point2D(0, 1), 2)'
  347. def test_transform():
  348. c = Circle((1, 1), 2)
  349. assert c.scale(-1) == Circle((-1, 1), 2)
  350. assert c.scale(y=-1) == Circle((1, -1), 2)
  351. assert c.scale(2) == Ellipse((2, 1), 4, 2)
  352. assert Ellipse((0, 0), 2, 3).scale(2, 3, (4, 5)) == \
  353. Ellipse(Point(-4, -10), 4, 9)
  354. assert Circle((0, 0), 2).scale(2, 3, (4, 5)) == \
  355. Ellipse(Point(-4, -10), 4, 6)
  356. assert Ellipse((0, 0), 2, 3).scale(3, 3, (4, 5)) == \
  357. Ellipse(Point(-8, -10), 6, 9)
  358. assert Circle((0, 0), 2).scale(3, 3, (4, 5)) == \
  359. Circle(Point(-8, -10), 6)
  360. assert Circle(Point(-8, -10), 6).scale(Rational(1, 3), Rational(1, 3), (4, 5)) == \
  361. Circle((0, 0), 2)
  362. assert Circle((0, 0), 2).translate(4, 5) == \
  363. Circle((4, 5), 2)
  364. assert Circle((0, 0), 2).scale(3, 3) == \
  365. Circle((0, 0), 6)
  366. def test_bounds():
  367. e1 = Ellipse(Point(0, 0), 3, 5)
  368. e2 = Ellipse(Point(2, -2), 7, 7)
  369. c1 = Circle(Point(2, -2), 7)
  370. c2 = Circle(Point(-2, 0), Point(0, 2), Point(2, 0))
  371. assert e1.bounds == (-3, -5, 3, 5)
  372. assert e2.bounds == (-5, -9, 9, 5)
  373. assert c1.bounds == (-5, -9, 9, 5)
  374. assert c2.bounds == (-2, -2, 2, 2)
  375. def test_reflect():
  376. b = Symbol('b')
  377. m = Symbol('m')
  378. l = Line((0, b), slope=m)
  379. t1 = Triangle((0, 0), (1, 0), (2, 3))
  380. assert t1.area == -t1.reflect(l).area
  381. e = Ellipse((1, 0), 1, 2)
  382. assert e.area == -e.reflect(Line((1, 0), slope=0)).area
  383. assert e.area == -e.reflect(Line((1, 0), slope=oo)).area
  384. raises(NotImplementedError, lambda: e.reflect(Line((1, 0), slope=m)))
  385. assert Circle((0, 1), 1).reflect(Line((0, 0), (1, 1))) == Circle(Point2D(1, 0), -1)
  386. def test_is_tangent():
  387. e1 = Ellipse(Point(0, 0), 3, 5)
  388. c1 = Circle(Point(2, -2), 7)
  389. assert e1.is_tangent(Point(0, 0)) is False
  390. assert e1.is_tangent(Point(3, 0)) is False
  391. assert e1.is_tangent(e1) is True
  392. assert e1.is_tangent(Ellipse((0, 0), 1, 2)) is False
  393. assert e1.is_tangent(Ellipse((0, 0), 3, 2)) is True
  394. assert c1.is_tangent(Ellipse((2, -2), 7, 1)) is True
  395. assert c1.is_tangent(Circle((11, -2), 2)) is True
  396. assert c1.is_tangent(Circle((7, -2), 2)) is True
  397. assert c1.is_tangent(Ray((-5, -2), (-15, -20))) is False
  398. assert c1.is_tangent(Ray((-3, -2), (-15, -20))) is False
  399. assert c1.is_tangent(Ray((-3, -22), (15, 20))) is False
  400. assert c1.is_tangent(Ray((9, 20), (9, -20))) is True
  401. assert e1.is_tangent(Segment((2, 2), (-7, 7))) is False
  402. assert e1.is_tangent(Segment((0, 0), (1, 2))) is False
  403. assert c1.is_tangent(Segment((0, 0), (-5, -2))) is False
  404. assert e1.is_tangent(Segment((3, 0), (12, 12))) is False
  405. assert e1.is_tangent(Segment((12, 12), (3, 0))) is False
  406. assert e1.is_tangent(Segment((-3, 0), (3, 0))) is False
  407. assert e1.is_tangent(Segment((-3, 5), (3, 5))) is True
  408. assert e1.is_tangent(Line((10, 0), (10, 10))) is False
  409. assert e1.is_tangent(Line((0, 0), (1, 1))) is False
  410. assert e1.is_tangent(Line((-3, 0), (-2.99, -0.001))) is False
  411. assert e1.is_tangent(Line((-3, 0), (-3, 1))) is True
  412. assert e1.is_tangent(Polygon((0, 0), (5, 5), (5, -5))) is False
  413. assert e1.is_tangent(Polygon((-100, -50), (-40, -334), (-70, -52))) is False
  414. assert e1.is_tangent(Polygon((-3, 0), (3, 0), (0, 1))) is False
  415. assert e1.is_tangent(Polygon((-3, 0), (3, 0), (0, 5))) is False
  416. assert e1.is_tangent(Polygon((-3, 0), (0, -5), (3, 0), (0, 5))) is False
  417. assert e1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is True
  418. assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is False
  419. assert e1.is_tangent(Polygon((0, 0), (3, 0), (7, 7), (0, 5))) is False
  420. assert e1.is_tangent(Polygon((3, 12), (3, -12), (6, 5))) is True
  421. assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is False
  422. assert e1.is_tangent(Polygon((3, 0), (5, 7), (6, -5))) is False
  423. raises(TypeError, lambda: e1.is_tangent(Point(0, 0, 0)))
  424. raises(TypeError, lambda: e1.is_tangent(Rational(5)))
  425. def test_parameter_value():
  426. t = Symbol('t')
  427. e = Ellipse(Point(0, 0), 3, 5)
  428. assert e.parameter_value((3, 0), t) == {t: 0}
  429. raises(ValueError, lambda: e.parameter_value((4, 0), t))
  430. @slow
  431. def test_second_moment_of_area():
  432. x, y = symbols('x, y')
  433. e = Ellipse(Point(0, 0), 5, 4)
  434. I_yy = 2*4*integrate(sqrt(25 - x**2)*x**2, (x, -5, 5))/5
  435. I_xx = 2*5*integrate(sqrt(16 - y**2)*y**2, (y, -4, 4))/4
  436. Y = 3*sqrt(1 - x**2/5**2)
  437. I_xy = integrate(integrate(y, (y, -Y, Y))*x, (x, -5, 5))
  438. assert I_yy == e.second_moment_of_area()[1]
  439. assert I_xx == e.second_moment_of_area()[0]
  440. assert I_xy == e.second_moment_of_area()[2]
  441. #checking for other point
  442. t1 = e.second_moment_of_area(Point(6,5))
  443. t2 = (580*pi, 845*pi, 600*pi)
  444. assert t1==t2
  445. def test_section_modulus_and_polar_second_moment_of_area():
  446. d = Symbol('d', positive=True)
  447. c = Circle((3, 7), 8)
  448. assert c.polar_second_moment_of_area() == 2048*pi
  449. assert c.section_modulus() == (128*pi, 128*pi)
  450. c = Circle((2, 9), d/2)
  451. assert c.polar_second_moment_of_area() == pi*d**3*Abs(d)/64 + pi*d*Abs(d)**3/64
  452. assert c.section_modulus() == (pi*d**3/S(32), pi*d**3/S(32))
  453. a, b = symbols('a, b', positive=True)
  454. e = Ellipse((4, 6), a, b)
  455. assert e.section_modulus() == (pi*a*b**2/S(4), pi*a**2*b/S(4))
  456. assert e.polar_second_moment_of_area() == pi*a**3*b/S(4) + pi*a*b**3/S(4)
  457. e = e.rotate(pi/2) # no change in polar and section modulus
  458. assert e.section_modulus() == (pi*a**2*b/S(4), pi*a*b**2/S(4))
  459. assert e.polar_second_moment_of_area() == pi*a**3*b/S(4) + pi*a*b**3/S(4)
  460. e = Ellipse((a, b), 2, 6)
  461. assert e.section_modulus() == (18*pi, 6*pi)
  462. assert e.polar_second_moment_of_area() == 120*pi
  463. e = Ellipse(Point(0, 0), 2, 2)
  464. assert e.section_modulus() == (2*pi, 2*pi)
  465. assert e.section_modulus(Point(2, 2)) == (2*pi, 2*pi)
  466. assert e.section_modulus((2, 2)) == (2*pi, 2*pi)
  467. def test_circumference():
  468. M = Symbol('M')
  469. m = Symbol('m')
  470. assert Ellipse(Point(0, 0), M, m).circumference == 4 * M * elliptic_e((M ** 2 - m ** 2) / M**2)
  471. assert Ellipse(Point(0, 0), 5, 4).circumference == 20 * elliptic_e(S(9) / 25)
  472. # circle
  473. assert Ellipse(None, 1, None, 0).circumference == 2*pi
  474. # test numerically
  475. assert abs(Ellipse(None, hradius=5, vradius=3).circumference.evalf(16) - 25.52699886339813) < 1e-10
  476. def test_issue_15259():
  477. assert Circle((1, 2), 0) == Point(1, 2)
  478. def test_issue_15797_equals():
  479. Ri = 0.024127189424130748
  480. Ci = (0.0864931002830291, 0.0819863295239654)
  481. A = Point(0, 0.0578591400998346)
  482. c = Circle(Ci, Ri) # evaluated
  483. assert c.is_tangent(c.tangent_lines(A)[0]) == True
  484. assert c.center.x.is_Rational
  485. assert c.center.y.is_Rational
  486. assert c.radius.is_Rational
  487. u = Circle(Ci, Ri, evaluate=False) # unevaluated
  488. assert u.center.x.is_Float
  489. assert u.center.y.is_Float
  490. assert u.radius.is_Float
  491. def test_auxiliary_circle():
  492. x, y, a, b = symbols('x y a b')
  493. e = Ellipse((x, y), a, b)
  494. # the general result
  495. assert e.auxiliary_circle() == Circle((x, y), Max(a, b))
  496. # a special case where Ellipse is a Circle
  497. assert Circle((3, 4), 8).auxiliary_circle() == Circle((3, 4), 8)
  498. def test_director_circle():
  499. x, y, a, b = symbols('x y a b')
  500. e = Ellipse((x, y), a, b)
  501. # the general result
  502. assert e.director_circle() == Circle((x, y), sqrt(a**2 + b**2))
  503. # a special case where Ellipse is a Circle
  504. assert Circle((3, 4), 8).director_circle() == Circle((3, 4), 8*sqrt(2))
  505. def test_evolute():
  506. #ellipse centered at h,k
  507. x, y, h, k = symbols('x y h k',real = True)
  508. a, b = symbols('a b')
  509. e = Ellipse(Point(h, k), a, b)
  510. t1 = (e.hradius*(x - e.center.x))**Rational(2, 3)
  511. t2 = (e.vradius*(y - e.center.y))**Rational(2, 3)
  512. E = t1 + t2 - (e.hradius**2 - e.vradius**2)**Rational(2, 3)
  513. assert e.evolute() == E
  514. #Numerical Example
  515. e = Ellipse(Point(1, 1), 6, 3)
  516. t1 = (6*(x - 1))**Rational(2, 3)
  517. t2 = (3*(y - 1))**Rational(2, 3)
  518. E = t1 + t2 - (27)**Rational(2, 3)
  519. assert e.evolute() == E
  520. def test_svg():
  521. e1 = Ellipse(Point(1, 0), 3, 2)
  522. assert e1._svg(2, "#FFAAFF") == '<ellipse fill="#FFAAFF" stroke="#555555" stroke-width="4.0" opacity="0.6" cx="1.00000000000000" cy="0" rx="3.00000000000000" ry="2.00000000000000"/>'