Bug in logic for creating checkerboard grid

Hello,

I’m hoping someone can find the flaw in my logic here. My goal is to create an even overlay checkerboard that has a transparency of 50. Here is my code in setup:

background(255);
int fillColor = 0;

int gridDimensions = 32;

for (int i = 0; i < gridDimensions; i++) {
  if (gridDimensions % 2 == 0)fillColor ^= 1;
  for (int j = 0; j < gridDimensions; j++) {
    fillColor ^= 1;
    fill(fillColor*255, 50);
    rect(i*gridDimensions,
      j*gridDimensions,
      i*gridDimensions+width/gridDimensions,
      j*gridDimensions+height/gridDimensions);
  }
}

My result is unexpected. Here’s the image I get and I’m unsure where I went wrong.

the two last parameters of rect() are its width and height

Chrisir

    rect(i*gridDimensions, 
      j*gridDimensions, 
      width/gridDimensions - 5, 
      height/gridDimensions - 5);

Thank you for helping me out of my brain derp. Brain wanted the last two params to be ending x and y point of the rect. :pray:

The following code now works:

background(255);
int fillColor = 0;

int gridDimensions = 32;

for (int i = 0; i < gridDimensions; i++) {
  if (gridDimensions % 2 == 0)fillColor ^= 1;
  for (int j = 0; j < gridDimensions; j++) {
    fillColor ^= 1;
    fill(fillColor*255, 50);
    rect(
      i*width/gridDimensions,
      j*height/gridDimensions,
      width/gridDimensions,
      height/gridDimensions);
  }
}
1 Like