文章目录
前言
怎么把一个凸多边形向内偏移制定距离呢?
一、Python代码
import matplotlib.pyplot as plt
import numpy as np
# pip install pyclipper
import pyclipper
def OffSet(vertices, offset):
pco = pyclipper.PyclipperOffset()
pco.AddPath(vertices, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
solution = pco.Execute(-offset)
return solution[0]
points = np.array([[10, 10], [70, 10], [60, 50], [20, 50], [15, 40]])
offset_distance = 10
offset_points = OffSet(points, offset_distance)
print(offset_points)
plt.figure()
plt.grid(True)
plt.xlim(right=100)
plt.ylim(top=100)
ax = plt.gca()
ax.set_aspect('equal')
points = list(points)
points.append(points[0])
offset_points = list(offset_points)
offset_points.append(offset_points[0])
polygon = plt.Polygon(points, closed=True, fill=False, edgecolor='b')
ax.add_patch(polygon)
plt.draw()
plt.pause(1)
offset_polygon = plt.Polygon(offset_points, closed=True, fill=False, edgecolor='r')
ax.add_patch(offset_polygon)
plt.draw()
plt.show()