Guest User

OMG

a guest
Feb 5th, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from random import random, randrange
  2.  
  3. # SVG strings
  4. # -----------
  5.  
  6. SVG_HEADER = '<svg xmlns="http://www.w3.org/2000/svg" width="%f" height="%f">'
  7. SVG_LINE = '<path d="M %f,%f L %f,%f" style="stroke:black; stroke-width:%f; fill:none;" />'
  8. SVG_CIRCLE = '<circle cx="%f" cy="%f" r="%f" style="fill:black; stroke:white; stroke-width:1px;" />'
  9. SVG_FOOTER = '</svg>'
  10.  
  11.  
  12. # Initial settings and data
  13. # -------------------------
  14.  
  15. graph_width = gw = 500
  16. graph_height = gh = 500
  17. line_width = 1
  18. circle_radius = cr = 10
  19. circle_diameter = cd = 2 * circle_radius
  20.  
  21. nodes_count = nc = 100
  22. edges_count = ec = 500
  23.  
  24. rndw = lambda: (graph_width - cd) * random() + cr
  25. rndh = lambda: (graph_height - cd) * random() + cr
  26.  
  27. random_nodes = [(rndw(),rndh()) for _ in range(nodes_count)]
  28. random_edges = [(randrange(nc),randrange(nc)) for _ in range(edges_count)]
  29.  
  30.  
  31. # The Program
  32. # -----------
  33.  
  34. print(SVG_HEADER % (graph_width, graph_height))
  35.  
  36. for e1,e2 in random_edges:
  37.     x1, y1 = random_nodes[e1]
  38.     x2, y2 = random_nodes[e2]
  39.     print(SVG_LINE % (x1, y1, x2, y2, line_width))
  40.  
  41. for x, y in random_nodes:
  42.     print(SVG_CIRCLE % (x, y, circle_radius))
  43.  
  44. print(SVG_FOOTER)
  45.  
  46. # End.
Advertisement
Add Comment
Please, Sign In to add comment