test_geometrysets.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from sympy.core.numbers import Rational
  2. from sympy.core.singleton import S
  3. from sympy.geometry import Circle, Line, Point, Polygon, Segment
  4. from sympy.sets import FiniteSet, Union, Intersection, EmptySet
  5. def test_booleans():
  6. """ test basic unions and intersections """
  7. half = S.Half
  8. p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)])
  9. p5, p6, p7 = map(Point, [(3, 2), (1, -1), (0, 2)])
  10. l1 = Line(Point(0,0), Point(1,1))
  11. l2 = Line(Point(half, half), Point(5,5))
  12. l3 = Line(p2, p3)
  13. l4 = Line(p3, p4)
  14. poly1 = Polygon(p1, p2, p3, p4)
  15. poly2 = Polygon(p5, p6, p7)
  16. poly3 = Polygon(p1, p2, p5)
  17. assert Union(l1, l2).equals(l1)
  18. assert Intersection(l1, l2).equals(l1)
  19. assert Intersection(l1, l4) == FiniteSet(Point(1,1))
  20. assert Intersection(Union(l1, l4), l3) == FiniteSet(Point(Rational(-1, 3), Rational(-1, 3)), Point(5, 1))
  21. assert Intersection(l1, FiniteSet(Point(7,-7))) == EmptySet
  22. assert Intersection(Circle(Point(0,0), 3), Line(p1,p2)) == FiniteSet(Point(-3,0), Point(3,0))
  23. assert Intersection(l1, FiniteSet(p1)) == FiniteSet(p1)
  24. assert Union(l1, FiniteSet(p1)) == l1
  25. fs = FiniteSet(Point(Rational(1, 3), 1), Point(Rational(2, 3), 0), Point(Rational(9, 5), Rational(1, 5)), Point(Rational(7, 3), 1))
  26. # test the intersection of polygons
  27. assert Intersection(poly1, poly2) == fs
  28. # make sure if we union polygons with subsets, the subsets go away
  29. assert Union(poly1, poly2, fs) == Union(poly1, poly2)
  30. # make sure that if we union with a FiniteSet that isn't a subset,
  31. # that the points in the intersection stop being listed
  32. assert Union(poly1, FiniteSet(Point(0,0), Point(3,5))) == Union(poly1, FiniteSet(Point(3,5)))
  33. # intersect two polygons that share an edge
  34. assert Intersection(poly1, poly3) == Union(FiniteSet(Point(Rational(3, 2), 1), Point(2, 1)), Segment(Point(0, 0), Point(1, 0)))