Advertisement
999ms

GraphBuilder

Apr 25th, 2020
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. from PIL import Image, ImageFilter, ImageDraw, ImageFont
  2. from math import *
  3.  
  4. def gen_graph(g, w, h):
  5.     n = len(g)
  6.     white = (255, 255, 255)
  7.     black = (0, 0, 0)
  8.     green = (0, 255, 0)
  9.     img = Image.new('RGB', (w, h), white)
  10.     arr = img.load()
  11.    
  12.     circle_size = 40
  13.     circle = (0, 0, 40, 40)
  14.    
  15.     pts = []
  16.    
  17.     pi = acos(-1)
  18.    
  19.     for i in range(n):
  20.         x = cos(2 * pi * i / n)
  21.         y = sin(2 * pi * i / n)
  22.         lenx = (w - circle_size) / 2
  23.         x *= lenx
  24.         leny = (h - circle_size) / 2
  25.         y *= leny
  26.         x += w / 2
  27.         y += h / 2
  28.         pts.append((int(x),int(y)))
  29.    
  30.    
  31.    
  32.     draw = ImageDraw.Draw(img)
  33.     print('ok')
  34.    
  35.     for v in range(n):
  36.         for u in g[v]:
  37.             if type(u) == type(1):
  38.                 draw.line([pts[v], pts[u]], fill=black, width=3)
  39.                 x, y = pts[u]
  40.                 dx = pts[v][0] - pts[u][0]
  41.                 dy = pts[v][1] - pts[u][1]
  42.                 l = (dx * dx + dy * dy) ** 0.5
  43.                 if l == 0:
  44.                     continue
  45.                 dx /= l
  46.                 dy /= l
  47.                 K = circle_size / 2
  48.                 x += dx * K
  49.                 y += dy * K
  50.                 sz = 5
  51.                 draw.rectangle([x-sz, y-sz, x+sz, y+sz], fill=green, outline=black)
  52.             elif type(u) == type((1,1)):
  53.                 w = u[1]
  54.                 u = u[0]
  55.                 draw.line([pts[v], pts[u]], fill=black, width=3)
  56.                 coord = ((pts[v][0] + pts[u][0]) // 2 + 15, (pts[v][1] + pts[u][1]) // 2)
  57.                 draw.text(coord, str(w), font=ImageFont.load_default(), fill=black)
  58.                 x, y = pts[u]
  59.                 dx = pts[v][0] - pts[u][0]
  60.                 dy = pts[v][1] - pts[u][1]
  61.                 l = (dx * dx + dy * dy) ** 0.5
  62.                 if l == 0:
  63.                     continue
  64.                 dx /= l
  65.                 dy /= l
  66.                 K = circle_size / 2
  67.                 x += dx * K
  68.                 y += dy * K
  69.                 sz = 5
  70.                 draw.rectangle([x-sz, y-sz, x+sz, y+sz], fill=green, outline=black)
  71.             else:
  72.                 continue
  73.     index = 0
  74.     for (x, y) in pts:
  75.         index += 1
  76.         x1, y1, x2, y2 = circle
  77.         draw.ellipse([x + x1 - circle_size // 2,
  78.                       y + y1 - circle_size // 2,
  79.                       x + x2 - circle_size // 2,
  80.                       y + y2 - circle_size // 2
  81.                      ], fill=white, outline=black)
  82.         draw.text((x, y), str(index), font=ImageFont.load_default(), fill=black)
  83.     img.show()
  84.    
  85.  
  86. def parse_graph(n, s):
  87.     g = [[] for _ in range(n)]
  88.     s = s.split('\n')
  89.     for edge in s:
  90.         if len(edge) == 0:
  91.             continue
  92.         edge = list(map(int, edge.split()))
  93.         f = edge[0] - 1
  94.         t = edge[1] - 1
  95.         if len(edge) == 3:
  96.             w = edge[2]
  97.             g[f].append((t, w))
  98.         else:
  99.             g[f].append(t)
  100.     return g
  101.  
  102. s = """1 2 10
  103. 2 3 5
  104. 1 3 100
  105. 3 5 7
  106. 5 4 10
  107. 4 3 -18
  108. 6 1 -1"""
  109. graph = parse_graph(6, s)
  110. gen_graph(graph, 500, 500)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement