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
wait() function? (Read 4195 times)
wait() function?
May 30th, 2009, 2:51pm
 
hello everybody!
do you know if there is a function that would freeze the program from running but will display the screen?
I have already used delay() but what it probably does is freezing the screen and not the program from runing.
I am actualy trying to visualize a sorting algorithm and I want the changes to be visible after each iteration....

thanks in advance!
Re: wait() function?
Reply #1 - May 30th, 2009, 4:07pm
 
So, do you want to make sure everything stops?

The easiest way is to set frameRate() to something really slow, like 1 or 2 frames per second.

Another way is to just set up a while() loop that watches the time in millis().  Then, let the while() loop finish once millis() == whatever number.
Re: wait() function?
Reply #2 - May 31st, 2009, 12:49am
 
See Re: pause within draw() , but you makes it conditional instead of depending on user input.
Well, perhaps you need to ask the user to do something (press a key, click...) to restart the cycle, because you might have trouble to make it restart after a while...
Re: wait() function?
Reply #3 - May 31st, 2009, 5:10am
 
Thanks for all the replies guys!
I tried what you suggest and I  don't think it's exactly what I want...
this is  the code...
it will look a bit fancier in its final form but the idea is that you first create some rectangles(in this case 50) with random width and tou display them on the screen and then the program starts to sort them by width length and I would like the picture and the code to freeze in every sorting step so everybody can visualize the procedure...

int numRects = 50;
// Declare the array
RectElem[] rectArray ;

void setup(){
background(242, 204, 47);
size(500,500);
noLoop();

rectArray = new RectElem[numRects];   //create the array
for(int index = 0;index < numRects;index++){
  rectArray[index] = new RectElem();   //initialize the array
 }
}


void draw(){
 
  int  count = 0;

for(int index = 0;index < numRects;index++){//draw some rectangles
 
  fill(100,100,100);
  //if ( rectArray[index] != null)
 rectArray[index].rectWidth = random(500);
 //if ( rectArray[index] != null)
 rectArray[index].yPos = count;
  //if ( rectArray[index] != null)
 rectArray[index].create();
 count += 10;
 
}


shortArrayBubble(rectArray, numRects);


}

void shortArrayBubble(RectElem array[], int elems){//sort the rectangles by size
 float temp;
 boolean swap = true;

   while(swap)
   {  
        swap = false;
       for (int count = 0; count < (elems - 1); count++){
         
          if (array[count].rectWidth > array[count + 1].rectWidth){
            temp = array[count].rectWidth;
            array[count].kill();
            array[count].rectWidth = array[count + 1].rectWidth;
            array[count].create();
            array[count +1].kill();
            array[count + 1].rectWidth = temp;
            array[count + 1].create();
           
            swap = true;
           
          }
      }
   }
}


class RectElem{  //simple class for creating and killing rectangles
  //int xPos;
  int yPos;
 // float rectHeight;
  float rectWidth;
 
  void create(){
    noStroke();
    fill(100,100,100);
    rect(0,yPos,rectWidth,10);
  }
 
  void kill(){
    noStroke();
    fill(242, 204, 47);
    rect(0,yPos,rectWidth,10);
  }
 
}
Re: wait() function?
Reply #4 - May 31st, 2009, 1:50pm
 
Looks like you are sorting them all at once, inside shortArrayBubble().  But the way Processing works, everything inside draw() is completed first, and then at the end of draw(), a new "frame" is drawn to the screen.

So, you need to rewrite shortArrayBubble() to something like incrementalSort(), that would just choose one rect (for example), and put it in the right place, then move to the next one, then the next one, etc.  So instead of calling shortArrayBubble() once, and then you're done, call incrementalSort() once per loop through draw().

Once that's working, use frameRate() to set a speed that you like, say 2 fps.
Re: wait() function?
Reply #5 - May 31st, 2009, 2:03pm
 
So, if I get it right, I would have to sort just one in every time the draw() function is called and and put a low frame rate?

thanks a lot
I will try that
Re: wait() function?
Reply #6 - May 31st, 2009, 4:15pm
 
Yes, that's a much more succinct way of putting it.  Good luck!
Page Index Toggle Pages: 1