From d1e3a60d5e232efdca7791a806c2917bf6b426fd Mon Sep 17 00:00:00 2001 From: Yong Bakos Date: Fri, 5 Apr 2013 12:10:50 -0600 Subject: [PATCH 1/2] Deprecating Random.nextGaussian() in lieu of randomGaussian. This change brings the Android example in-line with the Java example. References processing/processing_web#45 --- .../Simulate/SmokeParticleSystem/Particle.pde | 50 +++++------- .../SmokeParticleSystem/ParticleSystem.pde | 47 ++++------- .../SmokeParticleSystem.pde | 79 +++++++------------ 3 files changed, 66 insertions(+), 110 deletions(-) diff --git a/android/examples/Topics/Simulate/SmokeParticleSystem/Particle.pde b/android/examples/Topics/Simulate/SmokeParticleSystem/Particle.pde index 5e9ba9b5f..5ef219f68 100644 --- a/android/examples/Topics/Simulate/SmokeParticleSystem/Particle.pde +++ b/android/examples/Topics/Simulate/SmokeParticleSystem/Particle.pde @@ -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; } } } - - - - diff --git a/android/examples/Topics/Simulate/SmokeParticleSystem/ParticleSystem.pde b/android/examples/Topics/Simulate/SmokeParticleSystem/ParticleSystem.pde index a937d2efe..a101200bb 100644 --- a/android/examples/Topics/Simulate/SmokeParticleSystem/ParticleSystem.pde +++ b/android/examples/Topics/Simulate/SmokeParticleSystem/ParticleSystem.pde @@ -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 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(); // 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; - } - } - } - diff --git a/android/examples/Topics/Simulate/SmokeParticleSystem/SmokeParticleSystem.pde b/android/examples/Topics/Simulate/SmokeParticleSystem/SmokeParticleSystem.pde index 2cf672118..32683de91 100644 --- a/android/examples/Topics/Simulate/SmokeParticleSystem/SmokeParticleSystem.pde +++ b/android/examples/Topics/Simulate/SmokeParticleSystem/SmokeParticleSystem.pde @@ -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(); +} From c03f5e133c3cd77850a74ffd8f4293718ed38a5d Mon Sep 17 00:00:00 2001 From: Yong Bakos Date: Fri, 5 Apr 2013 12:40:21 -0600 Subject: [PATCH 2/2] Importing DecimalFormat, so the example runs. References processing/processing_web#45 --- .../Books/Nature of Code/chp10_nn/xor/xor.pde | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/xor.pde b/java/examples/Books/Nature of Code/chp10_nn/xor/xor.pde index f7f5aa7a5..4ffaa45b3 100755 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/xor.pde +++ b/java/examples/Books/Nature of Code/chp10_nn/xor/xor.pde @@ -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);