sketch/coding_challenge/starfield/starfield.pde

65 lines
1 KiB
Plaintext
Raw Permalink Normal View History

2020-03-25 05:01:04 -07:00
Star[] stars = new Star[400];
void setup() {
size(800,800);
smooth();
stroke(255);
strokeWeight(5);
for (int i = 0; i < stars.length; i++) {
stars[i] = new Star();
}
}
void draw() {
background(0);
translate(width/2, height/2);
for (int i = 0; i < stars.length; i++) {
stars[i].update();
stars[i].show();
}
}
class Star {
float x;
float y;
float z;
float pz;
Star() {
x = random(-width, width);
y = random(-height, height);
z = random(0, 2000);
pz = z;
}
void update() {
pz = z + 100;
z = z - 20;
if (z<1) {
z = 2000;
y = random(-height, height);
x = random(-width, width);
pz = z;
}
}
void show() {
fill(255);
noStroke();
float sx = map(x/z, 0, 1, 0, width);
float sy = map(y/z, 0, 1, 0, width);
float r = map(z-1000, 0, width, 16, 0);
ellipse(sx, sy, r, r);
float px = map(x/pz, 0, 1, 0, width);
float py = map(y/pz, 0, 1, 0, height);
stroke(255);
line(px, py, sx, sy);
}
}