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 & HelpSyntax Questions › newb - pausing recursive function
Page Index Toggle Pages: 1
newb - pausing recursive function (Read 433 times)
newb - pausing recursive function
Aug 13th, 2008, 3:45pm
 
hey a quick newbie question...

how do i pause a recursive function? i would like to use a mouse click to advance to the next recursion.

applet:
http://www.introspector.be/index.php?/applets/recursive-fracture/

source:
http://www.introspector.be/processing/recursive_fracture/applet/recursive_fracture.pde

any other comments/tips welcome.
Re: newb - pausing recursive function
Reply #1 - Aug 16th, 2008, 11:49am
 
So just you know, on Firefox 3, the source frame have no scroll bars, so the code looks strange. I had to view source to see it. Since you use PHP, you should avoid framesets...

Back to your question, there are a couple of issues.
First, as in most recursive algorithms, the whole recursion is done first, and only at the end the drawing starts. So you can't really do "step by step".

A solution is to start with a level of 1 or 2, and increment it on each mouse click: thus the recursion goes deeper and deeper each time, showing more detail.
BUT, the algorithm is random, so each step can be very different from the previous one.
So you have to get the same random sequence each time. Either you generate the required number of random values in setup and store them in an array, or you just use randomSeed() function to be sure to generate always the same sequence (with a random initial seed).
Re: newb - pausing recursive function
Reply #2 - Aug 17th, 2008, 5:50pm
 
if you really want to make it discrete, you'll probably need to break up the recursion into a "to-do" list.  (instead of a deep recursion, you build a wide list)

you'll need a storage class to keep a "region to be subdivided", then begin by seeding the list with the outermost boundary.

then on each pass through draw, you process that list *ONCE* through its initial size.  so on first pass it contains 1 item, so you subdivide it, which then adds two more items to the list (and removes the first)

on the second pass through, you process those next two items, adding four new, removing original two.  etc

you can also limit to a certain amount of processing per frame, so for example the amount to process is min(200,list.size()).  like once you're 8 levels deep and have 512 "things to do", you'd only do 300 of them per frame, (adding 600, remove 300), then do the next 300 on the next frame.  etc  (your list will grow! with this method, but will eventually catch up once recursion stops)
Re: newb - pausing recursive function
Reply #3 - Aug 18th, 2008, 5:18pm
 
thanks...

i got it working with the randomSeed() function.
i'll try to make it more effecient when i find some time Wink
Page Index Toggle Pages: 1