Brain lock 2D array

I trying to generate a grid of rectangles i have it looping in X and in Y, but Y starts where the X loop ends not sure what i am doing wrong (Need something like a checker board.

float xPos = 0.0f;
float yPos = 0.0f;
for (int i = 0; i < numY; i++) {
for (int j = 0; j < numX; j++) {
	ofNoFill();
	ofRect(xPos, yPos, ofGetWidth() / numX, ofGetHeight() / numY);
	xPos += (ofGetWidth() / numX) * 1.0f;
}
yPos += (ofGetHeight() / numY) * 1.0f;
}

Hi hive8,

you have to reset xPos to 0.0 when you come to a new row. You want the new row to start at the very left, at xPos 0, not wherever it left (which is, for the second row, the very right edge of the window - so you cannot see anything).

So just add xPos = 0; after the inner loop:

float xPos = 0.0f;
float yPos = 0.0f;

for (int i = 0; i < numY; i++) {
    for (int j = 0; j < numX; j++) {
    ofNoFill();
    ofRect(xPos, yPos, ofGetWidth() / numX, ofGetHeight() / numY);
    xPos += (ofGetWidth() / numX) * 1.0f;
    }
yPos += (ofGetHeight() / numY) * 1.0f;
xPos = 0;
}

Hope that helps!

Cheers

Thank you that worked :slight_smile: