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 › implementing delay in recursive function
Page Index Toggle Pages: 1
implementing delay in recursive function (Read 1129 times)
implementing delay in recursive function
Jan 11th, 2006, 4:47am
 
Im looking for an elegant way to implement delay in a recursive function. I've searched through other's code looking, but have yet to find anything. Id like the code to render, delay, then recursively call it's self. But currently the code is only rendering when the recursive function is over.

I suppose, I could make the function no longer recursive and do the same using whiles inside of the draw function. Reluctantly i could also use global variables to persist the data.

What am i missing?

<code>
void setup()
{
 size(900, 600);
 framerate(30);
 background(102);
 fill(255,255,255,100);
 noStroke();
 smooth();
 
 drawCircles(450, 300, 50, 15);
}

void draw()
{
 delay(300);
 drawCircles(450, 300, 50, 15);
}

void drawCircles(float x, float y, int radius, int level)
{
 if (level > 0)
 {
   float u;
   
   u = 2*PI*random(1);
   x = x + radius*cos(u);
   y = y + radius*sin(u);
   
   ellipse( x, y, radius, radius);

   // i'd like to dealy here, after rendering
   // delay(300);
   drawCircles( x, y, radius, level - 1);
 }
}
</code>
Re: implementing delay in recursive function
Reply #1 - Jan 11th, 2006, 7:42am
 
I was re-reading the post I made earlier and noticed the following statement could be taken the wrong way: "Im looking for an elegant way to implement delay in a recursive function. I've searched through other's code looking, but have yet to find anything." I didn't mean to imply I found no elegant code. Simply, that i couldn't find a solution in other's code.
Re: implementing delay in recursive function
Reply #2 - Jan 11th, 2006, 5:57pm
 
Things are only drawn to screen when void draw() finishes. So a delay won't help, since it'll still not draw anything until you return from drawCircles()

Sometmies the most elegant solution just doesn't work I'm afraid this is probably one of those times.
I think the best solution is a global variable. Or have the drawCircles function be a method of a class, and have the level variable stored within your circles class.
Page Index Toggle Pages: 1