Python – Sympy Polygon.cut_section()方法
在Sympy中,函数Polygon.cut_section()是用来获取分别位于相交线上方和下方的两个多边形段(多边形的两个部分)的元组。它简单地返回与直线相交的多边形的两个部分。当线上或线下没有多边形存在时,返回无。
语法: Polygon.cut_section(line)
返回: upper_polygon, lower_polygon: Polygon objects or None
upper_polygon: 是位于给定直线之上的多边形。
lower_polygon: 是位于给定线以下的多边形。
None: 当线上或线下不存在多边形时。
Raises:
ValueError: 当线与多边形不相交时。
示例 #1:
# 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)
输出:
(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)))
示例 #2:
# 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)
输出:
(Triangle(Point2D(0, 2), Point2D(0, 1), Point2D(1, 2)),
Polygon(Point2D(0, 1), Point2D(0, 0), Point2D(1, 0), Point2D(1, 2)))