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

Daily Creative Coding

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

五角形の再帰分割

/**
* pentagon division
*
* @author aa_debdeb
* @date 2016/10/01
*/

float phi = (1.0 + sqrt(5.0)) / 2.0;

void setup(){
  size(640, 640);
  translate(width / 2, height / 2);
  ArrayList<Pentagon> pentagons = new ArrayList<Pentagon>();
  pentagons.add(new Pentagon());
  for(int i = 0; i < 4; i++){
    ArrayList<Pentagon> nextPentagons = new ArrayList<Pentagon>();  
    for(Pentagon p: pentagons){
      nextPentagons.addAll(p.divide());
    }
    pentagons = nextPentagons;
  }
  background(230);
  stroke(180);
  fill(60);
  for(Pentagon p: pentagons){
    p.display();
  }
}

void draw(){

}

class Pentagon{
  
  PVector center;
  float radious;
  float startAngle;
  
  Pentagon(){
    center = new PVector(0, 0);
    radious = 300;
    startAngle = -HALF_PI;
  }
  
  Pentagon(PVector _center, float _radious, float _startAngle){
    center = _center;
    radious = _radious;
    startAngle = _startAngle;
  }
  
  void display(){
    pushMatrix();
    translate(center.x, center.y);
    beginShape();
    for(int i = 0; i < 5; i++){
      float angle = startAngle + i * TWO_PI / 5;
      vertex(radious * cos(angle), radious * sin(angle));  
    }
    endShape(CLOSE);
    popMatrix();
  }
  
  ArrayList<Pentagon> divide(){
    ArrayList<Pentagon> children = new ArrayList<Pentagon>();
    float r = radious / sq(phi);
    children.add(new Pentagon(new PVector(center.x, center.y), r, startAngle + PI));
    for(int i = 0; i < 5; i++){
      float angle = startAngle + i * TWO_PI / 5;
      float x = center.x + (radious - r) * cos(angle);
      float y = center.y + (radious - r) * sin(angle);
      children.add(new Pentagon(new PVector(x, y), r, angle));
    }
    return children;
  }
}
f:id:aa_debdeb:20160926171628j:plain