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

Daily Creative Coding

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

亀甲模様

/**
* hexagonal pattern
* 
* @author aa_debdeb
* @date 2016/07/03
*/

float e = 15.0;

void setup(){
  size(640, 480);
  mousePressed();
}

void draw(){
  
}

void mousePressed(){
  color c1 = color(random(255), random(255), random(255));
  color c2 = color(random(255), random(255), random(255));  
  strokeWeight(3);
  float margin = random(e, 50);
  boolean isDisplayed = false;
  for(float y = -margin; y < height + margin; y += 3.0 / 2.0 * e){
    for(float x = -margin; x < width + margin; x += sqrt(3) * e){
      pushMatrix();
      if(!isDisplayed){
        translate(x, y);
      } else {
        translate(x + sqrt(3) / 2.0 * e, y);     
      }
       
      stroke(c1);
      fill(c2);
      beginShape();
      for(float angle = 0.0; angle < 360; angle += 60){
        float radian = radians(angle - 90);
        vertex(e * cos(radian), e * sin(radian)); 
      }
      endShape(CLOSE);
      float rnd = random(1);
      if(rnd < 0.33){
        beginShape();
        for(float angle = 0.0; angle < 360; angle += 60){
          float radian = radians(angle - 90);
          vertex((e * 0.5) * cos(radian), (e * 0.5) * sin(radian)); 
        }
        endShape(CLOSE);      
      } else if(rnd < 0.66){
        noStroke();
        stroke(c1);
        fill(c1);
        beginShape();
        for(float angle = 0.0; angle < 360; angle += 60){
          float radian = radians(angle - 90);
          vertex((e * 0.5) * cos(radian), (e * 0.5) * sin(radian)); 
        }
        endShape(CLOSE);       
      }
      popMatrix();
    }
    isDisplayed = !isDisplayed;
  }

}