Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

積み重ねセルオートマトン

/**
* Incrustation
* 
* @author aa_debdeb
* @date 2016/02/01
*/

int[][] cells;
int cellNum = 50;
int cellSize = 10;

void setup(){
  size(500, 500);
  frameRate(1000);
  noStroke();
  colorMode(HSB, 360, 100, 100);
  
  cells = new int[cellNum][cellNum];
  for(int x = 0; x < cellNum; x++){
    for(int y = 0; y < cellNum; y++){
      cells[y][x] = 1;
    }
  }
}

void draw(){
  for(int x = 0; x < cellNum; x++){
    for(int y = 0; y < cellNum; y++){
      fill(16, 100, cells[y][x]);
      rect(x * cellSize, y * cellSize, cellSize, cellSize);
    }
  }
  
  int cellSum = 0;
  int[][] cellProb = new int[cellNum][cellNum];
  for(int x = 0; x < cellNum; x++){
    for(int y = 0; y < cellNum; y++){
      int up = y == 0 ? cellNum - 1: y - 1;
      int down = y == cellNum - 1 ? 0: y + 1;
      int left = x == 0 ? cellNum - 1: x - 1;
      int right = x == cellNum - 1 ? 0: x + 1;
      int sum = cells[y][x] + cells[up][x] + cells[down][x] + cells[y][left] + cells[y][right];
      cellSum += sum;
      cellProb[y][x] = sum;
    }
  } 
  float rand = random(1); 
  float randSum = 0;
  for(int x = 0; x < cellNum; x++){
    for(int y = 0; y < cellNum; y++){
      randSum += float(cellProb[y][x]) / float(cellSum);
      if(randSum > rand){
        cells[y][x] += 16;
        if(cells[y][x] >= 100){
          cells[y][x] = 1;
        }
        return;      
      }
    }
  } 
}