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

Daily Creative Coding

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

円のタイル

静画
/**
* circular filled tile
*
* @author aa_debdeb
* @date 2016/03/22
*/

int CELL_NUM = 25;
int CELL_SIZE = 20;

void setup(){
  size(500, 500);
  mousePressed();
}

void draw(){

}

void mousePressed(){
  background(random(255), random(255), random(255));
  noStroke();
  fill(random(255), random(255), random(255)); 
   
  Cell[][] cellMap = new Cell[CELL_NUM][CELL_NUM];
  ArrayList<Cell> cellQueue = new ArrayList<Cell>();
  for(int i = 0; i < CELL_NUM; i++){
    for(int j = 0; j < CELL_NUM; j++){
      Cell cell = new Cell(i, j);
      cellMap[i][j] = cell;
    }
  }
  cellQueue.add(cellMap[int(CELL_NUM / 2)][int(CELL_NUM / 2)]);
  while(!cellQueue.isEmpty()){
    Cell cell = cellQueue.remove(0);
    if(cell.isDecided){continue;}
    
    int[] dx = {0, 1, 0, -1};
    int[] dy = {-1, 0, 1, 0};
    int[][] dc = {{2, 3}, {3, 0}, {0, 1}, {1, 2}}; 
    int[] connect = new int[4]; // up, right, down, left , 0 = not connect, 1 = both, 2 = must connect
    for(int i = 0; i < 4; i++){
      int nx = cell.row + dx[i];
      int ny = cell.col + dy[i];
      if(!(0 <= nx && nx < CELL_NUM && 0 <= ny && ny < CELL_NUM)){
        connect[i] = 1;
      } else {
        if(!cellMap[ny][nx].isDecided){
          connect[i] = 1;      
        } else if(cellMap[ny][nx].states[dc[i][0]] || cellMap[ny][nx].states[dc[i][1]]){
          connect[i] = 2;      
        } else {
          connect[i] = 0;      
        }
        cellQueue.add(cellMap[ny][nx]);
      }
    }
    
    ArrayList<boolean[]> possibleStates = new ArrayList<boolean[]>();
    boolean[] truefalse = {true, false};
    for(boolean s0: truefalse){
      for(boolean s1: truefalse){
        for(boolean s2: truefalse){
          for(boolean s3: truefalse){
            boolean[] s = {s0, s1, s2, s3};
            possibleStates.add(s);
          }
        }
      }
    }
        
    int[][] conDir = {{0, 1}, {1, 2}, {2, 3}, {3, 0}};
    for(int i = 0; i < 4; i++){
      ArrayList<boolean[]> nextPS = new ArrayList<boolean[]>();
      for(boolean[] s: possibleStates){
        int con1 = conDir[i][0];
        int con2 = conDir[i][1];
        if(connect[i] == 0 && (s[con1] || s[con2])){
          continue;
        } else if(connect[i] == 2 && (!s[con1] && !s[con2])){
          continue;
        }
        nextPS.add(s);
      }
      possibleStates = nextPS;
    }
    cell.states = possibleStates.get(int(random(possibleStates.size())));
    cell.isDecided = true;
        
  }
  
  for(int i = 0; i < CELL_NUM; i++){
    for(int j = 0; j < CELL_NUM; j++){
      cellMap[i][j].display();
    }
  }
}

class Cell {

  int row, col;
  boolean isDecided;
  boolean[] states; // up left, up right, down right, down left
  
  Cell(int col, int row){
    this.col = col;
    this.row = row;
    isDecided = false;
    states = new boolean[4];
    for(int i = 0; i < 4; i++){
      states[i] = false;
    }
  }
  
  void display(){
    pushMatrix();
    translate(row * CELL_SIZE, col * CELL_SIZE);  
    if(states[0]){arc(0, 0, CELL_SIZE, CELL_SIZE, 0, HALF_PI);}
    if(states[1]){arc(CELL_SIZE, 0, CELL_SIZE, CELL_SIZE, HALF_PI, 2 * HALF_PI);}
    if(states[2]){arc(CELL_SIZE, CELL_SIZE, CELL_SIZE, CELL_SIZE, 2 * HALF_PI, 3 * HALF_PI);}
    if(states[3]){arc(0, CELL_SIZE, CELL_SIZE, CELL_SIZE,  3 * HALF_PI, 4 * HALF_PI);}
    popMatrix();  
  }
  
}