-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStem.pde
301 lines (248 loc) · 8.48 KB
/
Stem.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
class Stem {
PVector position, topAnchor, bottomAnchor;
float w, h, stemWidth, stemScale;
int stemShape, stemVariation, stemLeavesNum, stemLeafType, stemLeafPattern, sumLLeaves, sumRLeaves;
color mainColor, leafHighlightColor;
ArrayList<PVector>stemCenterPoints;
ArrayList<PVector>stemPoints;
ArrayList<Leaf> leaves;
IntList leafAttachedLeft; // maps to stemCenterPoints
IntList leafAttachedRight; // maps to stemCenterPoints
Stem(PVector _position,
float _w,
float _h,
PVector _topAnchor,
PVector _bottomAnchor,
color _mainColor,
int _stemShape,
float _stemWidth,
int _stemVariation,
int _stemLeavesNum,
int _stemLeafType,
int _stemLeafPattern,
float _stemScale,
color _leafHighlightColor) {
w = _w;
h = _h;
position = _position;
topAnchor = _topAnchor;
bottomAnchor = _bottomAnchor;
mainColor = _mainColor;
stemShape = _stemShape;
stemWidth = _stemWidth;
stemVariation = _stemVariation;
stemLeavesNum = _stemLeavesNum;
stemLeafType = _stemLeafType;
stemScale = _stemScale;
stemLeafPattern = _stemLeafPattern;
leafHighlightColor = _leafHighlightColor;
sumLLeaves = 0;
sumRLeaves = 0;
stemWidth = stemWidth * stemScale;
stemPoints = new ArrayList<PVector>();
stemCenterPoints = new ArrayList<PVector>();
leafAttachedLeft = new IntList();
leafAttachedRight = new IntList();
setupStemPoints();
stemLeavesNum = min(_stemLeavesNum, stemCenterPoints.size() * 2);
leaves = new ArrayList<Leaf>();
setupLeaves();
}
void display() {
// Baseline setup
noStroke();
pushMatrix();
translate(position.x, position.y);
for (Leaf l : leaves) {
l.display();
}
// Choose the stem draw function based on
// the genetic stemShape
fill(mainColor);
switch(stemShape) {
case STEM_SHAPE_STRAIGHT:
drawStemStraight();
break;
case STEM_SHAPE_ANGLES:
drawStemAngled();
break;
case STEM_SHAPE_CURVES:
drawStemCurved();
break;
default:
println("Stem Problem");
}
// If we are debugging
debugDisplay();
// Pop back to the regular space
popMatrix();
}
void drawStemStraight() {
beginShape();
vertex(topAnchor.x - stemWidth/2, topAnchor.y);
vertex(topAnchor.x + stemWidth/2, topAnchor.y);
vertex(bottomAnchor.x + stemWidth/2, bottomAnchor.y);
vertex(bottomAnchor.x - stemWidth/2, bottomAnchor.y);
endShape();
}
void drawStemAngled() {
beginShape();
for (int i=0; i<stemPoints.size();i++) {
PVector p = stemPoints.get(i);
vertex(p.x, p.y);
}
endShape();
}
void drawStemCurved() {
beginShape();
vertex(bottomAnchor.x - stemWidth/2.0, bottomAnchor.y);
for (int i=0; i<stemPoints.size();i++) {
PVector p = stemPoints.get(i);
curveVertex(p.x, p.y);
}
vertex(bottomAnchor.x + stemWidth/2.0, bottomAnchor.y);
endShape();
}
void debugDisplay() {
if (debugStem) {
fill(255, 0, 0);
for (PVector v : stemCenterPoints) {
ellipse(v.x, v.y, 3, 3);
}
fill(0, 0, 0);
text(leaves.size(), 5, 0.8*h);
fill(mainColor);
}
}
// Set up stem points for stems that are not straight.
// The stemVariation is not expressed for flowers with
// the trait of STEM_SHAPE_STRAIGHT
void setupStemPoints() {
float numer = (float)stemVariation + 1.0;
// LERP along the line
float lerpPercent = (100/numer) / 100;
// These represent the midpoints -- and leaf attach points, for the stem
// ArrayList<PVector>tempStemPoints = new ArrayList<PVector>();
for (int i = 1; i <= stemVariation; i++) {
float r = random(-0.05, 0.05);
stemCenterPoints.add( PVector.lerp(bottomAnchor, topAnchor, lerpPercent * i + r));
// Set up the leaf attachment ArrayLists with 0 as values indicating no leaf attached
leafAttachedLeft.append(0);
leafAttachedRight.append(0);
}
// Take the temp points and create a round-trip arraylist
// that will be used to create the stem.
PVector bt = bottomAnchor.get();
float baseFactor = random(0, stemWidth);
bt.sub(stemWidth/2.0 - baseFactor, 0, 0);
stemPoints.add(bt.get());
// This list stores the random offsets that are used at the
// variation points. It later is reversed and applied to the
// points descending the other side of the stem, so that the
// width of the stem remains constant in its variance.
FloatList randomOffsets = new FloatList();
for (PVector p : stemCenterPoints) {
float r = random(-stemWidth/2.0, stemWidth/2.0); // Change for random variation
randomOffsets.append(r);
PVector t = new PVector(p.x - stemWidth/2 + r, p.y);
stemPoints.add(t);
}
PVector tt = topAnchor.get();
tt.sub(stemWidth/2.0, 0, 0);
stemPoints.add(tt.get());
tt.add(stemWidth, 0, 0);
stemPoints.add(tt.get());
ArrayList<PVector> reversedCenterPoints = new ArrayList<PVector>(stemCenterPoints);
Collections.reverse(reversedCenterPoints);
FloatList reversedRandom = new FloatList();
for (int i=randomOffsets.size() - 1; i >=0; i--) {
reversedRandom.append(randomOffsets.get(i));
}
for (int i=0; i < reversedCenterPoints.size(); i++) {
PVector t = new PVector(reversedCenterPoints.get(i).x + stemWidth/2 + reversedRandom.get(i), reversedCenterPoints.get(i).y);
stemPoints.add(t);
}
bt.add(stemWidth + baseFactor, 0, 0);
stemPoints.add(bt.get());
ArrayList<PVector> newStemCenterPoints = new ArrayList<PVector>();
// Shift the center points so that leaves appear in the proper spot.
// This prevents them from poking out on the wrong side of the stem
// when they are rotated.
for (int i = 0; i < stemCenterPoints.size(); i++) {
PVector pt = stemCenterPoints.get(i);
Float offset = randomOffsets.get(i);
pt.x = pt.x + (0.5 * offset);
newStemCenterPoints.add(pt);
}
stemCenterPoints = newStemCenterPoints;
}
void setupLeaves() {
// Not assigned any leaves
if (stemLeavesNum == 0) return;
// Loop for creating leaves
for (int i = 0; i < stemLeavesNum; i++) {
float r = random(0, 100);
boolean left = false;
if (r > 50) {
left = true;
}
if (left == true) {
if (sumLLeaves > sumRLeaves + 1) {
left = false;
}
}
if (left == false) {
if (sumRLeaves > sumLLeaves + 1) {
left = true;
}
}
int idx = (int)random(0, stemCenterPoints.size());
PVector pt2;
boolean leftBlocked = false;
boolean rightBlocked = false;
float stemAnchorPerc = 0.1;
if (stemCenterPoints.size() == 1 || idx == 0) {
// There's only one center point, so lerp from it towards the bottom
// println("pt2 assigned because either idx is 0 or there's 1 center point");
pt2 = PVector.lerp(stemCenterPoints.get(0), bottomAnchor, stemAnchorPerc);
}
else {
// This is not at the first centerpoint; lerp towards the next point down
// Potential problem here because the distance between center points is variable,
// Meaning that the leaf stem will be variable in width
pt2 = PVector.lerp(stemCenterPoints.get(idx), stemCenterPoints.get(idx - 1), stemAnchorPerc);
}
// Check if the sides are blocked at this node
if (left == true) {
if (leafAttachedLeft.get(idx) > 0) {
left = false;
leftBlocked = true;
}
}
if (left == false) {
if (leafAttachedRight.get(idx) > 0) {
if (leafAttachedLeft.get(idx) == 0) {
left = true;
}
rightBlocked = true;
}
}
// picked a bad node... TODO figure out how to try another
if (leftBlocked == true && rightBlocked == true) {
continue;
}
if (left && leftBlocked == false) {
Leaf l = new Leaf(stemCenterPoints.get(idx), pt2, true, stemLeafType, stemLeafPattern, stemScale, leafHighlightColor, mainColor);
leaves.add(l);
leafAttachedLeft.set(idx, 1);
sumLLeaves += 1;
}
else if (left == false && rightBlocked == false) {
Leaf l = new Leaf(pt2, stemCenterPoints.get(idx), false, stemLeafType, stemLeafPattern, stemScale, leafHighlightColor, mainColor);
leaves.add(l);
leafAttachedRight.set(idx, 1);
sumRLeaves += 1;
}
}
}
}