# 再帰的な三角形分割

/**
* recursive triangle division
*
* @author aa_debdeb
* @date 2016/10/16
*/

void setup(){
size(640, 640);
noStroke();
colorMode(HSB, 360, 100, 100);
mousePressed();
}

void mousePressed(){
float hue = random(360);
background(hue, 40, 100);
pushMatrix();
translate(width / 2, height * 3.0 / 5);
ArrayList<Triangle> triangles = new ArrayList<Triangle>();
while(triangles.size() != 0){
Triangle tri = triangles.remove(0);
if((tri.depth <= 1) || (tri.depth < 5 && random(1) < 0.9)){
} else {
fill(hue, random(40, 100), random(60, 100));
tri.display();
}
}
popMatrix();
}

void draw(){

}

class Triangle{

PVector loc;
float startAngle;
int depth;

Triangle(){
loc = new PVector(0, 0);
startAngle = -HALF_PI;
depth = 0;
}

Triangle(PVector _loc, float _radious, float _startAngle, int _depth){
loc = _loc;
startAngle = _startAngle;
depth = _depth;
}

ArrayList<Triangle> divide(){
ArrayList<Triangle> children = new ArrayList<Triangle>();
for(int i = 0; i < 3; i++){
PVector cLoc = new PVector(loc.x + cRadious * cos(i * TWO_PI / 3 + startAngle), loc.y + cRadious* sin(i * TWO_PI / 3 + startAngle));
float cStartAngle = startAngle + i * TWO_PI / 3;
}

return children;
}

void display(){
pushMatrix();
translate(loc.x, loc.y);
beginShape();
for(int i = 0; i < 3; i++){
vertex(radious * cos(i * TWO_PI / 3 + startAngle), radious * sin(i * TWO_PI / 3 + startAngle));
}
endShape(CLOSE);
popMatrix();
}

}