-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.pde
202 lines (178 loc) · 5.06 KB
/
Tree.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Coding Rainbow
// Daniel Shiffman
// http://patreon.com/codingrainbow
// Code for: https://youtu.be/kKT0v3qhIQY
class Tree {
ArrayList<Branch> branches = new ArrayList<Branch>();
ArrayList<Leaf> leaves = new ArrayList<Leaf>();
int numBranches;
int maxLeaves = 1000;
boolean isDead;
boolean maxReached;
int stablised = 0;
int lifespan = 150;
int killRate = 1;
Branch root;
float swayValue;
int swayDir;
float maxDistanceFromRoot = 1;
float r;
float g;
float bl;
color start;
color end;
color treeColour;
int hiddenBranches;
Tree(int rootStart, int treeWidth, int trunkLength) {
end = color(255, 255, 255);
start = color(200, 200, 200);
swayValue = random(-8,8);
if(swayValue > 0){
swayDir = 1;
swayValue = 0.002;
} else {
swayDir = -1;
swayValue = -0.002;
}
isDead = false;
for (int i = 0; i < maxLeaves; i++) {
leaves.add(new Leaf(rootStart,treeWidth,trunkLength));
}
root = new Branch(new PVector(width/2, height), new PVector(0, -1));
branches.add(root);
Branch current = new Branch(root);
while (!closeEnough(current)) {
Branch trunk = new Branch(current);
if(trunk.distanceFromRoot >maxDistanceFromRoot){
maxDistanceFromRoot = trunk.distanceFromRoot;
}
branches.add(trunk);
current = trunk;
}
hiddenBranches = 0;
}
boolean closeEnough(Branch b) {
for (Leaf l : leaves) {
float d = PVector.dist(b.pos, l.pos);
if (d < max_dist) {
return true;
}
}
return false;
}
void grow() {
numBranches = branches.size();
for (Leaf l : leaves) {
Branch closest = null;
PVector closestDir = null;
float record = -1;
for (Branch b : branches) {
PVector dir = PVector.sub(l.pos, b.pos);
float d = dir.mag();
if (d < min_dist) {
l.reached();
closest = null;
break;
} else if (d > max_dist) {
} else if (closest == null || d < record) {
closest = b;
closestDir = dir;
record = d;
}
}
if (closest != null) {
closestDir.normalize();
closest.dir.add(closestDir);
closest.count++;
}
}
/*for (int i = leaves.size()-1; i >= 0; i--) {
if (leaves.get(i).reached) {
leaves.remove(i);
}
}*/
for (int i = branches.size()-1; i >= 0; i--) {
Branch b = branches.get(i);
if (b.count > 0) {
b.dir.div(b.count);
b.dir.normalize();
Branch newB = new Branch(b);
branches.add(newB);
b.reset();
if(b.distanceFromRoot >maxDistanceFromRoot){
maxDistanceFromRoot = b.distanceFromRoot;
}
}
}
maxReached = reachedMaxBranches();
}
//if the number of branches is the same before and after growing the tree,
//a certain amount of times indicated by "stablised" then maximum branches has been reached
boolean reachedMaxBranches(){
boolean maxReached = false;
if(numBranches < branches.size()){
numBranches = branches.size();
}
if(numBranches == branches.size()){
stablised ++;
}
if(stablised > lifespan){
return true;
}
return maxReached;
}
void drawWithSway(Branch b){
if(swayValue < -8){
swayValue = -8;
swayDir = 1;
} else if (swayValue > 8){
swayValue = 8;
swayDir = -1;
}
swayValue += swayDir*0.000001;
float distCurr = b.distanceFromRoot-80000;
float distParent = b.parent.distanceFromRoot-80000;
float swayValueCurr = swayValue*exp(distCurr/maxDistanceFromRoot);
float swayValueParent = swayValue*exp(distParent/maxDistanceFromRoot);
float swayCurrDown = abs(swayValueCurr)/2;
float swayParentDown = abs(swayValueParent)/2;
line(b.pos.x+swayValueCurr, b.pos.y+swayCurrDown, b.pos.z, b.parent.pos.x+swayValueParent, b.parent.pos.y+swayParentDown, b.parent.pos.z);
}
void show() {
for (Leaf l : leaves) {
l.show();
}
println(hiddenBranches);
for (int i = 0; i < branches.size()-hiddenBranches; i++) {
Branch b = branches.get(i);
if (b.parent != null) {
float sw = map(i, 0, branches.size(), 4, 0);
strokeWeight(sw);
float gradient = map(b.distanceFromRoot, 0, maxDistanceFromRoot, 1, 0);
stroke(lerpColor(start, end, gradient));
line(b.pos.x, b.pos.y, b.pos.z, b.parent.pos.x, b.parent.pos.y,b.parent.pos.z);
//drawWithSway(b);
}
}
}
void die(){
//println("removed life");
if(branches.size()>700){
hiddenBranches += 1;
}
if(hiddenBranches > branches.size()){
hiddenBranches = branches.size();
}
//keep a small section of tree when silent
if(branches.size()-hiddenBranches < 700 && branches.size()>700){
hiddenBranches = branches.size()-700;
}
}
void addLife(){
//println("added life");
hiddenBranches -= 1;
if(hiddenBranches < 0){
hiddenBranches = 0;
}
}
}