Python – Sympy Polygon.distance()方法
在Sympy中,函数Polygon.distance()用来返回给定的多边形与o之间的最短距离。但如果o是另一个多边形,那么,给定的多边形和o必须是凸的。
语法: Polygon.distance(o)
参数 :
o: Point or Polygon
返回: 给定的多边形与o之间的最短距离。
示例 #1:
# import sympy import Point, Polygon
from sympy import Point, Polygon
# 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 distance()
shortestDistance = poly.distance(Point(3, 5))
print(shortestDistance)
输出:
sqrt(13)
示例 #2:
# import sympy import Point, Polygon, RegularPolygon
from sympy import Point, Polygon, RegularPolygon
# creating points using Point()
p1, p2 = map(Point, [(0, 0), (7, 5)])
# creating polygon using Polygon() and RegularPolygon()
poly = Polygon(*RegularPolygon(p1, 1, 3).vertices)
# using distance()
shortestDistance = poly.distance(p2)
print(shortestDistance)
输出:
sqrt(61)