Skip to content

Instantly share code, notes, and snippets.

@phred
Forked from npryce/trees.lua
Created November 13, 2011 00:56
Show Gist options
  • Save phred/1361387 to your computer and use it in GitHub Desktop.
Save phred/1361387 to your computer and use it in GitHub Desktop.
Random Trees for Codea
-- Use this function to perform your initial setup
function setup()
   seed = 1
    iparameter("depth", 1, 15)
    depth = 10
    parameter("length", 0, 300)
    length = 120
    parameter("angle", 1, 90)
    angle = 20
end
function draw()
    background(136, 207, 224, 255)
    noStroke()
    fill(61, 109, 31, 255)
    rect(0, 0, WIDTH, 128)
        
    lineCapMode(ROUND)
    ellipseMode(CENTER)
        
    translate(WIDTH/2, 64)
    
    math.randomseed(seed)
    drawTree(depth, length, angle)
end
function touched(t)
    seed = seed + 1
end
function drawTree(depth, length, angle)
    if depth > 0 then
        strokeWidth(length/5)
        stroke(133, 55, 29, 255)
        line(0, 0, 0, length)
        
        pushMatrix()
        translate(0, length)
        pushMatrix()
        rotate(perturb(angle))
        drawTree(depth-1, length*0.8, angle)
        popMatrix()
        pushMatrix()
        rotate(perturb(-angle))
        drawTree(depth-1, length*0.8, angle)
        popMatrix()
        popMatrix()
    else
        noStroke()
        stroke(24, 164, 41, 255)
        fill(24, 164, 41, 255)
        ellipse(0, 0, 32, 32)
    end
end
function perturb(v)
    return v * math.random(0.95, 1.05)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment