We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
mousepressed (Read 220 times)
mousepressed
Nov 11th, 2008, 2:16pm
 
hello
I am trying to change the following script so that the tree is re-created everytime the mouse is pressed although im not having much luck. can anyone explain how i can add a mousePressed so that the tree gets randomly re-generated with every mouseclick?

Tree branches;

void setup() {
 size(500,500);
 smooth();
 branches = new Tree (random(10),random(3,5));

}

void draw() {
 background(0);
 stroke(255);

 branches.draw();
}

class Tree
{
int frame =0;  
float theta;
float branch;
float mainbranches;
float a;
float x;
float y;

Tree (float x, float s)// set up two parameters x and s
{
a = x; // this controls the angle of the branches
theta = radians(a);
mainbranches = (height/s);
branch=y;
}

 void branch (float y){
      y=y*0.7;
   if (y > 3) {
     pushMatrix();    
     rotate(a);  
     line(0,0,0,-y);  
     translate(0,-y);
     branch(y);
     popMatrix();  

  pushMatrix();
   rotate(-a+0.6);
   line(0,0,0,-y+2);
  translate(0,-y+2);
   branch(y+0.4);
   popMatrix();
   
   }
 }

 void draw () {

  translate(width/2,height);

   line(0,0,0,-height/2);

   translate(0,-height/2);
    // Start the recursive branching!
   branch(mainbranches);
 

 }
}


Re: mousepressed
Reply #1 - Nov 11th, 2008, 2:24pm
 
Code:

//add to existing code:
void mousePressed() {
branches = new Tree (random(10), random(3, 5));
}


garbage collector should clean out the old tree.
Re: mousepressed
Reply #2 - Nov 11th, 2008, 2:31pm
 
Thankyou!!!!!!!!
Page Index Toggle Pages: 1