26. Oktober 2011
Durch rekursive Funktionsaufrufe werden Tentakel konstruiert. Durch random Werte werden Radius und Distanz der einzelnen Abschnitte bestimmt und führen somit zu einmaligem Aussehen. Zusätzlich wird über einen random Wert entschieden, ob "Untertentakel" erzeugt werden. Die Baumstruktur entsteht durch immer kleiner werdenden Abschnitte.// Settings color FLOWER_FILL = color(180, 183, 245); color FLOWER_STROKE = color(53, 0, 180); // STACK OVERWRITE // adjust this value to whatever depth is actually necessary public final int STACK_DEPTH = 512; public float[][] matrixStack = new float[STACK_DEPTH][6]; public int matrixStackDepth; // this version will override the built-in version pushMatrix function public void pushMatrix() { if (matrixStackDepth == 512) { throw new RuntimeException("too many calls to pushMatrix()"); } this.g.getMatrix().get(matrixStack[matrixStackDepth]); matrixStackDepth++; } // this version will override the built-in version popMatrix function public void popMatrix() { if (matrixStackDepth == 0) { throw new RuntimeException("too many calls to popMatrix()" + "(or too few to pushMatrix)"); } matrixStackDepth--; PMatrix2D m = new PMatrix2D(); m.set(matrixStack[matrixStackDepth]); this.g.setMatrix(m); } // SKRIPT void setup() { size(600, 600); randomSeed(millis()+100); smooth(); strokeWeight(10); noLoop(); } void draw() { background(255); translate(width * .5, height - 100); struct(10); } // Mouseclick -> Redraw void mousePressed() { redraw(); } // Shortcuts void keyPressed() { switch(key) { // Save as Image case 's': saveFrame("TREE-####.PNG"); println("IMAGE SAVED!"); break; } } void struct(int tiefe) { if (tiefe <= 0) { return; } int x = 0; int y = -5 * tiefe; int rand =(int) (random(1, 4) + .5); line(0, 0, x, y); pushMatrix(); translate(x, y); scale(random(0.8, 1)); switch(rand) { case 1: wurzel(100, 0, -20, 0); break; case 2: wurzel(100, 0, -20, 0, -1); break; case 3: wurzel(100, 0, -20, 0); wurzel(100, 0, -20, 0, -1); break; case 4: line(0, 0, x, y); struct(tiefe - 1); break; } struct(tiefe - 1); popMatrix(); } // Wurzel Preset void wurzel(int tiefe, int x, int y, int rad){ wurzel(tiefe, x, y, rad, 1); } // Wurzel void wurzel(int tiefe, int x, int y, int rad, int turn) { if (tiefe <=0) { fill(FLOWER_FILL); stroke(FLOWER_STROKE); strokeWeight(2); ellipse(0,0,50,50); stroke(0); strokeWeight(10); return; } line(0, 0, x, y); float factor = tiefe / 10; if (random(1, 30) < factor) { wurzel(6, x, y, 0, turn * -1); } pushMatrix(); translate(x, y); rotate(radians(rad * turn)); scale(random(0.7, 0.9)); x += 5 * turn; y -= 2; rad += 3; wurzel(tiefe - 1, x, y, rad, turn); popMatrix(); }