Skip to content

Instantly share code, notes, and snippets.

@sasaki-shigeo
Last active December 25, 2024 09:15
Show Gist options
  • Save sasaki-shigeo/e5d57b926523371ed668a362b7305a07 to your computer and use it in GitHub Desktop.
Save sasaki-shigeo/e5d57b926523371ed668a362b7305a07 to your computer and use it in GitHub Desktop.
color trunkcolor = #663300;
color leafcolor = #00AA00;
class Crystal {
float x, y, phi;
Crystal(float x0, float y0, float phi0) {
x = x0; y = y0; phi = phi0;
}
Crystal(float x0, float y0) {
this(x0, y0, 20);
}
void fall() {
y++;
if (y >= height) {
y = 0;
x = random(width);
}
}
void show() {
push();
noStroke();
translate(x, y);
stroke(#ffffff);
strokeWeight(1);
for (int i = 0; i < 6; i++) {
prong(0.5 * phi);
rotate(radians(60));
}
pop();
}
}
class Star {
float x, y, r;
color col;
int count = int(random(200));
Star(float x0, float y0, float r0, color col0) {
x = x0; y = y0; r = r0; col = col0;
}
Star(float x0, float y0, float r0) {
this(x0, y0, r0, #ffff00);
}
Star(float x0, float y0, color col0) {
this(x0, y0, 20, col0);
}
Star(float x0, float y0) {
this(x0, y0, 20, #ffff00);
}
void show() {
count++;
if (count % 180 < 10) {
return;
}
noStroke();
fill(col);
pushMatrix();
translate(x, y);
beginShape();
for (int i = 0; i < 5; i++) {
vertex(r * cos(radians(144 * i - 90)),
r * sin(radians(144 * i - 90)));
}
endShape();
popMatrix();
}
}
// 7つ叉の枝
void prong(float len) {
pushMatrix();
line(0, 0, 0, len/4);
translate(0, len/4);
line(0, 0, 0.3 * len * cos( PI/6), 0.3 * len * sin(PI/6));
line(0, 0, 0.3 * len * cos(5*PI/6), 0.3 * len * sin(5*PI/6));
line(0, 0, 0, len/4);
translate(0, len/4);
line(0, 0, 0.3 * len * cos( PI/6), 0.3 * len * sin(PI/6));
line(0, 0, 0.3 * len * cos(5*PI/6), 0.3 * len * sin(5*PI/6));
line(0, 0, 0, len/4);
translate(0, len/4);
line(0, 0, 0.3 * len * cos( PI/6), 0.3 * len * sin(PI/6));
line(0, 0, 0.3 * len * cos(5*PI/6), 0.3 * len * sin(5*PI/6));
line(0, 0, 0, len/4);
popMatrix();
}
void branch(float len, int weight, int nleaves) {
pushMatrix();
stroke(trunkcolor);
strokeWeight(weight);
line(0, 0, 0, len);
// leaves
stroke(leafcolor);
strokeWeight(3);
for (int i = 0; i < nleaves; i++) {
translate(0, len / (nleaves + 1));
pushMatrix();
rotate(PI/3);
translate(0, 0.6 * weight);
prong(30);
popMatrix();
pushMatrix();
rotate(-PI/3);
translate(0, 0.6 * weight);
prong(30);
popMatrix();
}
popMatrix();
}
// 木
void tree(float len) {
push();
fill(#663300);
triangle(0, len,
0.1 * len, 0,
-0.1 * len, 0);
fill(#008800);
triangle(0, len,
0.4 * len, 0.2 * len,
-0.4 * len, 0.2 * len);
pop();
}
Crystal[] crystals = new Crystal[100];
Star[] stars = new Star[10];
boolean stop = false;
void setup() {
size(500, 500);
for (int i = 0; i < crystals.length; i++) {
crystals[i] = new Crystal(random(width), random(height));
}
for (int i = 0; i < stars.length; i++) {
stars[i] = new Star(random(width), 0.5 * random(height));
}
}
void draw() {
background(#0000aa);
for (Star s: stars) {
s.show();
}
pushMatrix();
translate(width/2, height);
scale(1, -1);
tree(400);
popMatrix();
for (Crystal c: crystals) {
c.fall(); c.show();
}
}
void mouseClicked() {
stop = ! stop;
if (stop) {
noLoop();
} else {
loop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment