#!/usr/bin/python3
# -------------------------------------------------------------------
# ---- draw a graph of the (shortest path) compact cost data
# -------------------------------------------------------------------
import json
import numpy as np
from graphics import *
import coordinate_conversion as cc
cfile = 'compact_costs.json'
# -------------------------------------------------------------------
# ---- read JSON file
# -------------------------------------------------------------------
def input_json_file(infile):
with open(infile,'r') as ifile:
jdata = json.load(ifile)
return jdata
# -------------------------------------------------------------------
# ---- draw circles with node (vertex) numbers
#--------------------------------------------------------------------
def draw_circles(win,cords):
for idx,pt in enumerate(cords):
c = Circle(pt,10)
c.setFill('black')
c.draw(win)
# ---- draw text
t = Text(pt,str(idx))
##t.setFace('courier')
##t.setSize(12)
t.setTextColor('white')
t.draw(win)
def draw_connections(win,compact_costs,cords):
for idx,pt in enumerate(cords):
cons = compact_costs[idx][1] # connections
print(f'[{idx}] {cons}')
for con in cons:
# ---- draw arrow
p1 = cords[idx]
p2 = cords[con[0]]
l = Line(p1,p2)
mid = l.getCenter()
l1 = Line(p1,mid)
l1.setWidth(3)
l1.setFill('blue')
l1.setArrow('last')
l1.draw(win)
#l2 = Line(mid,p2)
#l2.setWidth(6)
#l2.setFill('black')
#l2.setArrow('none')
#l2.draw(win)
def draw_notes(win,text):
x = win.width/2
y = win.height - 50
t = Text(Point(x,y),text)
t.setFace('courier')
t.setSize(12)
t.draw(win)
# -------------------------------------------------------------------
# ---- get node (vertex) graphics points
# -------------------------------------------------------------------
def node_graphic_points(win,compact_costs,raidus):
siz = len(compact_costs) # number of nodes
ang = 0.0 # starting angle
d_ang = 360.0/siz # delta angle
cords = [None for _ in range(siz)] # node's graphics point
for idx in range(siz):
##print(f'angle = {ang} {d_ang}')
rad = np.deg2rad(ang)
x = raidus * np.cos(rad)
y = raidus * np.sin(rad)
wx,wy = cc.center_to_win_coords(x,y,win.width,win.height)
cords[idx] = Point(wx,wy) # window coordinates
# (graphics points)
# ---- next node
ang += d_ang
return cords
# -------------------------------------------------------------------
# ---- draw graph
# -------------------------------------------------------------------
def draw_graph(win,compact_costs,raidus):
cords = node_graphic_points(win,compact_costs,raidus)
# ---- draw connections
draw_connections(win,compact_costs,cords)
# ---- draw circles with text (overlay connections)
for idx,pt in enumerate(cords):
draw_circles(win,cords)
# ---- draw notes
note = 'Arrows Show the Direction of Outbound Connections\n' + \
'They Point at a Destination Node'
draw_notes(win,note)
return
# -------------------------------------------------------------------
# ---- display costs
# -------------------------------------------------------------------
def display_costs(compact_costs,title,tail=None):
if title != None:
print(title)
for idx,cons in enumerate(compact_costs):
print(f'[{idx:2}] {cons}')
if tail != None:
print(tail)
# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------
if __name__ == '__main__':
import user_interface as ui
compact_costs = input_json_file(cfile)
win = GraphWin("Connection Costs - Outbould Direction",801,801)
draw_graph(win,compact_costs,300)
ui.pause()
win.close()