Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active February 25, 2024 11:06
Show Gist options
  • Save kou1okada/fc5850e633f23d80ba8d44056e749dec to your computer and use it in GitHub Desktop.
Save kou1okada/fc5850e633f23d80ba8d44056e749dec to your computer and use it in GitHub Desktop.
Fireworks for Processing
/**
* Fireworks for Processing
* Copyright (c) 2018 Koichi OKADA. All rights reserved;
* This code is distributed under the MIT license.
*/
int MaxLife = 255;
int N = 500;
class Fire {
double x, y, vx, vy, ax, ay;
int life;
public:
Fire() {
init(width / 2, height / 2, 2);
}
Fire(int x, int y, double v) {
init(x, y, v);
}
void init(int _x, int _y, double _v) {
x = _x;
y = _y;
double v = random(_v);
double r = random(2 * 3.14);
vx = v * cos(r);
vy = v * sin(r) - 1;
ax = 0;
ay = 0.01;
life = MaxLife;
}
void step() {
vx += ax;
vy += ay;
x += vx;
y += vy;
life--;
}
void draw() {
int c = 255 - MaxLife + life;
//stroke(random(50), random(255), random(255), 100);
stroke(c, c, c, 100);
//int x = random(width);
//int y = random(height);
line(x, y, x, y);
}
bool is_alive() {
return 0 < life;
}
}
class Fireworks {
ArrayList<Fire> f;
public:
Fireworks() {
f = new ArrayList<Fire>();
}
void fire(int x, int y, double v) {
for (int i = 0; i < N; i++) {
f.add(new Fire(x, y, v));
}
}
void step() {
for (int i = 0; i < f.size(); i++) {
f.get(i).step();
}
}
void draw() {
step();
for (int i = 0; i < f.size(); i++) {
f.get(i).draw();
}
cleanup();
}
void cleanup() {
for (int i = f.size() - 1; 0 <= i; i--) {
if (!f.get(i).is_alive()) {
f.remove(i);
}
}
}
}
Fireworks fw;
void setup() {
background(0);
size(300, 300);
strokeWeight(4);
frameRate(30);
fw = new Fireworks();
fw.fire(width / 2, height / 2, 2);
}
void draw() {
background(0);
fw.draw();
}
void mousePressed() {
fw.fire(mouseX, mouseY, random(0, 4));
}
@kou1okada
Copy link
Author

kou1okada commented Feb 25, 2024

Ported to p5js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment