adding nature of code examples

This commit is contained in:
shiffman
2012-09-04 00:55:42 +00:00
parent 6e10c689e1
commit 6ad7b782d9
581 changed files with 229203 additions and 0 deletions
@@ -0,0 +1,13 @@
ParticleSystem ps;
void setup() {
size(200,200);
smooth();
ps = new ParticleSystem(new PVector(width/2,50));
}
void draw() {
background(255);
ps.addParticle();
ps.run();
}
@@ -0,0 +1,61 @@
// Simple Particle System
// Daniel Shiffman <http://www.shiffman.net>
// A simple Particle class
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255.0;
}
void run() {
update();
push();
display();
pop();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
void push() {
pushMatrix();
}
void pop() {
popMatrix();
}
// Method to display
void display() {
stroke(0,lifespan);
fill(0,lifespan);
translate(location.x,location.y);
ellipse(0,0,8,8);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,20 @@
class ParticleChild extends Particle {
// We could add variables for only Confetti here if we so
ParticleChild(PVector l) {
super(l);
}
// Inherits update() from parent
// Override the display method
void display() {
super.display();
float theta = map(location.x,0,width,0,TWO_PI*2);
rotate(theta);
stroke(0);
line(0,0,50,0);
}
}
@@ -0,0 +1,34 @@
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector location) {
origin = location.get();
particles = new ArrayList<Particle>();
}
void addParticle() {
float r = random(1);
if (r < 0.5) {
particles.add(new Particle(origin));
} else {
particles.add(new ParticleChild(origin));
}
}
void run() {
Iterator<Particle> it = particles.iterator();
while (it.hasNext()) {
Particle p = it.next();
p.run();
if (p.isDead()) {
it.remove();
}
}
}
}