xxxxxxxxxx
// A triangle particle system.
// Press and/or drag mouse to generate triangles. Press any key to clear.
// particles, particle system, boolean, PVector, ArrayList, mouseX, mouseY, random, vertex, monochrome.
particleGenerator pg;
boolean makeParticles;
void setup() {
size(900, 550);
//fullscreen();
background(240);
pg = new particleGenerator();
}
void draw() {
pg.run();
}
void mousePressed() {
makeParticles = true;
}
void mouseReleased() {
makeParticles = false;
}
// %%%%%% Classes %%%%%%%%
class particleGenerator {
ArrayList<Particle> particles;
PVector pos;
particleGenerator() {
pos = new PVector();
particles = new ArrayList<Particle>();
}
void run() {
if (makeParticles) {
pos.set(mouseX, mouseY);
particles.add(new Particle(pos));
}
for (int i = particles.size ()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
class Particle {
PVector pos;
PVector vel;
float sze, mx, my, maxSze;
int angle, incr;
Particle(PVector p) {
vel = new PVector(random(-2, 2), random(-2, 2));
vel.mult(5);
pos = p.get();
sze = 1;
angle = 0;
maxSze = random(5, 50);
incr = 5;
if (random(1) < 0.5) incr = -5;
mx = random(0.3, 0.5);
if (random(1) < 0.5) mx = -mx;
my = random(0.3, 0.5);
if (random(1) < 0.5) my = -my;
}
void run() {
move();
display();
}
void move() {
vel.x += mx;
vel.y += my;
pos.add(vel);
sze += random(0.5, 1.5);
angle += incr;
}
void display() {
stroke(0);
strokeWeight(0.5);
//call to HTML5 canvas API
externals.context.shadowOffsetX = 5;
externals.context.shadowOffsetY = 5;
externals.context.shadowBlur = 15;
externals.context.shadowColor = "black";
drawTri(pos.x, pos.y, sze, angle);
}
boolean isDead() {
if (sze >= maxSze) {
return true;
} else {
return false;
}
}
void drawTri(float xIn, float yIn, float sz, int ang) {
float x, y;
beginShape();
for (int a = ang; a < 360 + ang; a += 360/3) {
x = xIn + cos(radians(a)) * sz;
y = yIn + sin(radians(a)) * sz;
vertex(x, y);
}
endShape(CLOSE);
}
}
void keyPressed() {
background(240);
}