Blame onefile/stars.c

261920
261920
#include <math.h></math.h>
261920
#include <string.h></string.h>
261920
#include <helianthus.h></helianthus.h>
261920
261920
261920
#define MAXCOUNT 10000
261920
#define STEP     1.001
261920
#define SPEED    2000
261920
261920
261920
typedef struct Star {
261920
  double cx, cy;
261920
  double dx, dy;
261920
  double r;
261920
  int visible;
261920
} Star;
261920
261920
Star stars[MAXCOUNT];
261920
double step;
261920
261920
261920
void updateStar(Star *s, double k, double w, double h) {
261920
  s->dx *= k;
261920
  s->dy *= k;
261920
  s->r *= k;
261920
  s->visible = fabs(s->dx + s->cx) + s->r < w/2
261920
            && fabs(s->dy + s->cy) + s->r < h/2;
261920
}
261920
261920
261920
void update(double dt, double mx, double my, double w, double h) {
261920
  step += SPEED*dt;
261920
  while(step > 1) {
261920
    int found = 0;
261920
    for(int i = 0; i < MAXCOUNT; ++i) {
261920
      if (stars[i].visible) updateStar(&stars[i], STEP, w, h);
261920
261920
      if (!found && !stars[i].visible) {
261920
        stars[i].cx = mx;
261920
        stars[i].cy = my;
261920
        stars[i].dx = (randomFloat() - 0.5)*w - mx;
261920
        stars[i].dy = (randomFloat() - 0.5)*h - my;
261920
        stars[i].r = 0.25*pow(STEP, randomFloat());
261920
        stars[i].visible = TRUE;
261920
        found = 1;
261920
      }
261920
    }
261920
    step -= 1;
261920
  }
261920
261920
  double k = pow(STEP, step);
261920
  for(int i = 0; i < MAXCOUNT; ++i)
261920
    if (stars[i].visible) updateStar(&stars[i], k, w, h);
261920
}
261920
261920
261920
void init() {
261920
  background(COLOR_BLACK);
261920
}
261920
261920
261920
void draw() {
261920
  double w = windowGetWidth();
261920
  double h = windowGetHeight();
261920
  saveState();
261920
  translate(w/2, h/2);
261920
261920
  update(windowGetFrameTime(), mouseTransformedX(), mouseTransformedY(), w, h);
261920
261920
  noStroke();
261920
  fill(COLOR_WHITE);
261920
  for(int i = 0; i < MAXCOUNT; ++i)
261920
    if (stars[i].visible)
261920
      circle(stars[i].cx + stars[i].dx, stars[i].cy + stars[i].dy, stars[i].r);
261920
261920
  restoreState();
261920
}
261920
261920
261920
int main() {
261920
  windowSetResizable(TRUE);
261920
  windowSetVariableFrameRate();
261920
  windowSetInit(&init);
261920
  windowSetDraw(&draw);
261920
  windowRun();
261920
}