Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define the graph tree as a table of nodes and edges
- local graph = {
- nodes = {
- { x = 10, y = 10 },
- { x = 30, y = 10 },
- { x = 20, y = 30 },
- { x = 10, y = 50 },
- { x = 30, y = 50 },
- { x = 20, y = 70 },
- { x = 5, y = 90 },
- { x = 35, y = 90 },
- },
- edges = {
- { 1, 2 },
- { 1, 3 },
- { 2, 3 },
- { 3, 4 },
- { 3, 5 },
- { 4, 6 },
- { 5, 6 },
- { 4, 7 },
- { 5, 8 },
- },
- }
- local letters = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"}
- -- Define a function to draw a node at a given position
- local function drawNode(x, y, name)
- paintutils.drawFilledBox(x - 2, y - 1, x + 2, y + 1, colors.green)
- paintutils.drawPixel(x - 2, y - 1, colors.white)
- paintutils.drawPixel(x + 2, y - 1, colors.white)
- paintutils.drawPixel(x - 2, y + 1, colors.white)
- paintutils.drawPixel(x + 2, y + 1, colors.white)
- term.setCursorPos(x - 1, y)
- term.write(name)
- end
- -- Define a function to draw an edge between two nodes
- local function drawEdge(x1, y1, x2, y2, name)
- paintutils.drawLine(x1, y1, x2, y2, colors.white)
- local x = math.floor((x1 + x2) / 2)
- local y = math.floor((y1 + y2) / 2)
- term.setCursorPos(x, y)
- term.write(name)
- end
- -- Draw the graph tree
- local monitor = peripheral.wrap("top")
- term.redirect(monitor)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextScale(0.5)
- monitor.clear()
- for i, edge in ipairs(graph.edges) do
- local node1 = graph.nodes[edge[1]]
- local node2 = graph.nodes[edge[2]]
- local edgeName = letters[2][i]
- drawEdge(node1.x, node1.y, node2.x, node2.y, edgeName)
- end
- for i, node in ipairs(graph.nodes) do
- local nodeName = letters[1][i]
- drawNode(node.x, node.y, nodeName)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement