背景:我有一个代码,它生成规则形状网络(在本例中为三角形)的笛卡尔坐标,然后在 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 (?)