Daily Creative Coding

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

青海波模様

/**
* Seigaiha pattern
*
* @author aa_debdeb
* @date 2016/07/06
*/

float r = 50;
float hStep = r / sqrt(3);
float[] radians;

void setup(){
  size(640, 480);
  noFill();
  strokeWeight(2);
  radians = new float[10];
  radians[0] = 0.0;
  for(int i = 1; i < 10; i++){
    float v = (i + 1) * 0.1;
    float e1 = 2 * (r / sqrt(3));
    float e2 = r;
    float e3 = v * r;
    float s = (e1 + e2 + e3) / 2.0;
    float S = sqrt(s * (s - e1) * (s - e2) * (s - e3));
    float h = (2 * S) / e1;
    radians[i] = asin(h / e3) - PI / 6;
  }
  mousePressed();
}

void draw(){

}

void mousePressed(){
  background(random(255), random(255), random(255));
  stroke(random(255), random(255), random(255));
  float margin = random(r, r * 2);
  boolean isDisplaced = false;
  for(float h = -margin; h < height + margin; h += hStep){
    for(float w = -margin; w < width + margin; w += 2 * r){
      float w_ = w;
      if(isDisplaced){
        w_ += r;
      }     
      for(int i = 0; i < 10; i++){
        float r_ = (i + 1) * 0.1 * r;
        arc(w_, h, 2 * r_, 2 * r_, PI + radians[i], TWO_PI - radians[i]);
      }
    }
    isDisplaced = !isDisplaced;
  }
}