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

30 min. Processing

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

ブーリアンネットワーク

ネットワーク
/**
* Boolean Network
*
* @author aa_debdeb
* @date 2015/12/28
*/

int MAX_POINT = 2000;
PVector positions[];
boolean states[];
boolean hasLinks[][];

void setup(){
  size(500, 500);
  smooth();
  frameRate(10);
  
  positions = new PVector[MAX_POINT];
  states = new boolean[MAX_POINT];
  hasLinks = new boolean[MAX_POINT][MAX_POINT];
  
  for(int i = 0; i < MAX_POINT; i++){
    positions[i] = new PVector(random(width), random(height));
    states[i] = random(1) < 0.01;
  }
  for(int i = 0; i < MAX_POINT - 1; i++){
    for(int j = i; j < MAX_POINT; j++){
      if(i == j){
        hasLinks[i][j] = false;
      }
      float distance = PVector.dist(positions[i], positions[j]); 
      if(distance < random(1) * 50){
        hasLinks[i][j] = hasLinks[j][i] = true;        
      } else {
        hasLinks[i][j] = hasLinks[j][i] = false;
      }  
    }
  }
}

void draw(){
  background(0);
  
  stroke(64);
  strokeWeight(1);
  for(int i = 0; i < MAX_POINT - 1; i++){
    for(int j = i + i; j < MAX_POINT; j++){
      if(hasLinks[i][j]){
        line(positions[i].x, positions[i].y, positions[j].x, positions[j].y);
      }
    }
  }
  
  noStroke();
  for(int i = 0; i < MAX_POINT; i++){
    if(states[i]){ 
      fill(255);
    } else {
      fill(128);    
    }
    ellipse(positions[i].x, positions[i].y, 4, 4);
  }
  
  boolean nextStates[] = new boolean[MAX_POINT];
  for(int i = 0; i < MAX_POINT; i++){
    if(states[i]){
      nextStates[i] = false;
    } else {
      for(int j = 0; j < MAX_POINT; j++){
        if(hasLinks[i][j] && states[j]){
          nextStates[i] = true;
          break;
        }
      }
    }
  }
  states = nextStates;
}