From 775870e585105e88ab1b8040744588f9a8094c23 Mon Sep 17 00:00:00 2001 From: Yong Bakos Date: Thu, 21 Feb 2013 18:32:53 -0700 Subject: [PATCH 1/5] Fixing syntax errors so the demo can run. --- .../MultipleParticleSystems/CrazyParticle.pde | 15 +++++---------- .../MultipleParticleSystems/ParticleSystem.pde | 6 ++++-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde index 02b2dfa97..fb4b7227d 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde @@ -21,25 +21,20 @@ class CrazyParticle extends Particle { void update() { super.update(); // Increment rotation based on horizontal velocity - float theta_vel = (vel.x * vel.mag()) / 10.0f; + float theta_vel = (velocity.x * velocity.mag()) / 10.0f; theta += theta_vel; } - // Override timer - void timer() { - timer -= 0.5; - } - // Method to display - void render() { + void display() { // Render the ellipse just like in a regular particle - super.render(); + super.display(); // Then add a rotating line pushMatrix(); - translate(loc.x,loc.y); + translate(location.x,location.y); rotate(theta); - stroke(255,timer); + stroke(255,lifespan); line(0,0,25,0); popMatrix(); } diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde index 1088f5c85..d6a3bed0a 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde @@ -1,4 +1,6 @@ -// An ArrayList is used to manage the list of Particles +// An ArrayList is used to manage the list of Particles +// An Iterator is used to remove "dead" particles while iterating over the list +import java.util.Iterator; class ParticleSystem { @@ -42,4 +44,4 @@ class ParticleSystem { } } -} +} \ No newline at end of file From 9ad642810ca589e5b8367df5dd2ea979993aed57 Mon Sep 17 00:00:00 2001 From: Yong Bakos Date: Thu, 21 Feb 2013 18:48:40 -0700 Subject: [PATCH 2/5] Cleaning up whitespace. --- .../MultipleParticleSystems.pde | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde index 9ab20cdef..5acfc96e6 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/MultipleParticleSystems.pde @@ -1,13 +1,13 @@ /** * Multiple Particle Systems - * by Daniel Shiffman. - * + * by Daniel Shiffman. + * * Click the mouse to generate a burst of particles - * at mouse location. - * + * at mouse location. + * * Each burst is one instance of a particle system * with Particles and CrazyParticles (a subclass of Particle) - * Note use of Inheritance and Polymorphism here. + * Note use of Inheritance and Polymorphism here. */ ArrayList systems; @@ -24,7 +24,6 @@ void draw() { ps.run(); ps.addParticle(); } - if (systems.isEmpty()) { fill(255); textAlign(CENTER); @@ -35,13 +34,3 @@ void draw() { void mousePressed() { systems.add(new ParticleSystem(1, new PVector(mouseX, mouseY))); } - - - - - - - - - - From eff645ec3d5f96a1adc66ce4c18c5e227a139b40 Mon Sep 17 00:00:00 2001 From: Yong Bakos Date: Thu, 21 Feb 2013 18:54:33 -0700 Subject: [PATCH 3/5] Adding comment for display, emphasizing inheritance. --- .../Simulate/MultipleParticleSystems/CrazyParticle.pde | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde index fb4b7227d..9c77dc6e3 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/CrazyParticle.pde @@ -12,7 +12,6 @@ class CrazyParticle extends Particle { super(l); // One more line of code to deal with the new variable, theta theta = 0.0; - } // Notice we don't have the method run() here; it is inherited from Particle @@ -25,11 +24,10 @@ class CrazyParticle extends Particle { theta += theta_vel; } - // Method to display + // This display() method overrides the parent class display() method void display() { // Render the ellipse just like in a regular particle super.display(); - // Then add a rotating line pushMatrix(); translate(location.x,location.y); @@ -38,5 +36,5 @@ class CrazyParticle extends Particle { line(0,0,25,0); popMatrix(); } -} +} From 80d417e4b2f182bd41317dce57d924e3dfd19295 Mon Sep 17 00:00:00 2001 From: Yong Bakos Date: Thu, 21 Feb 2013 18:55:09 -0700 Subject: [PATCH 4/5] Refactoring unnecessary if statement. --- .../Simulate/MultipleParticleSystems/Particle.pde | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde index b519208ca..078ef23c5 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/Particle.pde @@ -31,14 +31,10 @@ class Particle { fill(255,lifespan); ellipse(location.x,location.y,8,8); } - + // Is the particle still useful? boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } + return (lifespan < 0.0); } -} +} From 0d6af3584213c76da4b7456035d021df830e520c Mon Sep 17 00:00:00 2001 From: Yong Bakos Date: Thu, 21 Feb 2013 18:55:53 -0700 Subject: [PATCH 5/5] Incorporating random CrazyParticle addition and refactoring logic in dead. --- .../MultipleParticleSystems/ParticleSystem.pde | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde index d6a3bed0a..d387e8444 100644 --- a/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde +++ b/java/examples/Topics/Simulate/MultipleParticleSystems/ParticleSystem.pde @@ -16,7 +16,7 @@ class ParticleSystem { } void run() { - // Cycle through the ArrayList using Iterator we are deleting + // Cycle through the ArrayList using Iterator, because we are deleting while iterating Iterator it = particles.iterator(); while (it.hasNext()) { Particle p = it.next(); @@ -28,7 +28,14 @@ class ParticleSystem { } void addParticle() { - particles.add(new Particle(origin)); + Particle p; + // Add either a Particle or CrazyParticle to the system + if (int(random(0, 2)) == 0) { + p = new Particle(origin); + } else { + p = new CrazyParticle(origin); + } + particles.add(p); } void addParticle(Particle p) { @@ -37,11 +44,7 @@ class ParticleSystem { // A method to test if the particle system still has particles boolean dead() { - if (particles.isEmpty()) { - return true; - } else { - return false; - } + return particles.isEmpty(); } } \ No newline at end of file