rabidmachine9
YaBB Newbies
Offline
Posts: 9
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); } }