Last active
August 29, 2015 14:21
-
-
Save micaiahparker/dcb93a6e7ec71d80fe73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function love.load() | |
startDepth = 1 | |
spaceDiv = 2 | |
depthMult = 30 | |
font = love.graphics.getFont() | |
end | |
function love.update(dt) | |
startMid = love.window.getWidth()/2 | |
startSpace = startMid/2 | |
tree = getTree() -- loads 'test.lua' | |
end | |
function love.draw() | |
printTree(tree, startMid, startDepth, startSpace) | |
end | |
function printTree(node, mid, depth, space) | |
if node then | |
if node.left then | |
printTree(node.left, mid-space, depth+1, space/spaceDiv) -- print the left | |
love.graphics.setColor(0,0,255) | |
love.graphics.line(mid, depth*depthMult, mid-space, (depth+1)*depthMult) -- draw a blue line to the left | |
end | |
if node.right then | |
printTree(node.right, mid+space, depth+1, space/spaceDiv) -- print the right | |
love.graphics.setColor(255,0,0) | |
love.graphics.line(mid, depth*depthMult, mid+space, (depth+1)*depthMult) -- draw a red line to the right | |
end | |
love.graphics.setColor(255,255,255) -- reset to white for each node | |
if node.key then | |
kw = font:getWidth(node.key)/2 -- for sexy not-offset printing | |
dw = font:getWidth(node.data)/2 | |
love.graphics.print(node.key, mid-kw, depth*depthMult) -- prints last so that its over the line behind it | |
love.graphics.print(node.data, mid-dw, depth*depthMult+10) | |
end | |
end | |
end | |
function getTree() | |
chunk = love.filesystem.load("test.lua") | |
if chunk then | |
return chunk() | |
else | |
return {} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment