Daily Creative Coding

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

色を変えながらマスを塗りつぶしていく

/**
* line drawer
* 
* @author aa_debdeb
* @date 2017/01/24
*/

int CELL_SIZE = 2;
int CELL_NUM = 250;
int[][] DIRECTIONS = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
float MAX_STRAIT = 50;
color BG_COLOR = color(255, 255, 255);
color[] FILL_COLORS = {
  color(241, 142, 29),
  color(70, 83, 162),
  color(216, 29, 106)
};

boolean[][] isFilled;
int posX, posY;
int direction;
int moveCount;
int fillColor;

void setup(){
  size(500, 500);
  frameRate(120);
  noStroke();
  isFilled = new boolean[CELL_NUM][CELL_NUM];
  for(int x = 0; x < CELL_NUM; x++){
    for(int y = 0; y < CELL_NUM; y++){
      isFilled[x][y] = false;
    }
  }
  
  posX = int(random(CELL_NUM));
  posY = int(random(CELL_NUM));
  direction = int(random(4));
  moveCount = 0;
  fillColor = 0;
  
  background(BG_COLOR);
}

void draw(){
  if(fillColor < FILL_COLORS.length){
    fill(FILL_COLORS[fillColor]); 
    rect(posX * CELL_SIZE, posY * CELL_SIZE, CELL_SIZE, CELL_SIZE);
    isFilled[posX][posY] = true;
  } else {
     fill(BG_COLOR); 
    rect(posX * CELL_SIZE, posY * CELL_SIZE, CELL_SIZE, CELL_SIZE);
    isFilled[posX][posY] = false;   
  }
  
  posX += DIRECTIONS[direction][0];
  posY += DIRECTIONS[direction][1];  
  if(posX < 0){posX = CELL_NUM - 1;}
  if(posX >= CELL_NUM){posX = 0;}
  if(posY < 0){posY = CELL_NUM - 1;}
  if(posY >= CELL_NUM){posY = 0;}
  
  moveCount++;
  
  if(fillColor < FILL_COLORS.length && isFilled[posX][posY]){
    fillColor += 1;
  } else if(fillColor == FILL_COLORS.length && !isFilled[posX][posY]) {
    fillColor = 0;
  }
  if(random(1) < sq(min(moveCount, MAX_STRAIT) / MAX_STRAIT)){
    direction += random(1) < 0.5 ? -1: 1;
    if(direction == -1){direction = 3;}
    if(direction == 4){direction = 0;}
    moveCount = 0;
  }
}
f:id:aa_debdeb:20170119091501j:plain