FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge branch 'master' of github.com:processing/processing · perajim/processing@bd7e822 · GitHub

Repository navigation

Commit bd7e822

Browse files
committed
Merge branch 'master' of github.com:processing/processing
2 parents c068a71 + 0733b91 commit bd7e822

5 files changed

Lines changed: 77 additions & 120 deletions

File tree

‎android/examples/Topics/Simulate/SmokeParticleSystem/Particle.pde‎

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,55 @@ class Particle {
55
PVector loc;
66
PVector vel;
77
PVector acc;
8-
float timer;
8+
float lifespan;
99
PImage img;
1010

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)
2111
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);
2616
loc = l.get();
27-
timer = 100.0;
17+
lifespan = 100.0;
2818
img = img_;
2919
}
3020

3121
void run() {
3222
update();
3323
render();
3424
}
35-
25+
3626
// Method to apply a force vector to the Particle object
3727
// Note we are ignoring "mass" here
38-
void add_force(PVector f) {
28+
void applyForce(PVector f) {
3929
acc.add(f);
40-
}
30+
}
4131

4232
// Method to update location
4333
void update() {
4434
vel.add(acc);
4535
loc.add(vel);
46-
timer -= 2.5;
47-
acc.mult(0);
36+
lifespan -= 2.5;
37+
acc.mult(0); // clear Acceleration
4838
}
4939

5040
// Method to display
5141
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);
5549
}
5650

5751
// Is the particle still useful?
58-
boolean dead() {
59-
if (timer <= 0.0) {
52+
boolean isDead() {
53+
if (lifespan <= 0.0) {
6054
return true;
6155
} else {
6256
return false;
6357
}
6458
}
6559
}
66-
67-
68-
69-
Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,42 @@
11
// 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
33

44
class ParticleSystem {
55

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
88
PImage img;
9-
9+
1010
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
1313
img = img_;
1414
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
1616
}
1717
}
1818

1919
void run() {
20-
// Cycle through the ArrayList backwards b/c we are deleting
2120
for (int i = particles.size()-1; i >= 0; i--) {
22-
Particle p = (Particle) particles.get(i);
21+
Particle p = particles.get(i);
2322
p.run();
24-
if (p.dead()) {
23+
if (p.isDead()) {
2524
particles.remove(i);
2625
}
2726
}
2827
}
29-
28+
3029
// 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);
3534
}
36-
37-
}
3835

39-
void addParticle() {
40-
particles.add(new Particle(origin,img));
4136
}
4237

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));
5440
}
5541

5642
}
57-

‎android/examples/Topics/Simulate/SmokeParticleSystem/SmokeParticleSystem.pde‎

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,49 @@
22
* Smoke Particle System
33
* by Daniel Shiffman.
44
*
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.
77
*/
88

99
ParticleSystem ps;
10-
Random generator;
1110

1211
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);
2815
}
2916

3017
void draw() {
31-
background(75);
18+
background(0);
3219

3320
// 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);
3824
ps.run();
3925
for (int i = 0; i < 2; i++) {
4026
ps.addParticle();
4127
}
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-
6728

29+
// Draw an arrow representing the wind force
30+
drawVector(wind, new PVector(width/2,50,0),500);
6831

32+
}
6933

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+
}
3.47 KB
LoadingViewer requires iframe.

‎java/examples/Books/Nature of Code/chp10_nn/xor/xor.pde‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Neural network code is all in the "code" folder
77

88
import nn.*;
9+
import java.text.DecimalFormat;
910

1011
ArrayList inputs; // List of training input values
1112
Network nn; // Neural Network Object
@@ -29,17 +30,17 @@ void setup() {
2930
// Create a list of 4 training inputs
3031
inputs = new ArrayList();
3132
float[] input = new float[2];
32-
input[0] = 1;
33-
input[1] = 0;
33+
input[0] = 1;
34+
input[1] = 0;
3435
inputs.add((float []) input.clone());
35-
input[0] = 0;
36-
input[1] = 1;
36+
input[0] = 0;
37+
input[1] = 1;
3738
inputs.add((float []) input.clone());
38-
input[0] = 1;
39-
input[1] = 1;
39+
input[0] = 1;
40+
input[1] = 1;
4041
inputs.add((float []) input.clone());
41-
input[0] = 0;
42-
input[1] = 0;
42+
input[0] = 0;
43+
input[1] = 0;
4344
inputs.add((float []) input.clone());
4445
}
4546

@@ -51,7 +52,7 @@ void draw() {
5152
// Pick a random training input
5253
int pick = int(random(inputs.size()));
5354
// Grab that input
54-
float[] inp = (float[]) inputs.get(pick);
55+
float[] inp = (float[]) inputs.get(pick);
5556
// Compute XOR
5657
float known = 1;
5758
if ((inp[0] == 1.0 && inp[1] == 1.0) || (inp[0] == 0 && inp[1] == 0)) known = 0;
@@ -77,7 +78,7 @@ void draw() {
7778
// Draw the landscape
7879
popMatrix();
7980
land.calculate(nn);
80-
land.render();
81+
land.render();
8182
theta += 0.0025;
8283
popMatrix();
8384

@@ -96,7 +97,7 @@ void networkStatus() {
9697
text("Total iterations: " + count,10,40);
9798

9899
for (int i = 0; i < inputs.size(); i++) {
99-
float[] inp = (float[]) inputs.get(i);
100+
float[] inp = (float[]) inputs.get(i);
100101
float known = 1;
101102
if ((inp[0] == 1.0 && inp[1] == 1.0) || (inp[0] == 0 && inp[1] == 0)) known = 0;
102103
float result = nn.feedForward(inp);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL