【发布时间】:2013-08-11 00:25:59
【问题描述】:
背景: 我有一个代码,它生成规则形状(在本例中为三角形)网络的笛卡尔坐标,然后将形状的顶点绘制在Tkinter 画布作为小圆圈。该过程是自动化的,只需要网络的高度和宽度即可获得画布输出。每个顶点都有标签“顶点”和顶点的编号。
问题: 我想自动将形状的顶点连接在一起(即点到点),我已经研究过使用 find_closest 和 find_overlapping 方法来做到这一点,但作为网络由相互成角度的顶点组成,我经常发现find_overlapping 不可靠(由于依赖于矩形信封),而find_closest 似乎仅限于找到一个连接。由于顶点不一定按顺序连接,因此不可能创建一个循环来简单地连接顶点 1 --> 顶点 2 等。
问题:有没有办法有效地得到所有顶点的相邻顶点,然后“连接点”而不依赖于使用手动方法(例如self.c.create_line(vertex_coord[1], vertex_coord[0], fill='black'))为每个连接单独创建点之间的线?是否可以分享一个这样的代码的小例子?
提前感谢您的帮助!
以下是我的代码画布组件的缩略版。
原型方法:
from data_generator import *
run_coordinate_gen=data_generator.network_coordinates()
run_coordinate_gen.generator_go()
class Network_Canvas:
def __init__(self, canvas):
self.canvas=canvas
canvas.focus_set()
self.canvas.create_oval(Vertex_Position[0], dimensions[0], fill='black', tags=('Vertex1', Network_Tag, Vertex_Tag))
self.canvas.create_oval(Vertex_Position[5], dimensions[5], fill='black', tags=('Vertex2', Network_Tag, Vertex_Tag))
try:
self.canvas.create_line(Line_Position[5] ,Line_Position[0] , fill='black' tags=(Network_Tag,'Line1', Line_Tag )) #Connection Between 1 and 6 (6_1), Line 1
except:
pass
#Note: Line_Position, Dimensions and Vertex_Position are all lists composed of (x,y) cartesian coordinates in this case.
这当然会被复制到整个网络中的每条线和顶点,但仅用于 90 个顶点。新版本需要更多数量级的顶点,我正在这样做:
新方法:
#Import updated coordinate generator and run it as before
class Network_Canvas:
def __init__(self, canvas):
self.canvas=canvas
canvas.focus_set()
for V in range(len(vertex_coord_xy)):
self.canvas.create_text(vertex_coord_xy[V]+Text_Distance, text=V+1, fill='black', tags=(V, 'Text'), font=('Helvetica', '9'))
self.canvas.create_oval(vertex_coord_xy[V],vertex_coord_xy[V]+Diameter, fill='black', outline='black', tags=(V, 'Vertex'))
#loop to fit connections here (?)
【问题讨论】:
-
如果你能解释为什么跟踪哪些顶点连接到哪个顶点并使用
create_line不是一个可接受的解决方案(对我来说似乎非常简单和高效),它会帮助我们想到另一个解决方案。 -
我正在寻找一种方法来自动跟踪每个顶点的连接,而无需手动更改每条新线的坐标。由于网络可能很广泛(> 1000 个顶点),因此手动输入连接不是一种实用的方法,并且在顶点之间画线的简单循环即
self.c.create_line(vertex_coord[i], vertex_coord[i+1])等不能正确地“连接点”。我目前的方法依赖于手动跟踪连接,但它很耗时并且对于大量顶点没有用处。希望有帮助:) -
另一个问题:当你写
self.c.create_line(vertex_coord[0], vertex_coord[1], fill='black')时,你的意思一定是self.c.create_line(vertex0_coord[0], vertex0_coord[1], vertex1_coord[0], vertex1_coord[1], fill='black'),因为你需要4个坐标代表一条线。这就是你在实际代码中所做的吗? -
你说的完全正确,是的。实际代码类似于:
self.c.create_line(vertex_coord_x[V],vertex_coord_y[V],vertex_coord_x[V-1],vertex_coord_y[V-1], fill='black', tags=(V, 'network', 'connection')。当然,目前这会在整个网络中以 V 升序绘制一系列非常混乱的线。
标签: python algorithm loops python-2.7 tkinter