Merge branch 'master' of github.com:processing/processing

This commit is contained in:
Daniel Shiffman
2013-04-07 22:03:09 -04:00
4 changed files with 78 additions and 121 deletions
@@ -5,26 +5,16 @@ class Particle {
PVector loc;
PVector vel;
PVector acc;
float timer;
float lifespan;
PImage img;
// One constructor
Particle(PVector a, PVector v, PVector l, PImage img_) {
acc = a.get();
vel = v.get();
loc = l.get();
timer = 100.0;
img = img_;
}
// Another constructor (the one we are using here)
Particle(PVector l,PImage img_) {
acc = new PVector(0.0,0.0,0.0);
float x = (float) generator.nextGaussian()*0.3f;
float y = (float) generator.nextGaussian()*0.3f - 1.0f;
vel = new PVector(x,y,0);
acc = new PVector(0,0);
float vx = randomGaussian()*0.3;
float vy = randomGaussian()*0.3 - 1.0;
vel = new PVector(vx,vy);
loc = l.get();
timer = 100.0;
lifespan = 100.0;
img = img_;
}
@@ -32,38 +22,38 @@ class Particle {
update();
render();
}
// Method to apply a force vector to the Particle object
// Note we are ignoring "mass" here
void add_force(PVector f) {
void applyForce(PVector f) {
acc.add(f);
}
}
// Method to update location
void update() {
vel.add(acc);
loc.add(vel);
timer -= 2.5;
acc.mult(0);
lifespan -= 2.5;
acc.mult(0); // clear Acceleration
}
// Method to display
void render() {
imageMode(CORNER);
tint(255,timer);
image(img,loc.x-img.width/2,loc.y-img.height/2);
imageMode(CENTER);
tint(255,lifespan);
image(img,loc.x,loc.y);
// Drawing a circle instead
// fill(255,lifespan);
// noStroke();
// ellipse(loc.x,loc.y,img.width,img.height);
}
// Is the particle still useful?
boolean dead() {
if (timer <= 0.0) {
boolean isDead() {
if (lifespan <= 0.0) {
return true;
} else {
return false;
}
}
}
@@ -1,57 +1,42 @@
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
PImage img;
ParticleSystem(int num, PVector v, PImage img_) {
particles = new ArrayList(); // Initialize the arraylist
origin = v.get(); // Store the origin point
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.get(); // Store the origin point
img = img_;
for (int i = 0; i < num; i++) {
particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist
particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist
}
}
void run() {
// Cycle through the ArrayList backwards b/c we are deleting
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
Particle p = particles.get(i);
p.run();
if (p.dead()) {
if (p.isDead()) {
particles.remove(i);
}
}
}
// Method to add a force vector to all particles currently in the system
void add_force(PVector dir) {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
p.add_force(dir);
void applyForce(PVector dir) {
// Enhanced loop!!!
for (Particle p : particles) {
p.applyForce(dir);
}
}
}
void addParticle() {
particles.add(new Particle(origin,img));
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
if (particles.isEmpty()) {
return true;
} else {
return false;
}
}
}
@@ -2,68 +2,49 @@
* Smoke Particle System
* by Daniel Shiffman.
*
* A basic smoke effect using a particle system.
* Each particle is rendered as an alpha masked image.
* A basic smoke effect using a particle system. Each particle
* is rendered as an alpha masked image.
*/
ParticleSystem ps;
Random generator;
void setup() {
size(640, 200);
colorMode(RGB, 255, 255, 255, 100);
// Using a Java random number generator for Gaussian random numbers
generator = new Random();
// Create an alpha masked image to be applied as the particle's texture
PImage msk = loadImage("texture.gif");
PImage img = new PImage(msk.width,msk.height);
for (int i = 0; i < img.pixels.length; i++) img.pixels[i] = color(255);
img.mask(msk);
ps = new ParticleSystem(0, new PVector(width/2,height-20 ),img);
smooth();
size(640,360);
PImage img = loadImage("texture.png");
ps = new ParticleSystem(0,new PVector(width/2,height-60),img);
}
void draw() {
background(75);
background(0);
// Calculate a "wind" force based on mouse horizontal position
float dx = (mouseX - width/2) / 1000.0;
PVector wind = new PVector(dx,0,0);
displayVector(wind,width/2,50,500);
ps.add_force(wind);
float dx = map(mouseX,0,width,-0.2,0.2);
PVector wind = new PVector(dx,0);
ps.applyForce(wind);
ps.run();
for (int i = 0; i < 2; i++) {
ps.addParticle();
}
// Draw an arrow representing the wind force
drawVector(wind, new PVector(width/2,50,0),500);
}
void displayVector(PVector v, float x, float y, float scayl) {
pushMatrix();
float arrowsize = 4;
// Translate to location to render vector
translate(x,y);
stroke(255);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0,0,len,0);
line(len,0,len-arrowsize,+arrowsize/2);
line(len,0,len-arrowsize,-arrowsize/2);
popMatrix();
}
// Renders a vector object 'v' as an arrow and a location 'loc'
void drawVector(PVector v, PVector loc, float scayl) {
pushMatrix();
float arrowsize = 4;
// Translate to location to render vector
translate(loc.x,loc.y);
stroke(255);
// Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
rotate(v.heading());
// Calculate length of vector & scale it to be bigger or smaller if necessary
float len = v.mag()*scayl;
// Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
line(0,0,len,0);
line(len,0,len-arrowsize,+arrowsize/2);
line(len,0,len-arrowsize,-arrowsize/2);
popMatrix();
}
@@ -6,6 +6,7 @@
// Neural network code is all in the "code" folder
import nn.*;
import java.text.DecimalFormat;
ArrayList inputs; // List of training input values
Network nn; // Neural Network Object
@@ -29,17 +30,17 @@ void setup() {
// Create a list of 4 training inputs
inputs = new ArrayList();
float[] input = new float[2];
input[0] = 1;
input[1] = 0;
input[0] = 1;
input[1] = 0;
inputs.add((float []) input.clone());
input[0] = 0;
input[1] = 1;
input[0] = 0;
input[1] = 1;
inputs.add((float []) input.clone());
input[0] = 1;
input[1] = 1;
input[0] = 1;
input[1] = 1;
inputs.add((float []) input.clone());
input[0] = 0;
input[1] = 0;
input[0] = 0;
input[1] = 0;
inputs.add((float []) input.clone());
}
@@ -51,7 +52,7 @@ void draw() {
// Pick a random training input
int pick = int(random(inputs.size()));
// Grab that input
float[] inp = (float[]) inputs.get(pick);
float[] inp = (float[]) inputs.get(pick);
// Compute XOR
float known = 1;
if ((inp[0] == 1.0 && inp[1] == 1.0) || (inp[0] == 0 && inp[1] == 0)) known = 0;
@@ -77,7 +78,7 @@ void draw() {
// Draw the landscape
popMatrix();
land.calculate(nn);
land.render();
land.render();
theta += 0.0025;
popMatrix();
@@ -96,7 +97,7 @@ void networkStatus() {
text("Total iterations: " + count,10,40);
for (int i = 0; i < inputs.size(); i++) {
float[] inp = (float[]) inputs.get(i);
float[] inp = (float[]) inputs.get(i);
float known = 1;
if ((inp[0] == 1.0 && inp[1] == 1.0) || (inp[0] == 0 && inp[1] == 0)) known = 0;
float result = nn.feedForward(inp);