| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,65 +5,55 @@ class Particle { | |||
| 5 | 5 | PVector loc; | |
| 6 | 6 | PVector vel; | |
| 7 | 7 | PVector acc; | |
| 8 | - float timer; | ||
| 8 | + float lifespan; | ||
| 9 | 9 | PImage img; | |
| 10 | 10 | ||
| 11 | - // One constructor | ||
| 12 | - Particle(PVector a, PVector v, PVector l, PImage img_) { | ||
| 13 | - acc = a.get(); | ||
| 14 | - vel = v.get(); | ||
| 15 | - loc = l.get(); | ||
| 16 | - timer = 100.0; | ||
| 17 | - img = img_; | ||
| 18 | - } | ||
| 19 | - | ||
| 20 | - // Another constructor (the one we are using here) | ||
| 21 | 11 | Particle(PVector l,PImage img_) { | |
| 22 | - acc = new PVector(0.0,0.0,0.0); | ||
| 23 | - float x = (float) generator.nextGaussian()*0.3f; | ||
| 24 | - float y = (float) generator.nextGaussian()*0.3f - 1.0f; | ||
| 25 | - vel = new PVector(x,y,0); | ||
| 12 | + acc = new PVector(0,0); | ||
| 13 | + float vx = randomGaussian()*0.3; | ||
| 14 | + float vy = randomGaussian()*0.3 - 1.0; | ||
| 15 | + vel = new PVector(vx,vy); | ||
| 26 | 16 | loc = l.get(); | |
| 27 | - timer = 100.0; | ||
| 17 | + lifespan = 100.0; | ||
| 28 | 18 | img = img_; | |
| 29 | 19 | } | |
| 30 | 20 | ||
| 31 | 21 | void run() { | |
| 32 | 22 | update(); | |
| 33 | 23 | render(); | |
| 34 | 24 | } | |
| 35 | - | ||
| 25 | + | ||
| 36 | 26 | // Method to apply a force vector to the Particle object | |
| 37 | 27 | // Note we are ignoring "mass" here | |
| 38 | - void add_force(PVector f) { | ||
| 28 | + void applyForce(PVector f) { | ||
| 39 | 29 | acc.add(f); | |
| 40 | - } | ||
| 30 | + } | ||
| 41 | 31 | ||
| 42 | 32 | // Method to update location | |
| 43 | 33 | void update() { | |
| 44 | 34 | vel.add(acc); | |
| 45 | 35 | loc.add(vel); | |
| 46 | - timer -= 2.5; | ||
| 47 | - acc.mult(0); | ||
| 36 | + lifespan -= 2.5; | ||
| 37 | + acc.mult(0); // clear Acceleration | ||
| 48 | 38 | } | |
| 49 | 39 | ||
| 50 | 40 | // Method to display | |
| 51 | 41 | void render() { | |
| 52 | - imageMode(CORNER); | ||
| 53 | - tint(255,timer); | ||
| 54 | - image(img,loc.x-img.width/2,loc.y-img.height/2); | ||
| 42 | + imageMode(CENTER); | ||
| 43 | + tint(255,lifespan); | ||
| 44 | + image(img,loc.x,loc.y); | ||
| 45 | + // Drawing a circle instead | ||
| 46 | + // fill(255,lifespan); | ||
| 47 | + // noStroke(); | ||
| 48 | + // ellipse(loc.x,loc.y,img.width,img.height); | ||
| 55 | 49 | } | |
| 56 | 50 | ||
| 57 | 51 | // Is the particle still useful? | |
| 58 | - boolean dead() { | ||
| 59 | - if (timer <= 0.0) { | ||
| 52 | + boolean isDead() { | ||
| 53 | + if (lifespan <= 0.0) { | ||
| 60 | 54 | return true; | |
| 61 | 55 | } else { | |
| 62 | 56 | return false; | |
| 63 | 57 | } | |
| 64 | 58 | } | |
| 65 | 59 | } | |
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,57 +1,42 @@ | |||
| 1 | 1 | // A class to describe a group of Particles | |
| 2 | - // An ArrayList is used to manage the list of Particles | ||
| 2 | + // An ArrayList is used to manage the list of Particles | ||
| 3 | 3 | ||
| 4 | 4 | class ParticleSystem { | |
| 5 | 5 | ||
| 6 | - ArrayList particles; // An arraylist for all the particles | ||
| 7 | - PVector origin; // An origin point for where particles are birthed | ||
| 6 | + ArrayList<Particle> particles; // An arraylist for all the particles | ||
| 7 | + PVector origin; // An origin point for where particles are birthed | ||
| 8 | 8 | PImage img; | |
| 9 | - | ||
| 9 | + | ||
| 10 | 10 | ParticleSystem(int num, PVector v, PImage img_) { | |
| 11 | - particles = new ArrayList(); // Initialize the arraylist | ||
| 12 | - origin = v.get(); // Store the origin point | ||
| 11 | + particles = new ArrayList<Particle>(); // Initialize the arraylist | ||
| 12 | + origin = v.get(); // Store the origin point | ||
| 13 | 13 | img = img_; | |
| 14 | 14 | for (int i = 0; i < num; i++) { | |
| 15 | - particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist | ||
| 15 | + particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist | ||
| 16 | 16 | } | |
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | 19 | void run() { | |
| 20 | - // Cycle through the ArrayList backwards b/c we are deleting | ||
| 21 | 20 | for (int i = particles.size()-1; i >= 0; i--) { | |
| 22 | - Particle p = (Particle) particles.get(i); | ||
| 21 | + Particle p = particles.get(i); | ||
| 23 | 22 | p.run(); | |
| 24 | - if (p.dead()) { | ||
| 23 | + if (p.isDead()) { | ||
| 25 | 24 | particles.remove(i); | |
| 26 | 25 | } | |
| 27 | 26 | } | |
| 28 | 27 | } | |
| 29 | - | ||
| 28 | + | ||
| 30 | 29 | // Method to add a force vector to all particles currently in the system | |
| 31 | - void add_force(PVector dir) { | ||
| 32 | - for (int i = particles.size()-1; i >= 0; i--) { | ||
| 33 | - Particle p = (Particle) particles.get(i); | ||
| 34 | - p.add_force(dir); | ||
| 30 | + void applyForce(PVector dir) { | ||
| 31 | + // Enhanced loop!!! | ||
| 32 | + for (Particle p : particles) { | ||
| 33 | + p.applyForce(dir); | ||
| 35 | 34 | } | |
| 36 | - | ||
| 37 | - } | ||
| 38 | 35 | ||
| 39 | - void addParticle() { | ||
| 40 | - particles.add(new Particle(origin,img)); | ||
| 41 | 36 | } | |
| 42 | 37 | ||
| 43 | - void addParticle(Particle p) { | ||
| 44 | - particles.add(p); | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - // A method to test if the particle system still has particles | ||
| 48 | - boolean dead() { | ||
| 49 | - if (particles.isEmpty()) { | ||
| 50 | - return true; | ||
| 51 | - } else { | ||
| 52 | - return false; | ||
| 53 | - } | ||
| 38 | + void addParticle() { | ||
| 39 | + particles.add(new Particle(origin,img)); | ||
| 54 | 40 | } | |
| 55 | 41 | ||
| 56 | 42 | } | |
| 57 | - | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,68 +2,49 @@ | |||
| 2 | 2 | * Smoke Particle System | |
| 3 | 3 | * by Daniel Shiffman. | |
| 4 | 4 | * | |
| 5 | - * A basic smoke effect using a particle system. | ||
| 6 | - * Each particle is rendered as an alpha masked image. | ||
| 5 | + * A basic smoke effect using a particle system. Each particle | ||
| 6 | + * is rendered as an alpha masked image. | ||
| 7 | 7 | */ | |
| 8 | 8 | ||
| 9 | 9 | ParticleSystem ps; | |
| 10 | - Random generator; | ||
| 11 | 10 | ||
| 12 | 11 | void setup() { | |
| 13 | - | ||
| 14 | - size(640, 200); | ||
| 15 | - colorMode(RGB, 255, 255, 255, 100); | ||
| 16 | - | ||
| 17 | - // Using a Java random number generator for Gaussian random numbers | ||
| 18 | - generator = new Random(); | ||
| 19 | - | ||
| 20 | - // Create an alpha masked image to be applied as the particle's texture | ||
| 21 | - PImage msk = loadImage("texture.gif"); | ||
| 22 | - PImage img = new PImage(msk.width,msk.height); | ||
| 23 | - for (int i = 0; i < img.pixels.length; i++) img.pixels[i] = color(255); | ||
| 24 | - img.mask(msk); | ||
| 25 | - ps = new ParticleSystem(0, new PVector(width/2,height-20 ),img); | ||
| 26 | - | ||
| 27 | - smooth(); | ||
| 12 | + size(640,360); | ||
| 13 | + PImage img = loadImage("texture.png"); | ||
| 14 | + ps = new ParticleSystem(0,new PVector(width/2,height-60),img); | ||
| 28 | 15 | } | |
| 29 | 16 | ||
| 30 | 17 | void draw() { | |
| 31 | - background(75); | ||
| 18 | + background(0); | ||
| 32 | 19 | ||
| 33 | 20 | // Calculate a "wind" force based on mouse horizontal position | |
| 34 | - float dx = (mouseX - width/2) / 1000.0; | ||
| 35 | - PVector wind = new PVector(dx,0,0); | ||
| 36 | - displayVector(wind,width/2,50,500); | ||
| 37 | - ps.add_force(wind); | ||
| 21 | + float dx = map(mouseX,0,width,-0.2,0.2); | ||
| 22 | + PVector wind = new PVector(dx,0); | ||
| 23 | + ps.applyForce(wind); | ||
| 38 | 24 | ps.run(); | |
| 39 | 25 | for (int i = 0; i < 2; i++) { | |
| 40 | 26 | ps.addParticle(); | |
| 41 | 27 | } | |
| 42 | - } | ||
| 43 | - | ||
| 44 | - void displayVector(PVector v, float x, float y, float scayl) { | ||
| 45 | - pushMatrix(); | ||
| 46 | - float arrowsize = 4; | ||
| 47 | - // Translate to location to render vector | ||
| 48 | - translate(x,y); | ||
| 49 | - stroke(255); | ||
| 50 | - // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate | ||
| 51 | - rotate(v.heading()); | ||
| 52 | - // Calculate length of vector & scale it to be bigger or smaller if necessary | ||
| 53 | - float len = v.mag()*scayl; | ||
| 54 | - // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction) | ||
| 55 | - line(0,0,len,0); | ||
| 56 | - line(len,0,len-arrowsize,+arrowsize/2); | ||
| 57 | - line(len,0,len-arrowsize,-arrowsize/2); | ||
| 58 | - popMatrix(); | ||
| 59 | - } | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | 28 | ||
| 29 | + // Draw an arrow representing the wind force | ||
| 30 | + drawVector(wind, new PVector(width/2,50,0),500); | ||
| 68 | 31 | ||
| 32 | + } | ||
| 69 | 33 | ||
| 34 | + // Renders a vector object 'v' as an arrow and a location 'loc' | ||
| 35 | + void drawVector(PVector v, PVector loc, float scayl) { | ||
| 36 | + pushMatrix(); | ||
| 37 | + float arrowsize = 4; | ||
| 38 | + // Translate to location to render vector | ||
| 39 | + translate(loc.x,loc.y); | ||
| 40 | + stroke(255); | ||
| 41 | + // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate | ||
| 42 | + rotate(v.heading()); | ||
| 43 | + // Calculate length of vector & scale it to be bigger or smaller if necessary | ||
| 44 | + float len = v.mag()*scayl; | ||
| 45 | + // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction) | ||
| 46 | + line(0,0,len,0); | ||
| 47 | + line(len,0,len-arrowsize,+arrowsize/2); | ||
| 48 | + line(len,0,len-arrowsize,-arrowsize/2); | ||
| 49 | + popMatrix(); | ||
| 50 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ | |||
| 6 | 6 | // Neural network code is all in the "code" folder | |
| 7 | 7 | ||
| 8 | 8 | import nn.*; | |
| 9 | + import java.text.DecimalFormat; | ||
| 9 | 10 | ||
| 10 | 11 | ArrayList inputs; // List of training input values | |
| 11 | 12 | Network nn; // Neural Network Object | |
@@ -29,17 +30,17 @@ void setup() { | |||
| 29 | 30 | // Create a list of 4 training inputs | |
| 30 | 31 | inputs = new ArrayList(); | |
| 31 | 32 | float[] input = new float[2]; | |
| 32 | - input[0] = 1; | ||
| 33 | - input[1] = 0; | ||
| 33 | + input[0] = 1; | ||
| 34 | + input[1] = 0; | ||
| 34 | 35 | inputs.add((float []) input.clone()); | |
| 35 | - input[0] = 0; | ||
| 36 | - input[1] = 1; | ||
| 36 | + input[0] = 0; | ||
| 37 | + input[1] = 1; | ||
| 37 | 38 | inputs.add((float []) input.clone()); | |
| 38 | - input[0] = 1; | ||
| 39 | - input[1] = 1; | ||
| 39 | + input[0] = 1; | ||
| 40 | + input[1] = 1; | ||
| 40 | 41 | inputs.add((float []) input.clone()); | |
| 41 | - input[0] = 0; | ||
| 42 | - input[1] = 0; | ||
| 42 | + input[0] = 0; | ||
| 43 | + input[1] = 0; | ||
| 43 | 44 | inputs.add((float []) input.clone()); | |
| 44 | 45 | } | |
| 45 | 46 | ||
@@ -51,7 +52,7 @@ void draw() { | |||
| 51 | 52 | // Pick a random training input | |
| 52 | 53 | int pick = int(random(inputs.size())); | |
| 53 | 54 | // Grab that input | |
| 54 | - float[] inp = (float[]) inputs.get(pick); | ||
| 55 | + float[] inp = (float[]) inputs.get(pick); | ||
| 55 | 56 | // Compute XOR | |
| 56 | 57 | float known = 1; | |
| 57 | 58 | if ((inp[0] == 1.0 && inp[1] == 1.0) || (inp[0] == 0 && inp[1] == 0)) known = 0; | |
@@ -77,7 +78,7 @@ void draw() { | |||
| 77 | 78 | // Draw the landscape | |
| 78 | 79 | popMatrix(); | |
| 79 | 80 | land.calculate(nn); | |
| 80 | - land.render(); | ||
| 81 | + land.render(); | ||
| 81 | 82 | theta += 0.0025; | |
| 82 | 83 | popMatrix(); | |
| 83 | 84 | ||
@@ -96,7 +97,7 @@ void networkStatus() { | |||
| 96 | 97 | text("Total iterations: " + count,10,40); | |
| 97 | 98 | ||
| 98 | 99 | for (int i = 0; i < inputs.size(); i++) { | |
| 99 | - float[] inp = (float[]) inputs.get(i); | ||
| 100 | + float[] inp = (float[]) inputs.get(i); | ||
| 100 | 101 | float known = 1; | |
| 101 | 102 | if ((inp[0] == 1.0 && inp[1] == 1.0) || (inp[0] == 0 && inp[1] == 0)) known = 0; | |
| 102 | 103 | float result = nn.feedForward(inp); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments