We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Dear coders,
i want to draw a line by line-grid in Processing, but my program does not work anyway, it does not start properly, i guess it is because i try to reset x in the for-loop.
I there a good solution for that?
float y;
float a;
int cols = 16;
int rows = 50;
void setup() {
size(540, 540);
noStroke();
frameRate(10);
}
void draw() {
background(#0000ff);
for (float x = 0; x < width*height; x+=width/cols) {
if (x > width) {
y += height/rows;
x = 0;
}
fill(#ff0000);
rect(x, y, 1, 1);
}
// saveFrame();
}
Answers
Clarity over compactness ;)
Kf
you need another for-loop variable than x
(you shouldn't manipulate the for-loop variable in most cases)
Great, thank you guys!
x is remainder of i / cols (multiplied by width of each column)
y is the INTEGER division of i / cols (multiplied by the height of each row)
Compactness over clarity 8)
(but seriously, use kf's solution. much easier and, in fact, shorter than your original)