Advertisement
glitchdetector

Graph Nodes

Oct 16th, 2023 (edited)
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. -- Define the graph tree as a table of nodes and edges
  2. local graph = {
  3.     nodes = {
  4.         { x = 10, y = 10 },
  5.         { x = 30, y = 10 },
  6.         { x = 20, y = 30 },
  7.         { x = 10, y = 50 },
  8.         { x = 30, y = 50 },
  9.         { x = 20, y = 70 },
  10.         { x = 5, y = 90 },
  11.         { x = 35, y = 90 },
  12.     },
  13.     edges = {
  14.         { 1, 2 },
  15.         { 1, 3 },
  16.         { 2, 3 },
  17.         { 3, 4 },
  18.         { 3, 5 },
  19.         { 4, 6 },
  20.         { 5, 6 },
  21.         { 4, 7 },
  22.         { 5, 8 },
  23.     },
  24. }
  25.  
  26. local letters = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"}
  27.  
  28. -- Define a function to draw a node at a given position
  29. local function drawNode(x, y, name)
  30.     paintutils.drawFilledBox(x - 2, y - 1, x + 2, y + 1, colors.green)
  31.     paintutils.drawPixel(x - 2, y - 1, colors.white)
  32.     paintutils.drawPixel(x + 2, y - 1, colors.white)
  33.     paintutils.drawPixel(x - 2, y + 1, colors.white)
  34.     paintutils.drawPixel(x + 2, y + 1, colors.white)
  35.     term.setCursorPos(x - 1, y)
  36.     term.write(name)
  37. end
  38.  
  39. -- Define a function to draw an edge between two nodes
  40. local function drawEdge(x1, y1, x2, y2, name)
  41.     paintutils.drawLine(x1, y1, x2, y2, colors.white)
  42.     local x = math.floor((x1 + x2) / 2)
  43.     local y = math.floor((y1 + y2) / 2)
  44.     term.setCursorPos(x, y)
  45.     term.write(name)
  46. end
  47.  
  48. -- Draw the graph tree
  49. local monitor = peripheral.wrap("top")
  50. term.redirect(monitor)
  51. monitor.setBackgroundColor(colors.black)
  52. monitor.setTextScale(0.5)
  53. monitor.clear()
  54. for i, edge in ipairs(graph.edges) do
  55.     local node1 = graph.nodes[edge[1]]
  56.     local node2 = graph.nodes[edge[2]]
  57.     local edgeName = letters[2][i]
  58.     drawEdge(node1.x, node1.y, node2.x, node2.y, edgeName)
  59. end
  60. for i, node in ipairs(graph.nodes) do
  61.     local nodeName = letters[1][i]
  62.     drawNode(node.x, node.y, nodeName)
  63. end
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement