如何绘制可拖动的多边形

4
关于这个Matplotlib示例,它绘制了可拖动矩形(链接),我尝试使用多边形实现相同的功能。
到目前为止,我能够在画布上绘制一个多边形并将其拖动到某个位置。但如果我松开鼠标按钮,就无法再次移动多边形,这是我的问题所在。我想通过每次按下鼠标来拖放多边形,任意次数。
我还注意到,在移动多边形后,我仍然可以单击多边形原来的位置并再次将其拖动。因此,初始的 geometry 必须被保存在某个地方,但我认为它应该被覆盖。 编辑: 如下面的评论建议的那样,我会添加修补程序,而不是集合,因为我只会绘制一个多边形(参见旧代码中的注释)。此外,我关闭了多边形,以演示您只能通过单击多边形内部而不是边缘来拖动修补程序。
如果我想第二次拖动该多边形,它会自动跳回到其初始位置。
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
#from matplotlib.collections import PatchCollection

class DraggablePolygon:
    lock = None
    def __init__(self, polygon):
        self.poly = polygon
        self.press = None

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.poly.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.poly.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.poly.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)

    def on_press(self, event):
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.poly.axes: return
        if DraggablePolygon.lock is not None: return
        contains, attrd = self.poly.contains(event)
        if not contains: return
        x0, y0 = geometry[0]
        self.press = x0, y0, event.xdata, event.ydata
        DraggablePolygon.lock = self

    def on_motion(self, event):
        'on motion we will move the rect if the mouse is over us'
        if DraggablePolygon.lock is not self:
            return
        if event.inaxes != self.poly.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        xdx = [i+dx for i,_ in geometry]
        ydy = [i+dy for _,i in geometry]
        self.newGeometry = [[a, b] for a, b in zip(xdx, ydy)]
        #polygon = Polygon(self.newGeometry, closed=False, fill=False, linewidth=3, color='#F97306')
        #patches = []
        #patches.append(polygon)
        #plt.cla()
        #p = PatchCollection(patches, match_original=True)
        #ax.add_collection(p)

        self.poly.set_xy(newGeometry)  # this will set the vertices of the polygon

        self.poly.figure.canvas.draw()

    def on_release(self, event):
        'on release we reset the press data'
        if DraggablePolygon.lock is not self:
            return

        self.press = None
        DraggablePolygon.lock = None

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.poly.figure.canvas.mpl_disconnect(self.cidpress)
        self.poly.figure.canvas.mpl_disconnect(self.cidrelease)
        self.poly.figure.canvas.mpl_disconnect(self.cidmotion)


fig = plt.figure()
ax = fig.add_subplot(111)

geometry = [[0.0,0.0],[0.1,0.05],[0.2,0.15],[0.3,0.20],[0.4,0.25],[0.5,0.30],
        [0.6,0.25],[0.7,0.15],[0.8,0.05],[0.9,0.025],[1.0,0.0]]

patches = []
polygon = plt.Polygon(geometry, closed=True, fill=False, linewidth=3, color='#F97306')
#patches.append(polygon)
#p = PatchCollection(patches, match_original=True)
#ax.add_collection(p)

ax.add_patch(polygon)

dp = DraggablePolygon(polygon)
dp.connect()

plt.show()

我认为geometrynewGeometry的定义必须位于代码中不同的位置,但经过几次尝试后,我无法找到一个可行的解决方案。有人能找到我犯的错误吗?


每次鼠标移动时都创建一个全新的PatchCollection?那肯定不是解决方案。我可以问一下,为什么你要为单个多边形使用集合呢? - ImportanceOfBeingErnest
诚实的回答:因为我不知道其他方法。我是一个初学者,试图从示例中学习(如我的问题中所链接的)。是的,我只会使用单个多边形,所以我认为我真的不需要集合。当鼠标移动时,我只想更新多边形的几何形状并重新绘制它。 - Philipp
1
好的,对于矩形,您有 rect.set_x,但是这在多边形中不存在。相反,您需要通过 polygon.set_xy 一次性设置所有顶点。 - ImportanceOfBeingErnest
非常有帮助!但仍然存在一个问题,我无法从其实际位置第二次或第三次拖动多边形。也许是因为我在on_press中调用了geometry而不是newGeometry... - Philipp
1
是的,geometry没有在类内定义,因此它将始终保持初始位置。 - ImportanceOfBeingErnest
显示剩余2条评论
1个回答

1

在问题下面的评论帮助我最终找到了可用的代码。也许这不是最好的、最符合Python风格的方式,但它实现了我想要的功能。

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

class DraggablePolygon:
    lock = None
    def __init__(self):
        print('__init__')
        self.press = None

        fig = plt.figure()
        ax = fig.add_subplot(111)

        self.geometry = [[0.0,0.0],[0.1,0.05],[0.2,0.15],[0.3,0.20],[0.4,0.25],[0.5,0.30],
                    [0.6,0.25],[0.7,0.15],[0.8,0.05],[0.9,0.025],[1.0,0.0]]
        self.newGeometry = []
        poly = plt.Polygon(self.geometry, closed=True, fill=False, linewidth=3, color='#F97306')
        ax.add_patch(poly)
        self.poly = poly

    def connect(self):
        'connect to all the events we need'
        print('connect')
        self.cidpress = self.poly.figure.canvas.mpl_connect(
        'button_press_event', self.on_press)
        self.cidrelease = self.poly.figure.canvas.mpl_connect(
        'button_release_event', self.on_release)
        self.cidmotion = self.poly.figure.canvas.mpl_connect(
        'motion_notify_event', self.on_motion)

    def on_press(self, event):
        'on button press we will see if the mouse is over us and store some data'
        print('on_press')
        if event.inaxes != self.poly.axes: return
        if DraggablePolygon.lock is not None: return
        contains, attrd = self.poly.contains(event)
        if not contains: return

        if not self.newGeometry:
            x0, y0 = self.geometry[0]
        else:
            x0, y0 = self.newGeometry[0]

        self.press = x0, y0, event.xdata, event.ydata
        DraggablePolygon.lock = self

    def on_motion(self, event):
        'on motion we will move the rect if the mouse is over us'
        if DraggablePolygon.lock is not self:
            return
        if event.inaxes != self.poly.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress

        xdx = [i+dx for i,_ in self.geometry]
        ydy = [i+dy for _,i in self.geometry]
        self.newGeometry = [[a, b] for a, b in zip(xdx, ydy)]
        self.poly.set_xy(self.newGeometry)
        self.poly.figure.canvas.draw()

    def on_release(self, event):
        'on release we reset the press data'
        print('on_release')
        if DraggablePolygon.lock is not self:
            return

        self.press = None
        DraggablePolygon.lock = None
        self.geometry = self.newGeometry


    def disconnect(self):
        'disconnect all the stored connection ids'
        print('disconnect')
        self.poly.figure.canvas.mpl_disconnect(self.cidpress)
        self.poly.figure.canvas.mpl_disconnect(self.cidrelease)
        self.poly.figure.canvas.mpl_disconnect(self.cidmotion)


dp = DraggablePolygon()
dp.connect()

plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接