Skip to content

Instantly share code, notes, and snippets.

@so-c
Created November 26, 2016 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save so-c/f5766666fbfc5948394a597243226f44 to your computer and use it in GitHub Desktop.
Save so-c/f5766666fbfc5948394a597243226f44 to your computer and use it in GitHub Desktop.
void setup() {
size(800, 600);
background(255);
stroke(119, 117, 173);
drawTree(width/2, height, 100, 0, 12);
save("fractal-tree.png");
println("done!");
}
/**
* rootX 枝のx座標
* rootY 枝のy座標
* len 枝の長さ
* stand 枝の方向から枝の傾き
* n 残りの再帰回数
**/
void drawTree(float rootX, float rootY, float len, float stand, int n) {
final float scale = 0.85; // 枝が短くなる比率
final float angle = 15; // 枝の広がり
float rootX2 = rootX + len * sin(radians(stand));
float rootY2 = rootY - len * cos(radians(stand));
stroke(round(119 + (1-rootY2/height) * (255 - 119)),
round(117 + (1-rootY2/height) * (255 - 117)),
round(173 + (1-rootY2/height) * (255 - 173)));
line(rootX, rootY, rootX2, rootY2);
if (n >= 1) {
drawTree(rootX2, rootY2, len*scale, stand - angle, n-1);
drawTree(rootX2, rootY2, len*scale, stand + angle, n-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment