Created
December 26, 2024 00:52
-
-
Save sasaki-shigeo/f62722c777c1bd5de9fbbccdb55707dd to your computer and use it in GitHub Desktop.
Cedar tree drawing in Processing by hierarchy structure / Processing で杉の木を階層構造で描く
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
size(500, 500); | |
noLoop(); | |
} | |
void draw() { | |
translate(width/2, 0.9 * height); | |
scale(1, -1); | |
tree(0.8 * height, 5); | |
} | |
void cedarLeaf(float len) { | |
float unit = len/4; | |
float rx = cos(PI/6) * unit; // == √3/2 * unit | |
float ry = sin(PI/6) * unit; // == 0.5 * unit | |
line(0, 0, 0, len); | |
for (int i = 1; i < 4; i++) { | |
line(0, i * unit, rx, i * unit + ry); | |
line(0, i * unit, -rx, i * unit + ry); | |
} | |
} | |
void tree(float len, int nBranches) { | |
push(); | |
// trunk | |
stroke(#663300); | |
strokeWeight(10); | |
line(0, 0, 0, len); | |
// branch | |
float interval = len / (nBranches + 1); | |
for (int i = 1; i <= nBranches; i++) { | |
pushMatrix(); | |
translate(0, i * interval); | |
rotate(PI/3); | |
branch(0.5 * (nBranches - i + 1) * interval, 30, 2 * (nBranches - i + 1)); | |
rotate(-2 * PI / 3); | |
branch(0.5 * (nBranches - i + 1) * interval, 30, 2 * (nBranches - i + 1)); | |
popMatrix(); | |
} | |
pop(); | |
} | |
void branch(float len, float leafLen, int nLeaves) { | |
push(); | |
stroke(#663300); | |
strokeWeight(5); | |
line(0, 0, 0, len); | |
// leaf | |
stroke(#008800); | |
strokeWeight(3); | |
for (int i = 1; i <= nLeaves; i++) { | |
pushMatrix(); | |
translate(0, i * len / nLeaves); | |
rotate(PI/3); | |
line(0, 0, 0, leafLen); | |
rotate(-2 * PI/3); | |
line(0, 0, 0, leafLen); | |
popMatrix(); | |
} | |
pop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment