在Python中沿路径滑动形状?

时间:2017-09-26 13:37:20

标签: python polygons shapely

我想沿路径滑动任意多边形,并在整个路径上返回多边形轮廓的形状。例如,在下面的图片中,我希望蓝色多边形沿着红色路径滑动,产生绿色多边形:

circle slid along path

Shapely包有很多有用的功能,但不是这个功能。 缓冲区函数不处理任意形状,它只是循环扩展。

我可以在路径上制作精细点,在每个点创建圆的副本,然后执行级联联合,但这将非常缓慢并且边缘将有许多需要简化的点。

另一个选择是在圆圈中的每个线段之外创建矩形,在路径的每个线段上滑动,然后获得那些的级联联合。这也可能效率低下。

我确定之前有人可能会做过这个问题类型。 是否已经存在可以完成此任务的Python软件包,或者我没有考虑过的简单有效的解决方案?

以下是一段代码摘要:

from shapely.geometry import Point, LineString
import matplotlib
from matplotlib import pyplot
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

def plot_coords(ax, ob):
    x, y = ob.xy
    ax.plot(x, y, 'o', color='#000000', zorder=1)

def plot_line(ax, ob):
    x, y = ob.xy
    ax.plot(x, y, color='#FF2222', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2)

circle = Point([-2,0]).buffer(1.0, 4)
path = LineString([[0,0],[1,1],[2,-1]])

fig = pyplot.figure(1, figsize=(12,10), dpi=180)
ax = fig.add_subplot(121)
plot_coords(ax, path)
plot_line(ax, path)
polygon_patch = Polygon(circle.exterior.coords, True)
p = PatchCollection([polygon_patch], cmap=matplotlib.cm.jet, alpha=0.4)
ax.add_collection(p)

plot_coords(ax, circle.exterior)


ax.set_xlim(-4, 3)
ax.set_ylim(-2, 2)
ax.set_aspect(1)

pyplot.show()

python code output

0 个答案:

没有答案
相关问题