Created
May 31, 2015 13:24
-
-
Save tinchodias/2f65fdbbbcf4ddb59ae1 to your computer and use it in GitHub Desktop.
Processing: arbolito capture
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
/** | |
* Recursive Tree | |
* by Daniel Shiffman. | |
* | |
* Renders a simple tree-like structure via recursion. | |
* The branching angle is calculated as a function of | |
* the horizontal mouse location. Move the mouse left | |
* and right to change the angle. | |
*/ | |
import processing.video.*; | |
float theta; | |
Capture video; | |
float angle; | |
void setup() { | |
size(640, 360); | |
angle = 0.0; | |
//setup capture | |
video = new Capture(this, 640, 360); | |
video.start(); | |
noStroke(); | |
smooth(); | |
//colorMode(HSB, 100) ; | |
} | |
void draw() { | |
// find brightest pixel // | |
int brightestX = 0; // X-coordinate of the brightest video pixel | |
int brightestY = 0; // Y-coordinate of the brightest video pixel | |
float brightestValue = 0; // Brightness of the brightest video pixel | |
if (video.available()) { | |
video.read(); | |
// Search for the brightest pixel: For each row of pixels in the video image and | |
// for each pixel in the yth row, compute each pixel's index in the video | |
video.loadPixels(); | |
int index = 0; | |
for (int y = 0; y < video.height; y++) { | |
for (int x = 0; x < video.width; x++) { | |
// Get the color stored in the pixel | |
int pixelValue = video.pixels[index]; | |
// Determine the brightness of the pixel | |
float pixelBrightness = brightness(pixelValue); | |
// If that value is brighter than any previous, then store the | |
// brightness of that pixel, as well as its (x,y) location | |
if (pixelBrightness > brightestValue) { | |
brightestValue = pixelBrightness; | |
brightestY = y; | |
brightestX = x; | |
} | |
index++; | |
} | |
} | |
} | |
// update angle // | |
if (brightestX != 0) | |
angle = brightestX; | |
// draw tree // | |
background(0); | |
frameRate(30); | |
stroke(255); | |
// Let's pick an angle 0 to 90 degrees based on the mouse position | |
float a = (angle / (float) width) * 90f; | |
// Convert it to radians | |
theta = radians(a); | |
// Start the tree from the bottom of the screen | |
translate(width/2,height); | |
// Draw a line 120 pixels | |
line(0,0,0,-120); | |
// Move to the end of that line | |
translate(0,-120); | |
stroke(brightestY, 102, 0); | |
// Start the recursive branching! | |
branch(120); | |
} | |
void branch(float h) { | |
// Each branch will be 2/3rds the size of the previous one | |
h *= 0.66; | |
// All recursive functions must have an exit condition!!!! | |
// Here, ours is when the length of the branch is 2 pixels or less | |
if (h > 2) { | |
pushMatrix(); // Save the current state of transformation (i.e. where are we now) | |
rotate(theta); // Rotate by theta | |
line(0, 0, 0, -h); // Draw the branch | |
translate(0, -h); // Move to the end of the branch | |
branch(h); // Ok, now call myself to draw two new branches!! | |
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state | |
// Repeat the same thing, only branch off to the "left" this time! | |
pushMatrix(); | |
rotate(-theta); | |
line(0, 0, 0, -h); | |
translate(0, -h); | |
branch(h); | |
popMatrix(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment