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 › How to loop a loop
Page Index Toggle Pages: 1
How to loop a loop ? (Read 1554 times)
How to loop a loop ?
Mar 18th, 2010, 4:56pm
 
Hi , i have this loop of circles apearing from left to right.


how do i loop this loop in another way ?

like for example verticaly ?

any ideas ?

Code:
int endx = 0;
int cx = 30;
int endy = 0;
void setup (){
size (300,300);
background (#ff0044);
smooth ();
frameRate (30);
}

void draw (){
background (#ff0044 );
for ( int x = 0 ; x < endx ; x+=30 ){
stroke (255);
ellipse ( x+cx/2,cx/2, cx,cx );
}


endx +=10;

if ( endx > width ){
endx = 0;
}

}






Thanx !





...
Re: How to loop a loop ?
Reply #1 - Mar 18th, 2010, 5:16pm
 
You could do that...


Code:
int endx = 0;
int cx = 30;
int endy = 0;
void setup (){
 size (300,300);
 background (#ff0044);
 smooth ();
 frameRate (30);
}

void draw (){
  background (#ff0044);
  int y=0;
  //Fill lines up to current height
  for (; y < endy ; y+=30 ){
    for ( int x = 0 ; x < width ; x+=30 ){
      stroke (255);
      ellipse ( x+cx/2,y+cx/2, cx,cx );
    }
  }
 for ( int x = 0 ; x < endx ; x+=30 ){
  stroke (255);
 ellipse ( x+cx/2,y+cx/2, cx,cx );
 }


endx +=10;

if ( endx > width ){
 endx = 0;
 // Each time the endx is reached increase current height
 endy+=30;
}
// Reset current height
if (endy > height) {
 endy=0;
}
 }


Is it what yr looking for?
Re: How to loop a loop ?
Reply #2 - Mar 19th, 2010, 1:43am
 
cool !

but why are the ellipse like with a strange border , like if they where overlapped ?

thanx!
Re: How to loop a loop ?
Reply #3 - Mar 19th, 2010, 4:43am
 
maybe because of stroke?

try

noStroke();

see if it helps...

glad to be of help..
Re: How to loop a loop ?
Reply #4 - Mar 19th, 2010, 4:25pm
 
Thanx !


I have to get  this , loops are a basic part of programming right ?


But i get lost in the second loop. To me it seems as if both are the same.

theirelllipse values are the same , how can then the vertical addition be posible ?

hope you can help , thanx !     Smiley


...
Re: How to loop a loop ?
Reply #5 - Mar 19th, 2010, 5:19pm
 
The trick here is that y gets increased once more before the first loop gets invalidated. So, the sequence, at some point in time, is:

1. A line is drawn at the current y-position within the 1st loop.
2. y gets increased.
3. the loop checks for y<endy; It fails and exits. But the y has been increased already Wink
4. The next loop starts with the updated y-position and draws the incomplete line in the correct y-pos.

You could also check this code (it uses one loop):

Code:
int r=15; //Radius
int nel=0;   //Initial Number of ellipses

void setup() {
 size(300,300);
 background(#ff0044);
 smooth();
 frameRate(20);
}

void draw() {
 background(#ff0044);
 int x=r,y=r; // Initialize position variables.
// Ellipse will be drawn in that position
 fill(255);
 noStroke();
 for (int i=0;i<nel;i++) { //For up to the current number of ellipses...
   ellipse(x,y,2*r,2*r); // First draw an ellipse..
   x+=2*r; //Then increase the x position.
   if (x>width) { // But if it is more than width
     y+=2*r; //Increase y and..
     x=r; // Zero x(=radius). Essentially do a 'line break'
   }
   if (y>height) { // If we reach the bottom point
     nel=0; // Set number fo ellipses back to zero, to start again.
            // this also has the effect that it
// immediately cancels the for loop.
            // since the counter (nel) becomes zero.
   }
 } // Repeat up to nel.

 nel++; // After we painted all our ellipses once,
// increase their number
        // so that the next time we'll paint one more!
}
   

Re: How to loop a loop ?
Reply #6 - Mar 19th, 2010, 10:37pm
 
Please in future post the code as text so that it can be copy'n'pasted (and quoted). You can add comments to the code to indicate points of interest.

Ignoring the question of what you are trying to do (why the second loop to apparently do the same thing for one row?..), I think yconst has found the problem.

Code:
int y=0;

for (; y < endy ; y+= cx ){
 // ...
}

// (y < endy) == false
// for first call to draw(): "y+=cx" -> y == cx


The operation of a for() loop is as follows, where "end-of-loop" doesn't mean exiting, just what happens at the end of each loop cycle:

for (initalise-statement; test; end-of-loop) { loop-body }

1. execute initalise-statement (typically set up loop counters, etc, but can be many things; recommendation is to keep it to setting up loop control)
2. test is tested; if false, loop is exited
3. execute loop-body
4. execute end-of-loop
5. go back to step 2.

Possible additional complications in loop-body:
  • continue: remainder of loop-body is skipped, but program continues with step 4 (execute end-of-loop)
  • break: remainder of loop-body is skipped and loop is exited (does not execute end-of-loop)
  • return: returns from a method, as usual (loop is exited immediately)
  • thrown exceptions: program flow passed to wherever exception is caught (loop is exited immediately if the exception isn't caught inside the loop-body)


-spxl
Re: How to loop a loop ?
Reply #7 - Mar 23rd, 2010, 3:25pm
 
thanx ! im not doing anything in particular just trying to learn  Wink


So guess i have figured it out , by comenting each line and trying to explain it.



i understand all but the part of    end += 50


i dont understand why if 50 is changed to 100 the speed goes up ?

can someone explain ?

Code:

int endx = 0;
int cx = 30;
int endy = 0;
void setup (){
size (300,300);
background (#ff0044);
smooth ();
frameRate (30);
}

void draw (){
background (#ff0044);
int y=0;
//Fill lines up to current height
for (; y < endy ; y+=30 ){ // a loop just for increasing y
for ( int x = 0 ; x < width ; x+=30 ){ // loop to increase x
stroke (255);
ellipse ( x+cx/2,y+cx/2, cx,cx ); //ellipse is painted at x = 45 y = 15
}
}
for ( int x = 0 ; x < endx ; x+=30 ){ // a loop to increase x ,in relation to endx
stroke (255);
ellipse ( x+cx/2,y+cx/2, cx,cx ); // x is now 75+15=90 y is still 15
}






endx +=50; // end x is augmented by 50 ,but why does it sppeds up the sketch with greater values ?








if ( endx > width ){ //if endx is greater than width endx is 0
endx = 0;
// Each time the endx is reached increase current height
endy+=30;
}
// Reset current height
if (endy > height-cx/2) {
endy=0;
}
}



thanx !
Re: How to loop a loop ?
Reply #8 - Mar 23rd, 2010, 10:28pm
 
If you count to 10 (out aloud), counting by 1, how long does it take?
"One, two, three, four, five, six, seven, eight, nine, ten!"

If you count to 10, counting by 2, how long does it take?
"Two, four, six, eight, ten!"

Why is counting by twos faster than counting by 1? This is the same reason as why counting by 100 is faster than counting by 50.

-spxl
Re: How to loop a loop ?
Reply #9 - Mar 25th, 2010, 3:36pm
 
thanx ,


subpixel ,



i dont understand the part of :

 for (int i=0;i<n_el;i++) {

because i dont see where is i being used. is not being applied anywhere in the sketch.



i also dont understand this piece :

x=r; // Zero x(=radius). Essentially do a 'line break'


what is this doing ? reseting x to 15 ?


and last , i dont understand how n_el works


this is the code :

Code:
int r= 22; //Radius
int n_el=0; //Initial Number of ellipses

void setup() {
size(800,300);
background(#ff0044);
smooth();
frameRate(20);
}

void draw() {

background(#ff0044);

int x=r,y=r; // Initialize position variables.
// Ellipse will be drawn in that position
fill(255);
noStroke();
for (int i=0;i<n_el;i++) { //For up to the current number of ellipses...
ellipse(x,y,2*r,2*r); // First draw an ellipse..
x+=2*r; //Then increase the x position.
if (x>width) { // But if it is more than width
y+=2*r; //Increase y and..
x=r; // Zero x(=radius). Essentially do a 'line break'
}
if (y>height) { // If we reach the bottom point
n_el=0; // Set number fo ellipses back to zero, to start again.
// this also has the effect that it
// immediately cancels the for loop.
// since the counter (nel) becomes zero.
}
} // Repeat up to nel.

n_el++; // After we painted all our ellipses once,
// increase their number
// so that the next time we'll paint one more!

}


Page Index Toggle Pages: 1