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 › I dont understand 2 things in this small program .
Page Index Toggle Pages: 1
I dont understand 2 things in this small program . (Read 784 times)
I dont understand 2 things in this small program .
Mar 29th, 2010, 2:24pm
 
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:

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!

}
   
Re: I dont understand 2 things in this small program .
Reply #1 - Mar 29th, 2010, 2:46pm
 
The first thing...this is a standard for loop.... you can use i for something inside the loop for something, but you don't have to. This construction:
for (int i=0;i<n_el;i++) just says do everything in the loop n_el times.

Second thing - this is the point where, when you reach the right edge of the screen, you move down to the next line on the left side of the screen. x=r :set x to the radius of the circle, so the next circle is drawn at the left side of the screen.

y+=2*r : this moves the y position down at the same time....so we draw the next row of circles the required 2r below the first row.
Re: I dont understand 2 things in this small program .
Reply #2 - Mar 30th, 2010, 3:08pm
 
thanx !


ill read it carefully
Smiley
Page Index Toggle Pages: 1