Open In App

Python – Sympy Polygon.distance() method

In Sympy, the function Polygon.distance() is used to return the shortest distance between the given polygon and o. If o is a point, then given polygon does not need to be convex. But If o is another polygon then, the given polygon and o must be convex.

Syntax: Polygon.distance(o)

Parameters:
 o:Point or Polygon

Returns: the shortest distance between the given polygon and o.

Example #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)

Output:

sqrt(13)

Example #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)

Output:

sqrt(61)

Article Tags :