1+ import java.util.Date ;
2+ import java.text.SimpleDateFormat ;
3+
4+ Tree tree;
5+
6+ void setup () {
7+ size (1280 , 720 , P2D );
8+ background (#333333 );
9+ tree = new Tree ();
10+ }
11+
12+ void draw () {
13+ tree. draw();
14+ }
15+
16+ void mousePressed () {
17+ background (#333333 );
18+ tree = new Tree ();
19+ }
20+
21+ void keyPressed () {
22+ String timeStamp = new SimpleDateFormat (" yyyyMMdd" ). format(new Date ());
23+ String name = " tree_" + timeStamp + " _" + frameCount ;
24+ save (name + " .png" );
25+ }
26+
27+ class Tree {
28+ ArrayList<Branch > branches = new ArrayList<Branch > ();
29+
30+ Tree () {
31+ // float quarterScreen = width / 4;
32+ // float randomHorizontalPosition = random(quarterScreen, width - quarterScreen);
33+ // PVector position = new PVector(randomHorizontalPosition, height);
34+
35+ PVector position = new PVector (width / 2 , height );
36+ double angle = - 1.57 ;
37+ float thickness = 25 ;
38+
39+ createBranch(position, angle, thickness);
40+ }
41+
42+ void draw () {
43+ Branch branch;
44+ boolean divaricate;
45+
46+ for (int i = 0 ; i < branches. size(); i++ ) {
47+ branch = branches. get(i);
48+ branch. draw();
49+ divaricate = Math . round(Math . random() * (branch. thickness / 2 )) == 1 ;
50+
51+ if (divaricate) {
52+ double angle = branch. angle + ((Math . random() - .5 ) * 2 );
53+ float thickness = random (1 , branch. thickness);
54+ createBranch(branch. position, angle, thickness);
55+ }
56+
57+ if (branch. thickness < 7 ) {
58+ Leaf leaf = new Leaf (branch. position. get());
59+ leaf. draw();
60+ }
61+
62+ if (branch. ended) {
63+ branches. remove(branch);
64+ }
65+ }
66+ }
67+
68+ void createBranch (PVector position , double angle , float thickness ) {
69+ Branch branch = new Branch (position, angle, thickness);
70+ branches. add(branch);
71+ }
72+ }
73+
74+ class Branch {
75+ boolean ended = false ;
76+
77+ PVector position;
78+ double angle;
79+ float thickness;
80+
81+ Branch (PVector position , double angle , float thickness ) {
82+ this . position = position;
83+ this . angle = angle;
84+ this . thickness = thickness;
85+
86+ strokeCap (ROUND );
87+ }
88+
89+ void draw () {
90+ thickness -= .3 ;
91+
92+ if (thickness < 1 ) {
93+ ended = true ;
94+ }
95+
96+ strokeWeight (thickness);
97+
98+ double length = (Math . random() * 8 ) + 4 ;
99+ angle += Math . PI / 180 * ((Math . random() * 30 ) - 15 );
100+
101+ PVector newPosition = position. get();
102+ newPosition. x += length * Math . cos(angle);
103+ newPosition. y += length * Math . sin(angle);
104+
105+ noFill ();
106+ stroke (#F5F5F5 );
107+ line (position. x, position. y, newPosition. x, newPosition. y);
108+
109+ position = newPosition;
110+ }
111+ }
112+
113+ class Leaf {
114+ int [] colors = {#cc0000, #c00000, #cf0000, #d50000, #da0000, #da0000};
115+
116+ PVector position;
117+
118+ Leaf (PVector position ) {
119+ this . position = position;
120+ }
121+
122+ void draw () {
123+ int index = (int ) random (0 , colors. length);
124+ color leafColor = colors[index];
125+ float leafSize = (float ) ((Math . random() * 10 ) + 2 );
126+
127+ position. x = (float ) (- 30 + position. x + Math . random() * 60 );
128+ position. y = (float ) (- 30 + position. y + Math . random() * 60 );
129+
130+ fill (leafColor);
131+ noStroke ();
132+ ellipse (position. x, position. y, leafSize, leafSize);
133+ }
134+ }
0 commit comments