Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Rekursive Funktionen – Drawing a plant

9. November 2010

Konzept Die Planze habe ich um richtige Blüten erweitert und die Darstellung ein wenig angepasst (bunter, weniger Elemente, etc...)
/* 
* PLANT AS FRACTIONAL FUNCTION
* 
* This programs draws a plant with blossoms and branches randomly and redraw it
* when the mouse is presses
*
* @name PLANT AS FRACTIONAL FUNCTION
* @author Sebastian Pape <sebastian.pape@zhdk.ch>
* @version 0.9
*/

/* 
* SETUP
*/
void setup() {
  size(600,600);      // def. fenstergroesse

  randomSeed(millis());  // seed random

  smooth();           // aktiviere antialiasing
  strokeWeight(1);    // linienbreite
  stroke(0,0,0,150);

  noLoop();
}

/* 
* DRAW
*/
void draw() {
  background(255);

  pushMatrix();
    translate(width *.5,height - 20);
    scale(1.5, 1.5);
    drawPlant(3);
  popMatrix();
}

/* 
* mousePressed
*/
void mousePressed()
{
   redraw();
}

/* 
* DRAW PLANT
*/
void drawPlant(int depth) {
   if(depth <=0) {
     drawBlossom();
     return;
   }

  drawBranch(depth);
}

/* 
* DRAW BLOSSOM
*/
void drawBlossom() {
   pushStyle();
   pushMatrix();
    
     stroke(255);
     fill(getRandomColor(),getRandomColor(),getRandomColor(),190);
     for (int i = 0; i <= 5; i++) {
       if(i == 5) {
         // draw blossom center 
         drawBlossomCircle(0, 0);
       } else {
         float degree = radians(i * 72);
         drawBlossomCircle(round(60*cos(degree)), round(60*sin(degree)));
       }
     }
   popMatrix();
   popStyle();
}

void drawBlossomCircle(int _x, int _y) {
  int _width;
  int _height;
  
    if(_x == 0 && _y == 0) {
      _width = 60;
      _height = 60;
    } else {
      _width = 100;
      _height = 100;
    }
    ellipse(_x,_y,_width,_height);
}

/* 
* GET RANDOM COLOR
*/
int getRandomColor() {
  int _return = (int)random(0,255);
  return _return;
}

/* 
* DRAW BRANCH
*/
void drawBranch(int depth) {
  int x;
  int y;
  int count = (int)random(1,5);
  for(int i = 0; i < count;i++)
  {
    x = (int)random(-100,100);
    y = -(int)random(10,150);

    line(0,0,x,y);

    pushMatrix();
      translate(x,y);
      scale(random(.3,.95));
      drawPlant(depth-1);
     popMatrix();
  }
}