-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractalTree.pde
71 lines (59 loc) · 1.93 KB
/
FractalTree.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* 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.
*/
class FractalTree{
float theta;
public FractalTree(PGraphics offscreen) {
//size(640, 360);
}
void setup_obj(){
//break;
}
void pause_obj(){
//break;
}
void draw_obj(PVector surfaceMouse, PGraphics offscreen) {
offscreen.background(0);
//frameRate(30);
offscreen.stroke(255);
// Let's pick an angle 0 to 90 degrees based on the mouse position
float a = (surfaceMouse.x / (float) offscreen.width) * 90f;
// Convert it to radians
theta = radians(a);
// Start the tree from the bottom of the screen
offscreen.translate(offscreen.width/2,offscreen.height * .8);
// Draw a line 120 pixels
offscreen.line(0,0,0,-120);
// Move to the end of that line
offscreen.translate(0,-120);
// 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) {
offscreen.pushMatrix(); // Save the current state of transformation (i.e. where are we now)
offscreen.rotate(theta); // Rotate by theta
offscreen.line(0, 0, 0, -h); // Draw the branch
offscreen.translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
offscreen.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!
offscreen.pushMatrix();
offscreen.rotate(-theta);
offscreen.line(0, 0, 0, -h);
offscreen.translate(0, -h);
branch(h);
offscreen.popMatrix();
}
}
}