Daily Creative Coding

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

円からなる再帰的な木

/**
* recursive tree of circles
*
* @author aa_debdeb
* @date 2016/09/08
*/

float MAX_RADIOUS = 100;
float MIN_RADIOUS = 50;
float MAX_CIRCLE = 80;
float MIN_CIRCLE = 20;
int MAX_DEPTH = 6;
int BRANCH_NUM = 3;
Node root;

void setup(){
  size(640, 640);
  root = new Node(1);
}

void draw(){
  background(60);
  translate(width / 2, height / 2);
  root.display();
  root.update();
}

class Node{
  
  ArrayList<Node> branches;
  float radious, angle;
  int depth;
  float circle;
  float phase;
  
  Node(int _depth){
    depth = _depth;
    radious = map(depth, 1, MAX_DEPTH, MAX_RADIOUS, MIN_RADIOUS);
    angle = random(TWO_PI);
    circle = map(depth, 1, MAX_DEPTH, MAX_CIRCLE, MIN_CIRCLE);
    branches = new ArrayList<Node>();
    if(depth + 1 <= MAX_DEPTH){
      for(int i = 0; i < BRANCH_NUM; i++){
        branches.add(new Node(depth + 1));
      }
    }
    phase = random(TWO_PI);
  }
  
  void display(){
//    stroke(255, 179, 242, 150);
//    strokeWeight(2);
    float move = map(sin(frameCount * 0.01 + phase), -1, 1, -PI / 3, PI / 3);
//    for(Node b: branches){
//      line(0, 0, b.radious * cos(b.angle + move), b.radious * sin(b.angle + move)); 
//    }
    stroke(0, 191, 255);
    strokeWeight(5);
    fill(204, 243, 255);
    ellipse(0, 0, circle, circle);
    strokeWeight(2);
    fill(216, 204, 255);
    ellipse(0, 0, circle * 0.5, circle * 0.5);
    for(Node b: branches){
      pushMatrix();
      translate(b.radious * cos(b.angle + move), b.radious * sin(b.angle + move));
      b.display();
      popMatrix();
    }
  }
  
  void update(){
    for(Node b: branches){
      b.update();
    }
  }
    
}
f:id:aa_debdeb:20160907181407j:plain