Kivy:编辑线条图

时间:2014-08-29 09:22:56

标签: python python-2.7 drawing kivy

我目前正在使用折线图来表示图中的边。能够显示方向也很好!

我希望有一种简单的方法来改变线条,以便它们有红色提示(或类似的东西)。

如果你愿意,我可以提供我的基本代码,但目前我根本没有实现这一点,我想它只是提供一个框架。

我的一个想法是在当前行上绘制一条红色的附加线,但是我不确定我可以在画布上给多个线条添加一个小部件吗? (这会导致问题)。

询问是否需要更多详细信息:)

修改

我用来绘制和编辑行的附加代码:

def __init__(self, **kwargs):
    super(GraphEdge, self).__init__(**kwargs)
    with self.canvas:
        Color(self.col, 1, 1, 1)
        self.line = Line(points=[100, 200, 200, 200], width = 2.0)
        self.center = ((self.line.points[0]+self.line.points[2])/2,(self.line.points[1]+self.line.points[3])/2)
        self.size = [math.sqrt(((self.line.points[0]-self.line.points[2])**2 + (self.line.points[1]-self.line.points[3])**2))]*2
    with self.canvas.after:
        Color(1,0,0,1)
        Line(points=[self.line.points[0],self.line.points[1], 400,400], width = 3)

上面的代码绘制了线条,最后一部分只是让我在画布上绘制第二行。

然后我需要更新这些行的位置,目前我的代码(第一行)如下:

    if self.collide_widget(node):
        distance_from_0 = [math.sqrt(((self.line.points[0]-node.center[0])**2 + (self.line.points[1]-node.center[1])**2))]*2
        distance_from_1 = [math.sqrt(((self.line.points[2]-node.center[0])**2 + (self.line.points[3]-node.center[1])**2))]*2

        if distance_from_0 <= distance_from_1:
            if (self.connected_point_0 is False):
                print "collision"                
                if node is not self.connected_node_1:
                    self.connected_point_0 = True
                    self.connected_node_0 = node
                    node.edgeList.append(self)
                    self.line.points = node.center + self.line.points[2:]
                    self.size = 50,50
                    self.center = (self.line.points[2],self.line.points[3])
            return True

这只是一个提出想法的片段。

我希望能够根据第一行的位置更新第二行(还有其他一些需要担心的事情,比如节点的大小,但这可以在以后处理)。

这种多重绘图理念很可能不是最佳方法,事实上使用其他东西会使这更容易!

1 个答案:

答案 0 :(得分:0)

由于凯文没有回复,我会自己发布答案。

最终,绘制多行是最简单的方法。没有什么特别特别的事情要做。