Created
November 9, 2011 19:45
-
-
Save npryce/1352702 to your computer and use it in GitHub Desktop.
Random Trees for Codea
This file contains hidden or 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 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Poke the screen to get a new random tree.