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

Daily Creative Coding

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

年輪

/**
* Annual Growth Ring
*
* @author aa_debdeb
* @date 2016/02/08
*
*/

int LOOP_NUM = 60;

void setup(){
  size(500, 500);
  noLoop();
  smooth();
  background(255);
  
  ArrayList<ArrayList<Vertex>> verticiesList = new ArrayList<ArrayList<Vertex>>(); 
  float angle = 0.0;
  ArrayList<Vertex> verticies = new ArrayList<Vertex>();
  while(angle <= 360){
    float r = map(random(1), 0, 1, 5, 10);   
    angle += map(random(1), 0, 1, 3, 6);
    verticies.add(new Vertex(r, angle));
  }
  verticiesList.add(verticies);
  for(int i = 0; i < LOOP_NUM - 1; i++){
    ArrayList<Vertex> preVerticies = verticiesList.get(i);
    ArrayList<Vertex> nextVerticies = new ArrayList<Vertex>();
    for(Vertex v: preVerticies){
      float r = v.radious + map(random(1), 0, 1, 3, 5);
      nextVerticies.add(new Vertex(r, v.angle));
    }
    verticiesList.add(nextVerticies);
  }

  noFill();
  stroke(0);
  strokeWeight(1);
  translate(width / 2, height / 2);  
  for(int i = 0; i < LOOP_NUM; i++){
    ArrayList<Vertex> currentVerticies = verticiesList.get(i);
    beginShape();
    for(Vertex v: currentVerticies){
      curveVertex(v.radious * cos(radians(v.angle)), v.radious * sin(radians(v.angle)));
    }
    for(int vi = 0; vi < 3; vi++){
      Vertex v = currentVerticies.get(vi);
      curveVertex(v.radious * cos(radians(v.angle)), v.radious * sin(radians(v.angle)));
    }
    endShape();
  }
}

class Vertex{
  float radious;
  float angle;
  
  Vertex(float radious, float angle){
    this.radious = radious;
    this.angle = angle;
  }
}