Celular automata won't wrap around the window

I have partially implemented the Huegene cellular automata with processing, but I still have an issue:
it wraps around the screen but stops just after, and I have no idea why
code:

color[][] grid;
int MUTATION_RATE = 5;


void setup(){
  size(1000,1000);
  grid = new color[width][height];
  
  // place the first two points
  grid[width/2][height/4] = color(255, 100, 100);
  grid[width/3][height/3] = color(100, 100, 255);
  
  background(grid[0][0]);
}

boolean isPixelEmpty(color pixel){
  return red(pixel) == 0f && green(pixel) == 0f && blue(pixel) == 0f;
}
int w = 0;
void draw(){
  
  loadPixels();
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
      //current pixel color on the old grid
      var pixelColor = grid[x][y];
      if(isPixelEmpty(pixelColor)) continue;
      
      for(int i = x-1; i <= x+1; i++){
        // wrap i
        i = (i+height) % height;
        for(int j = y-1; j <= y+1; j++){
          // wrap j
          j = (j+width) % width;
          
          // if next cell is full ignore + randomness
          if(!isPixelEmpty(pixels[j*width+i])  || round(random(1)) == 0) continue;
          
          // mutate the new color
          var newColor = color(
            red(pixelColor) + random(-MUTATION_RATE, MUTATION_RATE),
            green(pixelColor) + random(-MUTATION_RATE, MUTATION_RATE),
            blue(pixelColor) + random(-MUTATION_RATE, MUTATION_RATE)
          );
       
          pixels[j*width+i] = newColor;
        }
      }
    }
  }
  updatePixels();
  

  
  // put current grid inside of the old grid
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
      grid[x][y] = pixels[y*width+x];
    }
  }
}


there is only one row of pixels that wrap on the bottom:

Hi @rafalou38 you’re overriding the variables i and j for the modulo, if you create two new variables for them then use that as the index values for the pixels it works, theres probably a way to do it in the for loop itself but i can never wrap my head around modulo

color[][] grid;
int MUTATION_RATE = 5;


void setup(){
  size(1000,1000);
  grid = new color[width][height];
  
  // place the first two points
  grid[width/2][height/4] = color(255, 100, 100);
  grid[width/3][height/3] = color(100, 100, 255);
  
  background(grid[0][0]);
}

boolean isPixelEmpty(color pixel){
  return red(pixel) == 0f && green(pixel) == 0f && blue(pixel) == 0f;
}
int w = 0;
void draw(){
  
  loadPixels();
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
      //current pixel color on the old grid
      color pixelColor = grid[x][y];
      if(isPixelEmpty(pixelColor)) continue;
      
      for(int i = x-1; i <= x+1; i++){
        // wrap i
        int ii = (i+height) % height;
        for(int j = y-1; j <= y+1; j++){
          // wrap j
          int jj = (j+width) % width;
          
          // if next cell is full ignore + randomness
          if(!isPixelEmpty(pixels[jj*width+ii])  || round(random(1)) == 0) continue;
          
          // mutate the new color
          color newColor = color(
            red(pixelColor) + random(-MUTATION_RATE, MUTATION_RATE),
            green(pixelColor) + random(-MUTATION_RATE, MUTATION_RATE),
            blue(pixelColor) + random(-MUTATION_RATE, MUTATION_RATE)
          );
       
          pixels[jj*width+ii] = newColor;
        }
      }
    }
  }
  updatePixels();
  

  
  // put current grid inside of the old grid
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
      grid[x][y] = pixels[y*width+x];
    }
  }
}
2 Likes