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 › Reverse a for loop
Page Index Toggle Pages: 1
Reverse a for loop ? (Read 1287 times)
Reverse a for loop ?
Dec 22nd, 2009, 2:11pm
 
Hi , i got this simple program , it makes some rectangles grow and then when they is as big as the screen the rectangles go back to 10 pixels width/height.


...

I want to know how to make the rectangles shrink, instead of all of a sudden to go to 10 pixels width / height ?


I just cant figure it out ,any ideas ?

Here's the code :


Code:
int limite ;


void setup (){
size (400,400);
//smooth ();
frameRate (25);
background (#ff0044);
}


void draw (){
background ( #ff0044 );



for ( int i = 0; i<limite ; i += 10){

fill ( #ff0044,30 );
noStroke ();
rect ( width/2,height/2,width,height);

noFill ();
stroke ( 255 );
strokeWeight ( 3 );
rectMode (CENTER );
rect ( width/2,height/2, i,i );
}

limite += 10;

if ( limite > width ){
limite = 10 ;
}
saveFrame();

}

Re: Reverse a for loop ?
Reply #1 - Dec 22nd, 2009, 2:23pm
 
I am doing it in two steps. fist you should optimize your program. cause at the moment you draw different sized rects in a for loop just to overdraw them again and leave the last one shown. you can just remove the whole for loop and just draw one single rect and increase the size of it.

so the code would become this :


Code:
int limite ;
void setup (){
 size (400,400);
 frameRate(25);
 background (#ff0044);
}

void draw (){
 fill(#ff0044,30);
 rectMode (CORNER);
 noStroke();
 rect(0,0,width,height);


 noFill ();
 stroke ( 255 );
 strokeWeight ( 3 );
 rectMode (CENTER );
 rect ( width/2,height/2,limite,limite );

 limite += 10;

 if ( limite > width ){
 limite = 10 ;
 }
}


Re: Reverse a for loop ?
Reply #2 - Dec 22nd, 2009, 2:29pm
 
so next step is to make it grow and shrink. this is the same as moving a pixel from one side to another for example. Do do that, you normaly define a direction variable. in this case this is "s" now as long as the size is not as big as the width, we add the direction variable. As soon as it is larger than width, we multiply it by -1 so 10 becomes -10 and instead of adding 10 we substract 10 and shrink it again. To make it go back and forth, i added  
"|| limite < 0 "  where "||" could be read at "OR" so it also mutliply by -10 when its smaler than 0 and starts to grow again.

hope that helps

Code:
int limite ;
int s = 10;

void setup (){
 size (400,400);
 frameRate(25);
 background (#ff0044);
}

void draw (){
 fill(#ff0044,30);
 rectMode (CORNER);
 noStroke();
 rect(0,0,width,height);


 noFill ();
 stroke ( 255 );
 strokeWeight ( 3 );
 rectMode (CENTER );
 rect ( width/2,height/2,limite,limite );

 limite += s;

 if ( limite > width || limite < 0 ){
  s*=-1; ;
 }
}

Re: Reverse a for loop ?
Reply #3 - Dec 22nd, 2009, 2:33pm
 
Code:
int limite;
int step = 10;


void setup (){
 size (400,400);
 //smooth ();
 frameRate (25);
}

void draw (){
[...]
 
 limite += step;
 
 if ( limite > width || limite < 0 ){
   step = -step;
 }
}

[EDIT] Too late! Smiley
Re: Reverse a for loop ?
Reply #4 - Dec 22nd, 2009, 3:16pm
 
Awesome guys!   Smiley

thanx!  i was doing a loop bacuase i am reading the "learning processing" book , and this was a variation of an example of for loops.

but this code is more efficient , and it looks cool !

thanx !

Hasta la vista ! Wink Wink Wink
Page Index Toggle Pages: 1