simple nested FOR loop: how to make a grid without using 'i & j' as 'x & y'?

edited February 2015 in Programming Questions

I know how to make a 10x10 grid of ellipses with FOR loops using the i & j from loop as x & y coordinates for the ellipses.

But I am trying to do the same ellipse grid, without using the i & j's. Just using them as counters. It works for one row of ellipses:

 size(200, 200);
 int x = 10;
 int y = 10;
 for (int i = 5; i < width; i = i+10) { 
     ellipse(x, y, 10, 10);
     x = x+20;
    }

but when I try to do a grid (adding the nested for), it doesn't work.

 size(200, 200);
 int x = 10;
 int y = 10;

 for (int i = 5; i < width; i = i+10) { 
   for (int j = 5; j < height; j = j+20) {
     ellipse(x, y, 10, 10);
     x = x+20;
     }
     y = y+20; 
 }

And I can not see what I am missing. Anyone? thanks!

Tagged:

Comments

  • edited February 2015

    After the end of the first row, the x value is still set at the end of the row! So when you start drawing the next row, you draw the first ellipse off the right edge of the screen!

    try adding the line "x = 10;" between lines 9 and 10.

  • edited February 2015

    YEEYY!! awesome! Makes Sense, I just couldn't see what what going on. Thanks!

    (p.s: I hadn't seen until now that you had edited your "hold on" comment with the answer. cool!)

  • Great. Remember, a program always does what you tell it. If it's not doing what you want, it's not because it's doing it wrong... it's because you told it to do the wrong thing! Rectifying the difference between what you think your program is doing and what it actually is doing is the most common form of fixing software bugs.

  • You're totally right. I correct "I couldn't see what I am not telling the code".

    As soon as I saw your answer and changed the width size to 3000, I could see the rows were going down in a sort of ladder.

Sign In or Register to comment.