背景:通常の形状 (この場合は三角形) のネットワークのデカルト座標を生成し、Tkinter キャンバス上の形状の頂点を小さな円としてプロットするコードがあります。このプロセスは自動化されており、ネットワークの高さと幅だけでキャンバス出力を取得できます。各頂点には、「Vertex」というタグと頂点の番号があります。
問題:形状の頂点を自動的に接続したい (点と点)。これを行う方法を調べましfind_closest
たfind_overlapping
が、ネットワークは互いに角度を成す頂点で構成されているため、多くの場合find_overlapping
、信頼できない (長方形のエンベロープに依存しているため)、およびfind_closest
接続が 1 つしか見つからないように見えます。頂点は必ずしも順番に接続されているとは限らないため、単純に頂点 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 (?)