Skip to content

Commit 331e465

Browse files
committed
updating nature of code examples for noc repo
1 parent e893237 commit 331e465

65 files changed

Lines changed: 1540 additions & 179 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Exercise_10_5_LayeredNetworkAnimation.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Network network;
88

99
void setup() {
10-
size(750,200);
10+
size(640,360);
1111
// Create the Network object
1212
network = new Network(width/2, height/2);
1313

@@ -17,7 +17,7 @@ void setup() {
1717
Neuron output = new Neuron(250, 0);
1818
for (int i = 0; i < layers; i++) {
1919
for (int j = 0; j < inputs; j++) {
20-
float x = map(i, 0, layers, -300, 300);
20+
float x = map(i, 0, layers, -250, 300);
2121
float y = map(j, 0, inputs-1, -75, 75);
2222
Neuron n = new Neuron(x, y);
2323
if (i > 0) {

java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/NOC_10_01_SimplePerceptron.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ float f(float x) {
2727
}
2828

2929
void setup() {
30-
size(800, 200);
30+
size(640, 360);
3131

3232
// The perceptron has 3 inputs -- x, y, and bias
3333
// Second value is "Learning Constant"

java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/NOC_10_02_SeekingNeural.pde

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ PVector desired;
1111
ArrayList<PVector> targets;
1212

1313
void setup() {
14-
size(800, 200);
14+
size(640, 360);
1515
// The Vehicle's desired location
1616
desired = new PVector(width/2,height/2);
1717

@@ -35,19 +35,20 @@ void makeTargets() {
3535
void draw() {
3636
background(255);
3737

38-
// Draw a rectangle to show the Vehicle's goal
39-
rectMode(CENTER);
38+
// Draw a circle to show the Vehicle's goal
4039
stroke(0);
4140
strokeWeight(2);
4241
fill(0, 100);
43-
rect(desired.x, desired.y, 36, 36);
42+
ellipse(desired.x, desired.y, 36, 36);
4443

4544
// Draw the targets
4645
for (PVector target : targets) {
47-
fill(0, 100);
46+
noFill();
4847
stroke(0);
4948
strokeWeight(2);
50-
ellipse(target.x, target.y, 30, 30);
49+
ellipse(target.x, target.y, 16, 16);
50+
line(target.x,target.y-16,target.x,target.y+16);
51+
line(target.x-16,target.y,target.x+16,target.y);
5152
}
5253

5354
// Update the Vehicle

java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/NOC_10_03_NetworkViz.pde

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
Network network;
88

99
void setup() {
10-
size(800, 200);
10+
size(640, 360);
1111
// Create the Network object
1212
network = new Network(width/2,height/2);
1313

1414
// Create a bunch of Neurons
15-
Neuron a = new Neuron(-300,0);
15+
Neuron a = new Neuron(-200,0);
1616
Neuron b = new Neuron(0,75);
1717
Neuron c = new Neuron(0,-75);
18-
Neuron d = new Neuron(300,0);
18+
Neuron d = new Neuron(200,0);
1919

2020
// Connect them
2121
network.connect(a,b);

java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/NOC_10_04_NetworkAnimation.pde

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
Network network;
88

99
void setup() {
10-
size(800, 200);
10+
size(640, 360);
1111
// Create the Network object
1212
network = new Network(width/2, height/2);
1313

1414
// Create a bunch of Neurons
15-
Neuron a = new Neuron(-350, 0);
16-
Neuron b = new Neuron(-200, 0);
15+
Neuron a = new Neuron(-275, 0);
16+
Neuron b = new Neuron(-150, 0);
1717
Neuron c = new Neuron(0, 75);
1818
Neuron d = new Neuron(0, -75);
19-
Neuron e = new Neuron(200, 0);
20-
Neuron f = new Neuron(350, 0);
19+
Neuron e = new Neuron(150, 0);
20+
Neuron f = new Neuron(275, 0);
2121

2222
// Connect them
2323
network.connect(a, b,1);

java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/Mover.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Mover {
1111

1212
Mover(float m, float x, float y) {
1313
mass = m;
14-
location = new PVector(random(width), random(height));
14+
location = new PVector(x, y);
1515
velocity = new PVector(1, 0);
1616
acceleration = new PVector(0, 0);
1717
}

java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/ParticleSystem.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class ParticleSystem {
3030
}
3131
}
3232

33-
void addParticle(Particle p) {
34-
particles.add(p);
33+
void addParticle() {
34+
particles.add(new Particle(origin));
3535
}
3636

3737
// A method to test if the particle system still has particles

java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/NOC_5_3_ChainShape_Simple.pde

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import pbox2d.*;
99
import org.jbox2d.collision.shapes.*;
1010
import org.jbox2d.common.*;
1111
import org.jbox2d.dynamics.*;
12-
import org.jbox2d.dynamics.*;
1312

1413
// A reference to our box2d world
1514
PBox2D box2d;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// The Nature of Code
2+
// Daniel Shiffman
3+
// http://natureofcode.com
4+
5+
// Separation
6+
// Via Reynolds: http://www.red3d.com/cwr/steer/
7+
8+
// A list of vehicles
9+
ArrayList<Vehicle> vehicles;
10+
11+
void setup() {
12+
size(640,360);
13+
// We are now making random vehicles and storing them in an ArrayList
14+
vehicles = new ArrayList<Vehicle>();
15+
for (int i = 0; i < 100; i++) {
16+
vehicles.add(new Vehicle(random(width),random(height)));
17+
}
18+
}
19+
20+
void draw() {
21+
background(255);
22+
23+
for (Vehicle v : vehicles) {
24+
// Path following and separation are worked on in this function
25+
v.align(vehicles);
26+
// Call the generic run method (update, borders, display, etc.)
27+
v.update();
28+
v.borders();
29+
v.display();
30+
}
31+
32+
// Instructions
33+
fill(0);
34+
text("Drag the mouse to generate new vehicles.",10,height-16);
35+
}
36+
37+
38+
void mouseDragged() {
39+
vehicles.add(new Vehicle(mouseX,mouseY));
40+
}
41+
42+
43+
44+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// The Nature of Code
2+
// Daniel Shiffman
3+
// http://natureofcode.com
4+
5+
// Vehicle class
6+
7+
class Vehicle {
8+
9+
// All the usual stuff
10+
PVector location;
11+
PVector velocity;
12+
PVector acceleration;
13+
float r;
14+
float maxforce; // Maximum steering force
15+
float maxspeed; // Maximum speed
16+
17+
// Constructor initialize all values
18+
Vehicle(float x, float y) {
19+
location = new PVector(x, y);
20+
r = 12;
21+
maxspeed = 3;
22+
maxforce = 0.2;
23+
acceleration = new PVector(0, 0);
24+
velocity = PVector.random2D();
25+
velocity.mult(random(1,4));
26+
}
27+
28+
void applyForce(PVector force) {
29+
// We could add mass here if we want A = F / M
30+
acceleration.add(force);
31+
}
32+
33+
// Alignment
34+
// For every nearby boid in the system, calculate the average velocity
35+
void align (ArrayList<Vehicle> boids) {
36+
float neighbordist = 30;
37+
PVector sum = new PVector(0, 0);
38+
int count = 0;
39+
for (Vehicle other : vehicles) {
40+
float d = PVector.dist(location, other.location);
41+
if ((d > 0) && (d < neighbordist)) {
42+
sum.add(other.velocity);
43+
count++;
44+
}
45+
}
46+
if (count > 0) {
47+
sum.div((float)count);
48+
sum.normalize();
49+
sum.mult(maxspeed);
50+
PVector steer = PVector.sub(sum, velocity);
51+
steer.limit(maxforce);
52+
applyForce(steer);
53+
}
54+
}
55+
56+
57+
// Method to update location
58+
void update() {
59+
// Update velocity
60+
velocity.add(acceleration);
61+
// Limit speed
62+
velocity.limit(maxspeed);
63+
location.add(velocity);
64+
// Reset accelertion to 0 each cycle
65+
acceleration.mult(0);
66+
}
67+
68+
void display() {
69+
fill(175);
70+
stroke(0);
71+
pushMatrix();
72+
translate(location.x, location.y);
73+
ellipse(0, 0, r, r);
74+
popMatrix();
75+
}
76+
77+
// Wraparound
78+
void borders() {
79+
if (location.x < -r) location.x = width+r;
80+
if (location.y < -r) location.y = height+r;
81+
if (location.x > width+r) location.x = -r;
82+
if (location.y > height+r) location.y = -r;
83+
}
84+
}
85+
86+
87+
88+
89+

0 commit comments

Comments
 (0)