読者です 読者をやめる 読者になる 読者になる

30 min. Processing

毎日30分、Processingで何かを作る

侵入型パーコレーションモデル

セルオートマトン
/**
* invation percolation model
* 
* @author aa_debdeb
* @date 2016/08/08
*/

float[][] values;
int[][] states;
ArrayList<PVector> targets;
int[][] dir = {{0, -1},
               {1, 0},
               {0, 1},
               {-1, 0}};

void setup(){
  size(400, 400);
  frameRate(360);
  values = new float[width][height];
  states = new int[width][height];
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      values[x][y] = random(1);
      states[x][y] = 0;
    }
  }
  targets = new ArrayList<PVector>();
  int x = int(random(width));
  int y = int(random(height));
  states[x][y] = 1;
  for(int i = 0; i < 4; i++){
    int nx = x + dir[i][0];
    int ny = y + dir[i][1];
    if(isIn(nx, ny) && states[nx][ny] == 0){
      targets.add(new PVector(nx, ny));
      states[nx][ny] = 1;
    }
  }
  background(255);
  point(x, y);
}

void draw(){
  if(targets.size() == 0){return;}
  PVector minT = null;
  float minV = 100; 
  for(PVector t: targets){
    float v = values[int(t.x)][int(t.y)];
    if(minV > v){
      minV = v;
      minT = t;
    }
  }
  targets.remove(minT);
  point(minT.x, minT.y);
  for(int i = 0; i < 4; i++){
    int nx = int(minT.x) + dir[i][0];
    int ny = int(minT.y) + dir[i][1];
    if(isIn(nx, ny) && states[nx][ny] == 0){
      targets.add(new PVector(nx, ny));
      states[nx][ny] = 1;
    }
  } 
}

boolean isIn(int x, int y){
  return 0 <= x && x < width && 0 <= y && y < height;
}
f:id:aa_debdeb:20160802171138j:plain