test_ellipse.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 False
  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)
  233. ) == [Point(5.0, 0, evaluate=False)]
  234. assert intersection(Ellipse(Point(0, 0), 5, 17), Ellipse(Point(4, 0), 0.999, 0.2)) == []
  235. assert Circle((0, 0), S.Half).intersection(
  236. Triangle((-1, 0), (1, 0), (0, 1))) == [
  237. Point(Rational(-1, 2), 0), Point(S.Half, 0)]
  238. raises(TypeError, lambda: intersection(e2, Line((0, 0, 0), (0, 0, 1))))
  239. raises(TypeError, lambda: intersection(e2, Rational(12)))
  240. raises(TypeError, lambda: Ellipse.intersection(e2, 1))
  241. # some special case intersections
  242. csmall = Circle(p1, 3)
  243. cbig = Circle(p1, 5)
  244. cout = Circle(Point(5, 5), 1)
  245. # one circle inside of another
  246. assert csmall.intersection(cbig) == []
  247. # separate circles
  248. assert csmall.intersection(cout) == []
  249. # coincident circles
  250. assert csmall.intersection(csmall) == csmall
  251. v = sqrt(2)
  252. t1 = Triangle(Point(0, v), Point(0, -v), Point(v, 0))
  253. points = intersection(t1, c1)
  254. assert len(points) == 4
  255. assert Point(0, 1) in points
  256. assert Point(0, -1) in points
  257. assert Point(v/2, v/2) in points
  258. assert Point(v/2, -v/2) in points
  259. circ = Circle(Point(0, 0), 5)
  260. elip = Ellipse(Point(0, 0), 5, 20)
  261. assert intersection(circ, elip) in \
  262. [[Point(5, 0), Point(-5, 0)], [Point(-5, 0), Point(5, 0)]]
  263. assert elip.tangent_lines(Point(0, 0)) == []
  264. elip = Ellipse(Point(0, 0), 3, 2)
  265. assert elip.tangent_lines(Point(3, 0)) == \
  266. [Line(Point(3, 0), Point(3, -12))]
  267. e1 = Ellipse(Point(0, 0), 5, 10)
  268. e2 = Ellipse(Point(2, 1), 4, 8)
  269. a = Rational(53, 17)
  270. c = 2*sqrt(3991)/17
  271. ans = [Point(a - c/8, a/2 + c), Point(a + c/8, a/2 - c)]
  272. assert e1.intersection(e2) == ans
  273. e2 = Ellipse(Point(x, y), 4, 8)
  274. c = sqrt(3991)
  275. ans = [Point(-c/68 + a, c*Rational(2, 17) + a/2), Point(c/68 + a, c*Rational(-2, 17) + a/2)]
  276. assert [p.subs({x: 2, y:1}) for p in e1.intersection(e2)] == ans
  277. # Combinations of above
  278. assert e3.is_tangent(e3.tangent_lines(p1 + Point(y1, 0))[0])
  279. e = Ellipse((1, 2), 3, 2)
  280. assert e.tangent_lines(Point(10, 0)) == \
  281. [Line(Point(10, 0), Point(1, 0)),
  282. Line(Point(10, 0), Point(Rational(14, 5), Rational(18, 5)))]
  283. # encloses_point
  284. e = Ellipse((0, 0), 1, 2)
  285. assert e.encloses_point(e.center)
  286. assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
  287. assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
  288. assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
  289. assert e.encloses_point(
  290. e.center + Point(e.hradius + Rational(1, 10), 0)) is False
  291. e = Ellipse((0, 0), 2, 1)
  292. assert e.encloses_point(e.center)
  293. assert e.encloses_point(e.center + Point(0, e.vradius - Rational(1, 10)))
  294. assert e.encloses_point(e.center + Point(e.hradius - Rational(1, 10), 0))
  295. assert e.encloses_point(e.center + Point(e.hradius, 0)) is False
  296. assert e.encloses_point(
  297. e.center + Point(e.hradius + Rational(1, 10), 0)) is False
  298. assert c1.encloses_point(Point(1, 0)) is False
  299. assert c1.encloses_point(Point(0.3, 0.4)) is True
  300. assert e.scale(2, 3) == Ellipse((0, 0), 4, 3)
  301. assert e.scale(3, 6) == Ellipse((0, 0), 6, 6)
  302. assert e.rotate(pi) == e
  303. assert e.rotate(pi, (1, 2)) == Ellipse(Point(2, 4), 2, 1)
  304. raises(NotImplementedError, lambda: e.rotate(pi/3))
  305. # Circle rotation tests (Issue #11743)
  306. # Link - https://github.com/sympy/sympy/issues/11743
  307. cir = Circle(Point(1, 0), 1)
  308. assert cir.rotate(pi/2) == Circle(Point(0, 1), 1)
  309. assert cir.rotate(pi/3) == Circle(Point(S.Half, sqrt(3)/2), 1)
  310. assert cir.rotate(pi/3, Point(1, 0)) == Circle(Point(1, 0), 1)
  311. assert cir.rotate(pi/3, Point(0, 1)) == Circle(Point(S.Half + sqrt(3)/2, S.Half + sqrt(3)/2), 1)
  312. def test_construction():
  313. e1 = Ellipse(hradius=2, vradius=1, eccentricity=None)
  314. assert e1.eccentricity == sqrt(3)/2
  315. e2 = Ellipse(hradius=2, vradius=None, eccentricity=sqrt(3)/2)
  316. assert e2.vradius == 1
  317. e3 = Ellipse(hradius=None, vradius=1, eccentricity=sqrt(3)/2)
  318. assert e3.hradius == 2
  319. # filter(None, iterator) filters out anything falsey, including 0
  320. # eccentricity would be filtered out in this case and the constructor would throw an error
  321. e4 = Ellipse(Point(0, 0), hradius=1, eccentricity=0)
  322. assert e4.vradius == 1
  323. #tests for eccentricity > 1
  324. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = S(3)/2))
  325. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity=sec(5)))
  326. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity=S.Pi-S(2)))
  327. #tests for eccentricity = 1
  328. #if vradius is not defined
  329. assert Ellipse(None, 1, None, 1).length == 2
  330. #if hradius is not defined
  331. raises(GeometryError, lambda: Ellipse(None, None, 1, eccentricity = 1))
  332. #tests for eccentricity < 0
  333. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = -3))
  334. raises(GeometryError, lambda: Ellipse(Point(3, 1), hradius=3, eccentricity = -0.5))
  335. def test_ellipse_random_point():
  336. y1 = Symbol('y1', real=True)
  337. e3 = Ellipse(Point(0, 0), y1, y1)
  338. rx, ry = Symbol('rx'), Symbol('ry')
  339. for ind in range(0, 5):
  340. r = e3.random_point()
  341. # substitution should give zero*y1**2
  342. assert e3.equation(rx, ry).subs(zip((rx, ry), r.args)).equals(0)
  343. # test for the case with seed
  344. r = e3.random_point(seed=1)
  345. assert e3.equation(rx, ry).subs(zip((rx, ry), r.args)).equals(0)
  346. def test_repr():
  347. assert repr(Circle((0, 1), 2)) == 'Circle(Point2D(0, 1), 2)'
  348. def test_transform():
  349. c = Circle((1, 1), 2)
  350. assert c.scale(-1) == Circle((-1, 1), 2)
  351. assert c.scale(y=-1) == Circle((1, -1), 2)
  352. assert c.scale(2) == Ellipse((2, 1), 4, 2)
  353. assert Ellipse((0, 0), 2, 3).scale(2, 3, (4, 5)) == \
  354. Ellipse(Point(-4, -10), 4, 9)
  355. assert Circle((0, 0), 2).scale(2, 3, (4, 5)) == \
  356. Ellipse(Point(-4, -10), 4, 6)
  357. assert Ellipse((0, 0), 2, 3).scale(3, 3, (4, 5)) == \
  358. Ellipse(Point(-8, -10), 6, 9)
  359. assert Circle((0, 0), 2).scale(3, 3, (4, 5)) == \
  360. Circle(Point(-8, -10), 6)
  361. assert Circle(Point(-8, -10), 6).scale(Rational(1, 3), Rational(1, 3), (4, 5)) == \
  362. Circle((0, 0), 2)
  363. assert Circle((0, 0), 2).translate(4, 5) == \
  364. Circle((4, 5), 2)
  365. assert Circle((0, 0), 2).scale(3, 3) == \
  366. Circle((0, 0), 6)
  367. def test_bounds():
  368. e1 = Ellipse(Point(0, 0), 3, 5)
  369. e2 = Ellipse(Point(2, -2), 7, 7)
  370. c1 = Circle(Point(2, -2), 7)
  371. c2 = Circle(Point(-2, 0), Point(0, 2), Point(2, 0))
  372. assert e1.bounds == (-3, -5, 3, 5)
  373. assert e2.bounds == (-5, -9, 9, 5)
  374. assert c1.bounds == (-5, -9, 9, 5)
  375. assert c2.bounds == (-2, -2, 2, 2)
  376. def test_reflect():
  377. b = Symbol('b')
  378. m = Symbol('m')
  379. l = Line((0, b), slope=m)
  380. t1 = Triangle((0, 0), (1, 0), (2, 3))
  381. assert t1.area == -t1.reflect(l).area
  382. e = Ellipse((1, 0), 1, 2)
  383. assert e.area == -e.reflect(Line((1, 0), slope=0)).area
  384. assert e.area == -e.reflect(Line((1, 0), slope=oo)).area
  385. raises(NotImplementedError, lambda: e.reflect(Line((1, 0), slope=m)))
  386. assert Circle((0, 1), 1).reflect(Line((0, 0), (1, 1))) == Circle(Point2D(1, 0), -1)
  387. def test_is_tangent():
  388. e1 = Ellipse(Point(0, 0), 3, 5)
  389. c1 = Circle(Point(2, -2), 7)
  390. assert e1.is_tangent(Point(0, 0)) is False
  391. assert e1.is_tangent(Point(3, 0)) is False
  392. assert e1.is_tangent(e1) is True
  393. assert e1.is_tangent(Ellipse((0, 0), 1, 2)) is False
  394. assert e1.is_tangent(Ellipse((0, 0), 3, 2)) is True
  395. assert c1.is_tangent(Ellipse((2, -2), 7, 1)) is True
  396. assert c1.is_tangent(Circle((11, -2), 2)) is True
  397. assert c1.is_tangent(Circle((7, -2), 2)) is True
  398. assert c1.is_tangent(Ray((-5, -2), (-15, -20))) is False
  399. assert c1.is_tangent(Ray((-3, -2), (-15, -20))) is False
  400. assert c1.is_tangent(Ray((-3, -22), (15, 20))) is False
  401. assert c1.is_tangent(Ray((9, 20), (9, -20))) is True
  402. assert c1.is_tangent(Ray((2, 5), (9, 5))) is True
  403. assert c1.is_tangent(Segment((2, 5), (9, 5))) is True
  404. assert e1.is_tangent(Segment((2, 2), (-7, 7))) is False
  405. assert e1.is_tangent(Segment((0, 0), (1, 2))) is False
  406. assert c1.is_tangent(Segment((0, 0), (-5, -2))) is False
  407. assert e1.is_tangent(Segment((3, 0), (12, 12))) is False
  408. assert e1.is_tangent(Segment((12, 12), (3, 0))) is False
  409. assert e1.is_tangent(Segment((-3, 0), (3, 0))) is False
  410. assert e1.is_tangent(Segment((-3, 5), (3, 5))) is True
  411. assert e1.is_tangent(Line((10, 0), (10, 10))) is False
  412. assert e1.is_tangent(Line((0, 0), (1, 1))) is False
  413. assert e1.is_tangent(Line((-3, 0), (-2.99, -0.001))) is False
  414. assert e1.is_tangent(Line((-3, 0), (-3, 1))) is True
  415. assert e1.is_tangent(Polygon((0, 0), (5, 5), (5, -5))) is False
  416. assert e1.is_tangent(Polygon((-100, -50), (-40, -334), (-70, -52))) is False
  417. assert e1.is_tangent(Polygon((-3, 0), (3, 0), (0, 1))) is False
  418. assert e1.is_tangent(Polygon((-3, 0), (3, 0), (0, 5))) is False
  419. assert e1.is_tangent(Polygon((-3, 0), (0, -5), (3, 0), (0, 5))) is False
  420. assert e1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is True
  421. assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is False
  422. assert e1.is_tangent(Polygon((0, 0), (3, 0), (7, 7), (0, 5))) is False
  423. assert e1.is_tangent(Polygon((3, 12), (3, -12), (6, 5))) is False
  424. assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is False
  425. assert e1.is_tangent(Polygon((3, 0), (5, 7), (6, -5))) is False
  426. assert c1.is_tangent(Segment((0, 0), (-5, -2))) is False
  427. assert e1.is_tangent(Segment((-3, 0), (3, 0))) is False
  428. assert e1.is_tangent(Segment((-3, 5), (3, 5))) is True
  429. assert e1.is_tangent(Polygon((0, 0), (5, 5), (5, -5))) is False
  430. assert e1.is_tangent(Polygon((-100, -50), (-40, -334), (-70, -52))) is False
  431. assert e1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is True
  432. assert c1.is_tangent(Polygon((-3, -5), (-3, 5), (3, 5), (3, -5))) is False
  433. assert e1.is_tangent(Polygon((3, 12), (3, -12), (0, -5), (0, 5))) is False
  434. assert e1.is_tangent(Polygon((3, 0), (5, 7), (6, -5))) is False
  435. raises(TypeError, lambda: e1.is_tangent(Point(0, 0, 0)))
  436. raises(TypeError, lambda: e1.is_tangent(Rational(5)))
  437. def test_parameter_value():
  438. t = Symbol('t')
  439. e = Ellipse(Point(0, 0), 3, 5)
  440. assert e.parameter_value((3, 0), t) == {t: 0}
  441. raises(ValueError, lambda: e.parameter_value((4, 0), t))
  442. @slow
  443. def test_second_moment_of_area():
  444. x, y = symbols('x, y')
  445. e = Ellipse(Point(0, 0), 5, 4)
  446. I_yy = 2*4*integrate(sqrt(25 - x**2)*x**2, (x, -5, 5))/5
  447. I_xx = 2*5*integrate(sqrt(16 - y**2)*y**2, (y, -4, 4))/4
  448. Y = 3*sqrt(1 - x**2/5**2)
  449. I_xy = integrate(integrate(y, (y, -Y, Y))*x, (x, -5, 5))
  450. assert I_yy == e.second_moment_of_area()[1]
  451. assert I_xx == e.second_moment_of_area()[0]
  452. assert I_xy == e.second_moment_of_area()[2]
  453. #checking for other point
  454. t1 = e.second_moment_of_area(Point(6,5))
  455. t2 = (580*pi, 845*pi, 600*pi)
  456. assert t1==t2
  457. def test_section_modulus_and_polar_second_moment_of_area():
  458. d = Symbol('d', positive=True)
  459. c = Circle((3, 7), 8)
  460. assert c.polar_second_moment_of_area() == 2048*pi
  461. assert c.section_modulus() == (128*pi, 128*pi)
  462. c = Circle((2, 9), d/2)
  463. assert c.polar_second_moment_of_area() == pi*d**3*Abs(d)/64 + pi*d*Abs(d)**3/64
  464. assert c.section_modulus() == (pi*d**3/S(32), pi*d**3/S(32))
  465. a, b = symbols('a, b', positive=True)
  466. e = Ellipse((4, 6), a, b)
  467. assert e.section_modulus() == (pi*a*b**2/S(4), pi*a**2*b/S(4))
  468. assert e.polar_second_moment_of_area() == pi*a**3*b/S(4) + pi*a*b**3/S(4)
  469. e = e.rotate(pi/2) # no change in polar and section modulus
  470. assert e.section_modulus() == (pi*a**2*b/S(4), pi*a*b**2/S(4))
  471. assert e.polar_second_moment_of_area() == pi*a**3*b/S(4) + pi*a*b**3/S(4)
  472. e = Ellipse((a, b), 2, 6)
  473. assert e.section_modulus() == (18*pi, 6*pi)
  474. assert e.polar_second_moment_of_area() == 120*pi
  475. e = Ellipse(Point(0, 0), 2, 2)
  476. assert e.section_modulus() == (2*pi, 2*pi)
  477. assert e.section_modulus(Point(2, 2)) == (2*pi, 2*pi)
  478. assert e.section_modulus((2, 2)) == (2*pi, 2*pi)
  479. def test_circumference():
  480. M = Symbol('M')
  481. m = Symbol('m')
  482. assert Ellipse(Point(0, 0), M, m).circumference == 4 * M * elliptic_e((M ** 2 - m ** 2) / M**2)
  483. assert Ellipse(Point(0, 0), 5, 4).circumference == 20 * elliptic_e(S(9) / 25)
  484. # circle
  485. assert Ellipse(None, 1, None, 0).circumference == 2*pi
  486. # test numerically
  487. assert abs(Ellipse(None, hradius=5, vradius=3).circumference.evalf(16) - 25.52699886339813) < 1e-10
  488. def test_issue_15259():
  489. assert Circle((1, 2), 0) == Point(1, 2)
  490. def test_issue_15797_equals():
  491. Ri = 0.024127189424130748
  492. Ci = (0.0864931002830291, 0.0819863295239654)
  493. A = Point(0, 0.0578591400998346)
  494. c = Circle(Ci, Ri) # evaluated
  495. assert c.is_tangent(c.tangent_lines(A)[0]) == True
  496. assert c.center.x.is_Rational
  497. assert c.center.y.is_Rational
  498. assert c.radius.is_Rational
  499. u = Circle(Ci, Ri, evaluate=False) # unevaluated
  500. assert u.center.x.is_Float
  501. assert u.center.y.is_Float
  502. assert u.radius.is_Float
  503. def test_auxiliary_circle():
  504. x, y, a, b = symbols('x y a b')
  505. e = Ellipse((x, y), a, b)
  506. # the general result
  507. assert e.auxiliary_circle() == Circle((x, y), Max(a, b))
  508. # a special case where Ellipse is a Circle
  509. assert Circle((3, 4), 8).auxiliary_circle() == Circle((3, 4), 8)
  510. def test_director_circle():
  511. x, y, a, b = symbols('x y a b')
  512. e = Ellipse((x, y), a, b)
  513. # the general result
  514. assert e.director_circle() == Circle((x, y), sqrt(a**2 + b**2))
  515. # a special case where Ellipse is a Circle
  516. assert Circle((3, 4), 8).director_circle() == Circle((3, 4), 8*sqrt(2))
  517. def test_evolute():
  518. #ellipse centered at h,k
  519. x, y, h, k = symbols('x y h k',real = True)
  520. a, b = symbols('a b')
  521. e = Ellipse(Point(h, k), a, b)
  522. t1 = (e.hradius*(x - e.center.x))**Rational(2, 3)
  523. t2 = (e.vradius*(y - e.center.y))**Rational(2, 3)
  524. E = t1 + t2 - (e.hradius**2 - e.vradius**2)**Rational(2, 3)
  525. assert e.evolute() == E
  526. #Numerical Example
  527. e = Ellipse(Point(1, 1), 6, 3)
  528. t1 = (6*(x - 1))**Rational(2, 3)
  529. t2 = (3*(y - 1))**Rational(2, 3)
  530. E = t1 + t2 - (27)**Rational(2, 3)
  531. assert e.evolute() == E
  532. def test_svg():
  533. e1 = Ellipse(Point(1, 0), 3, 2)
  534. 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"/>'