Skip to content

Instantly share code, notes, and snippets.

@npryce
Created November 9, 2011 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save npryce/1352702 to your computer and use it in GitHub Desktop.
Save npryce/1352702 to your computer and use it in GitHub Desktop.
Random Trees for Codea
function setup()
    seed = 1
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(10, 120, 20)
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
@npryce
Copy link
Author

npryce commented Nov 9, 2011

Poke the screen to get a new random tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment