# Celular automata won't wrap around the window

**URL:** <https://discourse.processing.org/t/celular-automata-wont-wrap-around-the-window/37229>\
**Category:** Coding Questions\
**Created:** [May 30, 2022, 2:36pm UTC](https://discourse.processing.org/t/celular-automata-wont-wrap-around-the-window/37229 "2022-05-30T14:36:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafalou38](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafalou38/32/16353_2.png) [@rafalou38](https://discourse.processing.org/u/rafalou38)\
**Post date:** [May 30, 2022, 2:36pm UTC](https://discourse.processing.org/t/celular-automata-wont-wrap-around-the-window/37229/1 "2022-05-30T14:36:38Z")

</div>

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:

```auto
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];
    }
  }
}

```

 ![Capture d’écran 2022-05-30 163253](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/5/056bf316ae90c1ce4da8ea2f2e380df83e057631.jpeg)  
there is only one row of pixels that wrap on the bottom:

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [May 31, 2022, 6:01am UTC](https://discourse.processing.org/t/celular-automata-wont-wrap-around-the-window/37229/2 "2022-05-31T06:01:47Z")

</div>

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

```auto
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];
    }
  }
}

```
