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
因为Calvin没有回复,我就自己来发这个答案。
最后发现,画多条线其实是最简单的方法。并不需要做什么特别复杂的事情。