Python – Sympy Polygon.cut_section() Method

In Sympy, the function Polygon.cut_section() is used to get a tuple of two polygon segments(two-part of the polygon) that lie above and below the intersecting line respectively. It simply returns the two parts of the polygon intersected by the line. Returns None when no polygon exists above the line or below the line.

Syntax: Polygon.cut_section(line)

Returns: upper_polygon, lower_polygon: Polygon objects or None
 upper_polygon: is the polygon that lies above the given line
 lower_polygon: is the polygon that lies below the given line
 None: when no polygon exists above the line or below the line
 
Raises: 
 ValueError: When the line does not intersect the polygon

Example #1:

Python3




# import sympy import Point, Polygon, Line
from sympy import Point, Polygon, Line
  
# creating points using Point()
p1, p2, p3, p4 = map(Point, [(0, 2), (0, 0), (1, 0), (1, 2)])
  
# creating polygon using Polygon()
poly = Polygon(p1, p2, p3, p4)
  
# using cut_section()
cutSection = poly.cut_section(Line((0, 1), slope = 0))
  
print(cutSection)


Output:

(Polygon(Point2D(0, 2), Point2D(0, 1), Point2D(1, 1), Point2D(1, 2)), 
 Polygon(Point2D(0, 1), Point2D(0, 0), Point2D(1, 0), Point2D(1, 1)))

Example #2:

Python3




# import sympy import Point, Polygon, Line
from sympy import Point, Polygon, Line
  
# creating points using Point()
p1, p2, p3, p4 = map(Point, [(0, 2), (0, 0), (1, 0), (1, 2)])
  
# creating polygon using Polygon()
poly = Polygon(p1, p2, p3, p4)
  
# using cut_section()
cutSection = poly.cut_section(Line((0, 1), slope = 1))
  
print(cutSection)


Output:

(Triangle(Point2D(0, 2), Point2D(0, 1), Point2D(1, 2)), 
 Polygon(Point2D(0, 1), Point2D(0, 0), Point2D(1, 0), Point2D(1, 2)))