Skip to content

Instantly share code, notes, and snippets.

@sasaki-shigeo
Last active January 30, 2017 06:26
Show Gist options
  • Save sasaki-shigeo/125d89eb53d7434c8922870c3ae8b841 to your computer and use it in GitHub Desktop.
Save sasaki-shigeo/125d89eb53d7434c8922870c3ae8b841 to your computer and use it in GitHub Desktop.
a tree, stars, snow
float angle = radians(20);
PGraphics aTree;
class Snow {
float x;
float y;
float z;
float sz;
Snow(float x0, float y0, float z0, float sz0) {
x = x0; y = y0; z = z0; sz =sz0;
}
void fall() {
float vx = random(-1, 1);
float vz = random(-1, 1);
y += 1;
x += vx; // z += vz;
}
void relocate(float newX, float newY, float newZ) {
x = newX; y = newY; z = newZ;
}
void draw() {
noStroke();
fill(#ffffff);
pushMatrix();
translate(0, 0, z);
ellipse(x, y, sz, sz);
popMatrix();
}
}
class Star {
float x;
float y;
float z;
float theta;
float r = 10;
color col;
Star(float x0, float y0, float z0, float th0, color c0) {
x = x0;
y = y0;
z = z0;
theta = th0;
col = c0;
}
void rot(float theta1) {
theta += theta1;
}
void draw() {
noStroke();
fill(col);
pushMatrix();
translate(x, y, z);
rotateZ(-HALF_PI);
rotateX(theta);
beginShape();
for (int i = 0; i < 5; i++) {
vertex(r * cos(radians(144 * i)),
r * sin(radians(144 * i)));
}
endShape(CLOSE);
popMatrix();
}
}
Snow[] crystals = new Snow[100];
Star[] stars = new Star[20];
void setup() {
size(500, 500, P3D);
frameRate(30);
for (int i = 0; i < stars.length; i++) {
stars[i] = new Star(random(width), random(height/2), -10, random(PI), #ffff00);
}
for (int i = 0; i < crystals.length; i++) {
crystals[i] = new Snow(random(width), random(height), 0, random(5, 10));
}
aTree = createGraphics(width, height, P2D);
aTree.beginDraw();
// aTree.background(0, 0, 100);
aTree.stroke(100, 50, 0);
aTree.translate(width/2, height);
tree(aTree, 100, 10);
aTree.endDraw();
}
void draw() {
background(0, 0, 100);
for (Star s: stars) {
s.draw();
s.rot(radians(1));
}
image(aTree, 0, 0);
for (Snow s: crystals) {
s.draw();
s.fall();
if (s.y > height) {
s.relocate(random(width), 10, random(1, -100));
}
}
}
void tree(PGraphics g, float len, float thick) {
g.strokeWeight(thick);
g.line(0, 0, 0, -len);
if (len <= 5)
return;
g.pushMatrix();
g.translate(0, -len);
g.rotate(angle);
tree(g, len * random(0.6, 0.8), 0.7 * thick);
g.popMatrix();
g.pushMatrix();
g.translate(0, -len);
g.rotate(-angle);
tree(g, 0.7 * len, 0.7 * thick);
g.popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment