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.
IndexProgramming Questions & HelpPrograms › Help with pushMatrix and random
Page Index Toggle Pages: 1
Help with pushMatrix and random (Read 554 times)
Help with pushMatrix and random
Apr 30th, 2007, 3:54am
 
Hi all,

Beneath is a program which draws eight legs and then applies recursion to these legs to create branches.

If you take the noLoop() out of setup you will see that the original eight legs animate smoothly but the sub-branches appear only for a moment. I know this is because they are called using random() but I can't get my head around a way to keep them constant. Most things I try result in a popMatrix error.

Any ideas?

Thanks a lot

Nathan

-----------------------------------

Leech myLeech;
int maxLevel = 200;

void setup() {
 size(400,400);
 smooth();
 noStroke();
 myLeech = new Leech(160, 260, 8, 0.97, 0.01);
 background(0);
 noLoop();
}

void draw() {  
 //translate(200,300);
 background(0);
 myLeech.drawCluster();
}


class Leech
{
 int xpos;
 int ypos;
 int legcount;
 float leg_sc;
 float leg_an;
 float[] scaleinc;
 float[] angleinc;

 float[] leg_s;
 float[] leg_a;

 Leech(int xp, int yp, int legno, float legscale, float legangle){

   xpos = xp;
   ypos = yp;
   legcount = legno;
   scaleinc = new float[legno];
   angleinc = new float[legno];
   leg_s = new float[legno];
   leg_a = new float[legno];

   for (int i = 0; i < legcount; i++) {
     leg_s[i] = random(0.970,0.985);
     leg_a[i] = random(-0.1,0.1);
     scaleinc[i] = 0.0001;
     angleinc[i] = 0.001;
   }
 }

 void drawCluster(){ // this function joins the legs together into a cluster
   translate(xpos,ypos); // move each leg to the right place
   for (int i = 0; i < legcount; i++) {
     pushMatrix();
     rotate(i/TWO_PI); // space the legs evenly around a full circle
     drawleg(i,0,0,150); // legnumber,level,branchdepth,maxlevel
     popMatrix(); // reset geometry after drawing the leg
   }
 }


 void drawleg(int thisleg, int level, int branchdepth, int maxlevel){
   fill(level,level,level);
   scale(leg_s[thisleg]);
   rotate(leg_a[thisleg]);
   translate(0,-5);

   for(int i =0;i<legcount;i++){
     leg_a[i]=leg_a[i]-angleinc[i];
     leg_s[i]=leg_s[i]+scaleinc[i];
     if (leg_s[i] >= 0.985 || leg_s[i] <= 0.970)scaleinc[i]=scaleinc[i]*-1;
     if (leg_a[i] >= 0.1 || leg_a[i] <= -0.1)angleinc[i]=angleinc[i]*-1;
   }
   ellipse(0,0,30,30);

   if (random(1.0)<0.020){ // start a branch with
     pushMatrix();
     scale(leg_s[thisleg]);
     if (branchdepth%2 == 0) { // even numbered branch
       rotate(-0.8); // rotate to the right
       //translate(-3,0);
     }
     else {
       rotate(0.8);
       //translate(3,0);
     }

     branchdepth++;
     drawleg(thisleg,level,branchdepth,maxlevel);
     popMatrix();
   }
   level++; // increment the level
   if (level<maxlevel){ // if we're not yet at maxlevel
     drawleg(thisleg,level,branchdepth,maxlevel);  // call the function again
   }
 }
}

Re: Help with pushMatrix and random
Reply #1 - Apr 30th, 2007, 4:14am
 
What I usually do in cases like this, where I need something "random" yet repeatable, is give the class ("Leech" in your case) a property called "seed" and assign it some integer value.  Then just before you start using random values, call randomSeed(seed) to reset the generator.  I think (though not tested) in your case the right place to reset the generator would be as the very first line of drawCluster(), then your structure ought to remain the same through subsequent frames.
Re: Help with pushMatrix and random
Reply #2 - Apr 30th, 2007, 4:33am
 
Thanks a lot. Almost got that working well.
Does the int I choose for seed make any difference?
Re: Help with pushMatrix and random
Reply #3 - Apr 30th, 2007, 4:43am
 
It should make "all" the difference.

Each unique seed will set up a whole new sequence of random numbers, thus a whole new set of potential outcomes.  You may even come to the point where you'd consider naming a particular result "seed x", because you can always reproduce that specific result when you seed the RNG with x.

Sorry if that was fairly geek-technical, hope it made sense. Cheesy
Re: Help with pushMatrix and random
Reply #4 - Apr 30th, 2007, 4:51am
 
Your making plenty of sense.
And, thanks again for your quick reply and excellent advice.
Appreciate it.
Page Index Toggle Pages: 1