-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerativeTree.pde
135 lines (102 loc) · 2.92 KB
/
GenerativeTree.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.Date;
import java.text.SimpleDateFormat;
Tree tree;
void setup() {
size(1280, 720, P2D);
background(#333333);
tree = new Tree();
}
void draw() {
tree.draw();
}
void mousePressed() {
background(#333333);
tree = new Tree();
}
void keyPressed() {
String timeStamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
String name = "tree_" + timeStamp + "_" + frameCount;
save(name + ".png");
}
class Tree {
ArrayList<Branch> branches = new ArrayList<Branch>();
Tree() {
// float quarterScreen = width / 4;
// float randomHorizontalPosition = random(quarterScreen, width - quarterScreen);
// PVector position = new PVector(randomHorizontalPosition, height);
PVector position = new PVector(width / 2, height);
double angle = -1.57;
float thickness = 25;
createBranch(position, angle, thickness);
}
void draw() {
for (int i = 0; i < branches.size(); i++) {
updateBranch(branches.get(i));
}
}
void createBranch(PVector position, double angle, float thickness) {
Branch branch = new Branch(position, angle, thickness);
branches.add(branch);
}
void updateBranch(Branch branch) {
branch.draw();
boolean divaricate = Math.round(Math.random() * (branch.thickness / 2)) == 1;
if (divaricate) {
double angle = branch.angle + ((Math.random() - .5) * 2);
float thickness = random(1, branch.thickness);
createBranch(branch.position, angle, thickness);
}
if (branch.thickness < 7) {
Leaf leaf = new Leaf(branch.position.get());
leaf.draw();
}
if (branch.ended) {
branches.remove(branch);
}
}
}
class Branch {
boolean ended = false;
PVector position;
double angle;
float thickness;
Branch(PVector position, double angle, float thickness) {
this.position = position;
this.angle = angle;
this.thickness = thickness;
strokeCap(ROUND);
}
void draw() {
thickness -= .3;
if (thickness < 1) {
ended = true;
}
strokeWeight(thickness);
double length = (Math.random() * 8) + 4;
angle += PI / 180 * ((Math.random() * 30) -15);
PVector newPosition = position.get();
newPosition.x += length * Math.cos(angle);
newPosition.y += length * Math.sin(angle);
noFill();
stroke(#F5F5F5);
line(position.x, position.y, newPosition.x, newPosition.y);
position = newPosition;
}
}
class Leaf {
int[] colors = {#CC0000, #C00000, #CF0000, #D50000, #DA0000, #DA0000};
PVector position;
Leaf(PVector position) {
this.position = position;
}
void draw() {
int index = (int) random(0, colors.length);
color leafColor = colors[index];
float leafSize = (float) ((Math.random() * 10) + 2);
position.x = (float) (-30 + position.x + Math.random() * 60);
position.y = (float) (-30 + position.y + Math.random() * 60);
fill(leafColor);
noStroke();
ellipse(position.x, position.y, leafSize, leafSize);
}
}