Daily Creative Coding

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

擬似ヘックス

/**
* Hex
*
* @autor aa_debdeb
* @date 2015/11/30
*/

Hex[][] hexs; 

void setup(){
  size(500, 500);
  background(255);
  
  hexs = new Hex[21][21];
  for(int x = 0; x <= 20; x++){
    for(int y = 0; y <= 20; y++){
      hexs[x][y] = new Hex(12.5);
    }
  }

  noStroke();
  fill(255, 20, 147);
  for(int x = 0; x <= 20; x++){
    for(int y = 0; y <= 20; y++){
      pushMatrix();
      if(y % 2 == 0){
        translate(x * 25, y * 25);
      } else {
        translate(x * 25 + 12.5, y * 25);      
      }
      hexs[x][y].draw();
      popMatrix();
    }
  }
}

class Hex{
  
  float r;
  
  Hex(float r){
    this.r = r;
  }
  
  void draw(){
    float angle = TWO_PI / 12.0;
    beginShape();
    for(int i = 0; i < 6; i++){
      angle += TWO_PI / 6;
      vertex(r * cos(angle), r * sin(angle));
    }
    endShape(CLOSE);
  }
}