diff --git a/java/examples/Books/Nature of Code/README.md b/java/examples/Books/Nature of Code/README.md deleted file mode 100644 index d008e776b..000000000 --- a/java/examples/Books/Nature of Code/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# The Nature of Code - -![Screenshot](http://www.shiffman.net/images/noc/1.jpg) ![Screenshot](http://www.shiffman.net/images/noc/2.jpg) ![Screenshot](http://www.shiffman.net/images/noc/3.jpg) ![Screenshot](http://www.shiffman.net/images/noc/4.jpg) ![Screenshot](http://www.shiffman.net/images/noc/5.jpg) ![Screenshot](http://www.shiffman.net/images/noc/6.jpg) ![Screenshot](http://www.shiffman.net/images/noc/7.jpg) ![Screenshot](http://www.shiffman.net/images/noc/8.jpg) - -# What is this? - -This repository contains all of the examples for my ITP course and upcoming book "The Nature of Code." It is currently in progress and as I write the book, I will continue to add and update the examples here. - -Relevant links: - -[Site](http://www.natureofcode.com) - -[Online tutorials](http://www.shiffman.net/teaching/nature/) - -[2011 Syllabus](http://itp.nyu.edu/varwiki/Syllabus/Nature-of-Code-S11) diff --git a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Connection.pde b/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Connection.pde deleted file mode 100644 index 8d6ec8907..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Connection.pde +++ /dev/null @@ -1,63 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Connection { - // Connection is from Neuron A to B - Neuron a; - Neuron b; - - // Connection has a weight - float weight; - - // Variables to track the animation - boolean sending = false; - PVector sender; - - // Need to store the output for when its time to pass along - float output = 0; - - Connection(Neuron from, Neuron to, float w) { - weight = w; - a = from; - b = to; - } - - - // The Connection is active - void feedforward(float val) { - output = val*weight; // Compute output - sender = a.location.get(); // Start animation at Neuron A - sending = true; // Turn on sending - } - - // Update traveling sender - void update() { - if (sending) { - // Use a simple interpolation - sender.x = lerp(sender.x, b.location.x, 0.1); - sender.y = lerp(sender.y, b.location.y, 0.1); - float d = PVector.dist(sender, b.location); - // If we've reached the end - if (d < 1) { - // Pass along the output! - b.feedforward(output); - sending = false; - } - } - } - - // Draw line and traveling circle - void display() { - stroke(0); - strokeWeight(1+weight*4); - line(a.location.x, a.location.y, b.location.x, b.location.y); - - if (sending) { - fill(0); - strokeWeight(1); - ellipse(sender.x, sender.y, 16, 16); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Exercise_10_5_LayeredNetworkAnimation.pde b/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Exercise_10_5_LayeredNetworkAnimation.pde deleted file mode 100644 index 951bf44c0..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Exercise_10_5_LayeredNetworkAnimation.pde +++ /dev/null @@ -1,49 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -Network network; - -void setup() { - size(750,200); - smooth(); - - // Create the Network object - network = new Network(width/2, height/2); - - int layers = 3; - int inputs = 2; - - Neuron output = new Neuron(250, 0); - for (int i = 0; i < layers; i++) { - for (int j = 0; j < inputs; j++) { - float x = map(i, 0, layers, -300, 300); - float y = map(j, 0, inputs-1, -75, 75); - Neuron n = new Neuron(x, y); - if (i > 0) { - for (int k = 0; k < inputs; k++) { - Neuron prev = network.neurons.get(network.neurons.size()-inputs+k-j); - network.connect(prev, n, random(1)); - } - } - if (i == layers-1) { - network.connect(n, output, random(1)); - } - network.addNeuron(n); - } - } - network.addNeuron(output); -} - -void draw() { - background(255); - // Update and display the Network - network.update(); - network.display(); - - // Every 30 frames feed in an input - if (frameCount % 30 == 0) { - network.feedforward(random(1),random(1)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Network.pde b/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Network.pde deleted file mode 100644 index 045808741..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Network.pde +++ /dev/null @@ -1,66 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Network { - - // The Network has a list of neurons - ArrayList neurons; - - // The Network now keeps a duplicate list of all Connection objects. - // This makes it easier to draw everything in this class - ArrayList connections; - PVector location; - - Network(float x, float y) { - location = new PVector(x, y); - neurons = new ArrayList(); - connections = new ArrayList(); - } - - // We can add a Neuron - void addNeuron(Neuron n) { - neurons.add(n); - } - - // We can connection two Neurons - void connect(Neuron a, Neuron b, float weight) { - Connection c = new Connection(a, b, weight); - a.addConnection(c); - // Also add the Connection here - connections.add(c); - } - - // Sending an input to the first Neuron - // We should do something better to track multiple inputs - void feedforward(float input1, float input2) { - Neuron n1 = neurons.get(0); - n1.feedforward(input1); - - Neuron n2 = neurons.get(1); - n2.feedforward(input2); - - } - - // Update the animation - void update() { - for (Connection c : connections) { - c.update(); - } - } - - // Draw everything - void display() { - pushMatrix(); - translate(location.x, location.y); - for (Neuron n : neurons) { - n.display(); - } - - for (Connection c : connections) { - c.display(); - } - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Neuron.pde b/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Neuron.pde deleted file mode 100644 index 372ba3fa7..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/Exercise_10_5_LayeredNetworkAnimation/Neuron.pde +++ /dev/null @@ -1,62 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Neuron { - // Neuron has a location - PVector location; - - // Neuron has a list of connections - ArrayList connections; - - // We now track the inputs and sum them - float sum = 0; - - // The Neuron's size can be animated - float r = 32; - - Neuron(float x, float y) { - location = new PVector(x, y); - connections = new ArrayList(); - } - - // Add a Connection - void addConnection(Connection c) { - connections.add(c); - } - - // Receive an input - void feedforward(float input) { - // Accumulate it - sum += input; - // Activate it? - if (sum > 1) { - fire(); - sum = 0; // Reset the sum to 0 if it fires - } - } - - // The Neuron fires - void fire() { - r = 64; // It suddenly is bigger - - // We send the output through all connections - for (Connection c : connections) { - c.feedforward(sum); - } - } - - // Draw it as a circle - void display() { - stroke(0); - strokeWeight(1); - // Brightness is mapped to sum - float b = map(sum,0,1,255,0); - fill(b); - ellipse(location.x, location.y, r, r); - - // Size shrinks down back to original dimensions - r = lerp(r,32,0.1); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Connection.pde b/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Connection.pde deleted file mode 100644 index e0a563df8..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Connection.pde +++ /dev/null @@ -1,20 +0,0 @@ -class Connection { - float weight; - Neuron a; - Neuron b; - - Connection(Neuron from, Neuron to,float w) { - weight = w; - a = from; - b = to; - } - - void display() { - stroke(0); - strokeWeight(weight*4); - line(a.location.x, a.location.y, b.location.x, b.location.y); - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/LayeredNetworkViz.pde b/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/LayeredNetworkViz.pde deleted file mode 100644 index 0e5597662..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/LayeredNetworkViz.pde +++ /dev/null @@ -1,14 +0,0 @@ - -Network network; - -void setup() { - size(640, 360); - smooth(); - network = new Network(4,3,1); -} - -void draw() { - background(255); - network.display(); -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Network.pde b/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Network.pde deleted file mode 100644 index 2a5515ea2..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Network.pde +++ /dev/null @@ -1,45 +0,0 @@ - - -class Network { - ArrayList neurons; - PVector location; - Network(int layers, int inputs, int outputs) { - location = new PVector(width/2, height/2); - - neurons = new ArrayList(); - - Neuron output = new Neuron(250, 0); - for (int i = 0; i < layers; i++) { - for (int j = 0; j < inputs; j++) { - float x = map(i, 0, layers, -200, 200); - float y = map(j, 0, inputs-1, -100, 100); - println(j + " " + y); - Neuron n = new Neuron(x, y); - - if (i > 0) { - for (int k = 0; k < inputs; k++) { - Neuron prev = neurons.get(neurons.size()-inputs+k-j); - prev.connect(n); - } - } - - if (i == layers-1) { - n.connect(output); - } - neurons.add(n); - } - } - neurons.add(output); - } - - - void display() { - pushMatrix(); - translate(location.x, location.y); - for (Neuron n : neurons) { - n.display(); - } - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Neuron.pde b/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Neuron.pde deleted file mode 100644 index b99256c82..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/Neuron.pde +++ /dev/null @@ -1,29 +0,0 @@ - - -class Neuron { - PVector location; - - ArrayList connections; - - Neuron(float x, float y) { - location = new PVector(x, y); - connections = new ArrayList(); - } - - void connect(Neuron n) { - Connection c = new Connection(this, n, random(1)); - connections.add(c); - } - - void display() { - stroke(0); - strokeWeight(1); - fill(0); - ellipse(location.x, location.y, 16, 16); - - for (Connection c : connections) { - c.display(); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/sketch.properties b/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/LayeredNetworkViz/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/NOC_10_01_SimplePerceptron.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/NOC_10_01_SimplePerceptron.pde deleted file mode 100644 index 67658e853..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/NOC_10_01_SimplePerceptron.pde +++ /dev/null @@ -1,89 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/teaching/nature -// Simple Perceptron Example -// See: http://en.wikipedia.org/wiki/Perceptron - -// Code based on text "Artificial Intelligence", George Luger - -// A list of points we will use to "train" the perceptron -Trainer[] training = new Trainer[2000]; -// A Perceptron object -Perceptron ptron; - -// We will train the perceptron with one "Point" object at a time -int count = 0; - -// Coordinate space -float xmin = -400; -float ymin = -100; -float xmax = 400; -float ymax = 100; - -// The function to describe a line -float f(float x) { - return 0.4*x+1; -} - -void setup() { - size(800, 200); - - // The perceptron has 3 inputs -- x, y, and bias - // Second value is "Learning Constant" - ptron = new Perceptron(3, 0.00001); // Learning Constant is low just b/c it's fun to watch, this is not necessarily optimal - - // Create a random set of training points and calculate the "known" answer - for (int i = 0; i < training.length; i++) { - float x = random(xmin, xmax); - float y = random(ymin, ymax); - int answer = 1; - if (y < f(x)) answer = -1; - training[i] = new Trainer(x, y, answer); - } - smooth(); -} - - -void draw() { - background(255); - translate(width/2,height/2); - - // Draw the line - strokeWeight(4); - stroke(127); - float x1 = xmin; - float y1 = f(x1); - float x2 = xmax; - float y2 = f(x2); - line(x1,y1,x2,y2); - - // Draw the line based on the current weights - // Formula is weights[0]*x + weights[1]*y + weights[2] = 0 - stroke(0); - strokeWeight(1); - float[] weights = ptron.getWeights(); - x1 = xmin; - y1 = (-weights[2] - weights[0]*x1)/weights[1]; - x2 = xmax; - y2 = (-weights[2] - weights[0]*x2)/weights[1]; - line(x1,y1,x2,y2); - - - - // Train the Perceptron with one "training" point at a time - ptron.train(training[count].inputs, training[count].answer); - count = (count + 1) % training.length; - - // Draw all the points based on what the Perceptron would "guess" - // Does not use the "known" correct answer - for (int i = 0; i < count; i++) { - stroke(0); - strokeWeight(1); - fill(0); - int guess = ptron.feedforward(training[i].inputs); - if (guess > 0) noFill(); - - ellipse(training[i].inputs[0], training[i].inputs[1], 8, 8); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/Perceptron.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/Perceptron.pde deleted file mode 100644 index f49e0343b..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/Perceptron.pde +++ /dev/null @@ -1,59 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/teaching/nature -// Simple Perceptron Example -// See: http://en.wikipedia.org/wiki/Perceptron - -// Perceptron Class - -class Perceptron { - float[] weights; // Array of weights for inputs - float c; // learning constant - - // Perceptron is created with n weights and learning constant - Perceptron(int n, float c_) { - weights = new float[n]; - // Start with random weights - for (int i = 0; i < weights.length; i++) { - weights[i] = random(-1,1); - } - c = c_; - } - - // Function to train the Perceptron - // Weights are adjusted based on "desired" answer - void train(float[] inputs, int desired) { - // Guess the result - int guess = feedforward(inputs); - // Compute the factor for changing the weight based on the error - // Error = desired output - guessed output - // Note this can only be 0, -2, or 2 - // Multiply by learning constant - float error = desired - guess; - // Adjust weights based on weightChange * input - for (int i = 0; i < weights.length; i++) { - weights[i] += c * error * inputs[i]; - } - } - - // Guess -1 or 1 based on input values - int feedforward(float[] inputs) { - // Sum all values - float sum = 0; - for (int i = 0; i < weights.length; i++) { - sum += inputs[i]*weights[i]; - } - // Result is sign of the sum, -1 or 1 - return activate(sum); - } - - int activate(float sum) { - if (sum > 0) return 1; - else return -1; - } - - // Return weights - float[] getWeights() { - return weights; - } -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/Trainer.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/Trainer.pde deleted file mode 100644 index f7ee88d68..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_01_SimplePerceptron/Trainer.pde +++ /dev/null @@ -1,23 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/teaching/nature -// Simple Perceptron Example -// See: http://en.wikipedia.org/wiki/Perceptron - -// A class to describe a training point -// Has an x and y, a "bias" (1) and known output -// Could also add a variable for "guess" but not required here - -class Trainer { - - float[] inputs; - int answer; - - Trainer(float x, float y, int a) { - inputs = new float[3]; - inputs[0] = x; - inputs[1] = y; - inputs[2] = 1; - answer = a; - } -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/NOC_10_02_SeekingNeural.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/NOC_10_02_SeekingNeural.pde deleted file mode 100644 index 66fe6590a..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/NOC_10_02_SeekingNeural.pde +++ /dev/null @@ -1,62 +0,0 @@ -// A Vehicle controlled by a Perceptron -// Daniel Shiffman -// Nature of Code, Spring 2011 - -Vehicle v; - -PVector desired; - -ArrayList targets; - -void setup() { - size(800, 200); - smooth(); - - // The Vehicle's desired location - desired = new PVector(width/2,height/2); - - - // Create a list of targets - makeTargets(); - - // Create the Vehicle (it has to know about the number of targets - // in order to configure its brain) - v = new Vehicle(targets.size(), random(width), random(height)); -} - -// Make a random ArrayList of targets to steer towards -void makeTargets() { - targets = new ArrayList(); - for (int i = 0; i < 8; i++) { - targets.add(new PVector(random(width), random(height))); - } -} - -void draw() { - background(255); - - // Draw a rectangle to show the Vehicle's goal - rectMode(CENTER); - stroke(0); - strokeWeight(2); - fill(0, 100); - rect(desired.x, desired.y, 36, 36); - - // Draw the targets - for (PVector target : targets) { - fill(0, 100); - stroke(0); - strokeWeight(2); - ellipse(target.x, target.y, 30, 30); - } - - // Update the Vehicle - v.steer(targets); - v.update(); - v.display(); -} - -void mousePressed() { - makeTargets(); -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/Perceptron.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/Perceptron.pde deleted file mode 100644 index 8936904f8..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/Perceptron.pde +++ /dev/null @@ -1,44 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/teaching/nature -// Simple Perceptron Example -// See: http://en.wikipedia.org/wiki/Perceptron - -// Perceptron Class - -class Perceptron { - float[] weights; // Array of weights for inputs - float c; // learning constant - - // Perceptron is created with n weights and learning constant - Perceptron(int n, float c_) { - weights = new float[n]; - c = c_; - // Start with random weights - for (int i = 0; i < weights.length; i++) { - weights[i] = random(0, 1); - } - } - - // Function to train the Perceptron - // Weights are adjusted based on vehicle's error - void train(PVector[] forces, PVector error) { - for (int i = 0; i < weights.length; i++) { - weights[i] += c*error.x*forces[i].x; - weights[i] += c*error.y*forces[i].y; - weights[i] = constrain(weights[i], 0, 1); - } - } - - // Give me a steering result - PVector feedforward(PVector[] forces) { - // Sum all values - PVector sum = new PVector(); - for (int i = 0; i < weights.length; i++) { - forces[i].mult(weights[i]); - sum.add(forces[i]); - } - return sum; - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/Vehicle.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/Vehicle.pde deleted file mode 100644 index efd9bf287..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_02_SeekingNeural/Vehicle.pde +++ /dev/null @@ -1,102 +0,0 @@ -// Seek -// Daniel Shiffman - -// The "Vehicle" class - -class Vehicle { - - // Vehicle now has a brain! - Perceptron brain; - - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(int n, float x, float y) { - brain = new Perceptron(n,0.001); - acceleration = new PVector(0,0); - velocity = new PVector(0,0); - location = new PVector(x,y); - r = 3.0; - maxspeed = 4; - maxforce = 0.1; - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelerationelertion to 0 each cycle - acceleration.mult(0); - - location.x = constrain(location.x,0,width); - location.y = constrain(location.y,0,height); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // Here is where the brain processes everything - void steer(ArrayList targets) { - // Make an array of forces - PVector[] forces = new PVector[targets.size()]; - - // Steer towards all targets - for (int i = 0; i < forces.length; i++) { - forces[i] = seek(targets.get(i)); - } - - // That array of forces is the input to the brain - PVector result = brain.feedforward(forces); - - // Use the result to steer the vehicle - applyForce(result); - - // Train the brain according to the error - PVector error = PVector.sub(desired, location); - brain.train(forces,error); - - } - - // A method that calculates a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - PVector seek(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - - return steer; - } - - void display() { - - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + PI/2; - fill(175); - stroke(0); - strokeWeight(1); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(CLOSE); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Connection.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Connection.pde deleted file mode 100644 index a8e75c1ca..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Connection.pde +++ /dev/null @@ -1,29 +0,0 @@ -// A static drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Connection { - - // Connection is from Neuron A to B - Neuron a; - Neuron b; - - // Connection has a weight - float weight; - - Connection(Neuron from, Neuron to,float w) { - weight = w; - a = from; - b = to; - } - - // Drawn as a line - void display() { - stroke(0); - strokeWeight(weight*4); - line(a.location.x, a.location.y, b.location.x, b.location.y); - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/NOC_10_03_NetworkViz.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/NOC_10_03_NetworkViz.pde deleted file mode 100644 index 5aa884c9b..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/NOC_10_03_NetworkViz.pde +++ /dev/null @@ -1,39 +0,0 @@ -// A static drawing of a Neural Network -// Daniel Shiffman -// Nature of Code, Spring 2011 - -Network network; - -void setup() { - size(800, 200); - smooth(); - - // Create the Network object - network = new Network(width/2,height/2); - - // Create a bunch of Neurons - Neuron a = new Neuron(-300,0); - Neuron b = new Neuron(0,75); - Neuron c = new Neuron(0,-75); - Neuron d = new Neuron(300,0); - - // Connect them - network.connect(a,b); - network.connect(a,c); - network.connect(b,d); - network.connect(c,d); - - // Add them to the Network - network.addNeuron(a); - network.addNeuron(b); - network.addNeuron(c); - network.addNeuron(d); -} - -void draw() { - background(255); - // Draw the Network - network.display(); - noLoop(); -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Network.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Network.pde deleted file mode 100644 index 52ad9a9bf..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Network.pde +++ /dev/null @@ -1,37 +0,0 @@ -// A static drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Network { - - // The Network has a list of neurons - ArrayList neurons; - PVector location; - - Network(float x, float y) { - location = new PVector(x,y); - neurons = new ArrayList(); - } - - // We can add a Neuron - void addNeuron(Neuron n) { - neurons.add(n); - } - - // We can connection two Neurons - void connect(Neuron a, Neuron b) { - Connection c = new Connection(a, b, random(1)); - a.addConnection(c); - } - - // We can draw the network - void display() { - pushMatrix(); - translate(location.x, location.y); - for (Neuron n : neurons) { - n.display(); - } - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Neuron.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Neuron.pde deleted file mode 100644 index a6a14be07..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/Neuron.pde +++ /dev/null @@ -1,36 +0,0 @@ -// A static drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Neuron { - - // Neuron has a location - PVector location; - - // Neuron has a list of connections - ArrayList connections; - - Neuron(float x, float y) { - location = new PVector(x, y); - connections = new ArrayList(); - } - - // Add a Connection - void addConnection(Connection c) { - connections.add(c); - } - - // Draw Neuron as a circle - void display() { - stroke(0); - strokeWeight(1); - fill(0); - ellipse(location.x, location.y, 16, 16); - - // Draw all its connections - for (Connection c : connections) { - c.display(); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/sketch.properties b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_03_NetworkViz/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Connection.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Connection.pde deleted file mode 100644 index 8d6ec8907..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Connection.pde +++ /dev/null @@ -1,63 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Connection { - // Connection is from Neuron A to B - Neuron a; - Neuron b; - - // Connection has a weight - float weight; - - // Variables to track the animation - boolean sending = false; - PVector sender; - - // Need to store the output for when its time to pass along - float output = 0; - - Connection(Neuron from, Neuron to, float w) { - weight = w; - a = from; - b = to; - } - - - // The Connection is active - void feedforward(float val) { - output = val*weight; // Compute output - sender = a.location.get(); // Start animation at Neuron A - sending = true; // Turn on sending - } - - // Update traveling sender - void update() { - if (sending) { - // Use a simple interpolation - sender.x = lerp(sender.x, b.location.x, 0.1); - sender.y = lerp(sender.y, b.location.y, 0.1); - float d = PVector.dist(sender, b.location); - // If we've reached the end - if (d < 1) { - // Pass along the output! - b.feedforward(output); - sending = false; - } - } - } - - // Draw line and traveling circle - void display() { - stroke(0); - strokeWeight(1+weight*4); - line(a.location.x, a.location.y, b.location.x, b.location.y); - - if (sending) { - fill(0); - strokeWeight(1); - ellipse(sender.x, sender.y, 16, 16); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/NOC_10_04_NetworkAnimation.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/NOC_10_04_NetworkAnimation.pde deleted file mode 100644 index 07596246b..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/NOC_10_04_NetworkAnimation.pde +++ /dev/null @@ -1,50 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -Network network; - -void setup() { - size(800, 200); - smooth(); - - // Create the Network object - network = new Network(width/2, height/2); - - // Create a bunch of Neurons - Neuron a = new Neuron(-350, 0); - Neuron b = new Neuron(-200, 0); - Neuron c = new Neuron(0, 75); - Neuron d = new Neuron(0, -75); - Neuron e = new Neuron(200, 0); - Neuron f = new Neuron(350, 0); - - // Connect them - network.connect(a, b,1); - network.connect(b, c,random(1)); - network.connect(b, d,random(1)); - network.connect(c, e,random(1)); - network.connect(d, e,random(1)); - network.connect(e, f,1); - - // Add them to the Network - network.addNeuron(a); - network.addNeuron(b); - network.addNeuron(c); - network.addNeuron(d); - network.addNeuron(e); - network.addNeuron(f); -} - -void draw() { - background(255); - // Update and display the Network - network.update(); - network.display(); - - // Every 30 frames feed in an input - if (frameCount % 30 == 0) { - network.feedforward(random(1)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Network.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Network.pde deleted file mode 100644 index 2a2d2e5b5..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Network.pde +++ /dev/null @@ -1,62 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Network { - - // The Network has a list of neurons - ArrayList neurons; - - // The Network now keeps a duplicate list of all Connection objects. - // This makes it easier to draw everything in this class - ArrayList connections; - PVector location; - - Network(float x, float y) { - location = new PVector(x, y); - neurons = new ArrayList(); - connections = new ArrayList(); - } - - // We can add a Neuron - void addNeuron(Neuron n) { - neurons.add(n); - } - - // We can connection two Neurons - void connect(Neuron a, Neuron b, float weight) { - Connection c = new Connection(a, b, weight); - a.addConnection(c); - // Also add the Connection here - connections.add(c); - } - - // Sending an input to the first Neuron - // We should do something better to track multiple inputs - void feedforward(float input) { - Neuron start = neurons.get(0); - start.feedforward(input); - } - - // Update the animation - void update() { - for (Connection c : connections) { - c.update(); - } - } - - // Draw everything - void display() { - pushMatrix(); - translate(location.x, location.y); - for (Neuron n : neurons) { - n.display(); - } - - for (Connection c : connections) { - c.display(); - } - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Neuron.pde b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Neuron.pde deleted file mode 100644 index 372ba3fa7..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/Neuron.pde +++ /dev/null @@ -1,62 +0,0 @@ -// An animated drawing of a Neural Network -// Daniel Shiffman -// Nature of Code - -class Neuron { - // Neuron has a location - PVector location; - - // Neuron has a list of connections - ArrayList connections; - - // We now track the inputs and sum them - float sum = 0; - - // The Neuron's size can be animated - float r = 32; - - Neuron(float x, float y) { - location = new PVector(x, y); - connections = new ArrayList(); - } - - // Add a Connection - void addConnection(Connection c) { - connections.add(c); - } - - // Receive an input - void feedforward(float input) { - // Accumulate it - sum += input; - // Activate it? - if (sum > 1) { - fire(); - sum = 0; // Reset the sum to 0 if it fires - } - } - - // The Neuron fires - void fire() { - r = 64; // It suddenly is bigger - - // We send the output through all connections - for (Connection c : connections) { - c.feedforward(sum); - } - } - - // Draw it as a circle - void display() { - stroke(0); - strokeWeight(1); - // Brightness is mapped to sum - float b = map(sum,0,1,255,0); - fill(b); - ellipse(location.x, location.y, r, r); - - // Size shrinks down back to original dimensions - r = lerp(r,32,0.1); - } -} - diff --git a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/sketch.properties b/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/NOC_10_04_NetworkAnimation/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/Landscape.pde b/java/examples/Books/Nature of Code/chp10_nn/xor/Landscape.pde deleted file mode 100755 index 40ef0a733..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/Landscape.pde +++ /dev/null @@ -1,73 +0,0 @@ -// Daniel Shiffman -// -// "Landscape" example - -class Landscape { - - int scl; // size of each cell - int w,h; // width and height of thingie - int rows, cols; // number of rows and columns - float zoff = 0.0; // perlin noise argument - float[][] z; // using an array to store all the height values - - Landscape(int scl_, int w_, int h_) { - scl = scl_; - w = w_; - h = h_; - cols = w/scl; - rows = h/scl; - z = new float[cols][rows]; - } - - - // Calculate height values (based off a neural netork) - void calculate(Network nn) { - float x = 0; - float dx = (float) 1.0 / cols; - for (int i = 0; i < cols; i++) - { - float y = 0; - float dy = (float) 1.0 / rows; - for (int j = 0; j < rows; j++) - { - float[] input = new float[2]; - input[0] = x; - input[1] = y; - float result = nn.feedForward(input); - z[i][j] = z[i][j]*0.95 + 0.05*(float)(result*280.0f-140.0); - y += dy; - } - x += dx; - } - - } - - // Render landscape as grid of quads - void render() { - // Every cell is an individual quad - // (could use quad_strip here, but produces funny results, investigate this) - for (int x = 0; x < z.length-1; x++) - { - for (int y = 0; y < z[x].length-1; y++) - { - // one quad at a time - // each quad's color is determined by the height value at each vertex - // (clean this part up) - noStroke(); - pushMatrix(); - beginShape(QUADS); - translate(x*scl-w/2,y*scl-h/2,0); - fill(z[x][y]+127,220); - vertex(0,0,z[x][y]); - fill(z[x+1][y]+127,220); - vertex(scl,0,z[x+1][y]); - fill(z[x+1][y+1]+127,220); - vertex(scl,scl,z[x+1][y+1]); - fill(z[x][y+1]+127,220); - vertex(0,scl,z[x][y+1]); - endShape(); - popMatrix(); - } - } - } -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Connection.java b/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Connection.java deleted file mode 100644 index d8415d2a6..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Connection.java +++ /dev/null @@ -1,47 +0,0 @@ -// Daniel Shiffman -// The Nature of Code, Fall 2006 -// Neural Network - -// Class to describe a connection between two neurons - -package nn; - -public class Connection { - - private Neuron from; // Connection goes from. . . - private Neuron to; // To. . . - private float weight; // Weight of the connection. . . - - // Constructor builds a connection with a random weight - public Connection(Neuron a_, Neuron b_) { - from = a_; - to = b_; - weight = (float) Math.random()*2-1; - } - - // In case I want to set the weights manually, using this for testing - public Connection(Neuron a_, Neuron b_, float w) { - from = a_; - to = b_; - weight = w; - } - - public Neuron getFrom() { - return from; - } - - public Neuron getTo() { - return to; - } - - public float getWeight() { - return weight; - } - - // Changing the weight of the connection - public void adjustWeight(float deltaWeight) { - weight += deltaWeight; - } - - -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/HiddenNeuron.java b/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/HiddenNeuron.java deleted file mode 100644 index 3c8d7945d..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/HiddenNeuron.java +++ /dev/null @@ -1,20 +0,0 @@ -//Daniel Shiffman -//The Nature of Code, Fall 2006 -//Neural Network - -// Hidden Neuron Class -// So far not necessary to differentiate these - -package nn; - -public class HiddenNeuron extends Neuron { - - public HiddenNeuron() { - super(); - } - - public HiddenNeuron(int i) { - super(i); - } - -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/InputNeuron.java b/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/InputNeuron.java deleted file mode 100644 index a2191632f..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/InputNeuron.java +++ /dev/null @@ -1,23 +0,0 @@ -//Daniel Shiffman -//The Nature of Code, Fall 2006 -//Neural Network - -// Input Neuron Class -// Has additional functionality to receive beginning input - -package nn; - -public class InputNeuron extends Neuron { - public InputNeuron() { - super(); - } - - public InputNeuron(int i) { - super(i); - } - - public void input(float d) { - output = d; - } - -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Network.java b/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Network.java deleted file mode 100644 index c0854712d..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Network.java +++ /dev/null @@ -1,143 +0,0 @@ -// Daniel Shiffman -// The Nature of Code, Fall 2006 -// Neural Network - -// Class to describe the entire network -// Arrays for input neurons, hidden neurons, and output neuron - -// Need to update this so that it would work with an array out outputs -// Rather silly that I didn't do this initially - -// Also need to build in a "Layer" class so that there can easily -// be more than one hidden layer - -package nn; - -import java.util.ArrayList; - -public class Network { - - // Layers - InputNeuron[] input; - HiddenNeuron[] hidden; - OutputNeuron output; - - public static final float LEARNING_CONSTANT = 0.5f; - - // Only One output now to start!!! (i can do better, really. . .) - // Constructor makes the entire network based on number of inputs & number of neurons in hidden layer - // Only One hidden layer!!! (fix this dood) - - public Network(int inputs, int hiddentotal) { - - input = new InputNeuron[inputs+1]; // Got to add a bias input - hidden = new HiddenNeuron[hiddentotal+1]; - - // Make input neurons - for (int i = 0; i < input.length-1; i++) { - input[i] = new InputNeuron(); - } - - // Make hidden neurons - for (int i = 0; i < hidden.length-1; i++) { - hidden[i] = new HiddenNeuron(); - } - - // Make bias neurons - input[input.length-1] = new InputNeuron(1); - hidden[hidden.length-1] = new HiddenNeuron(1); - - // Make output neuron - output = new OutputNeuron(); - - // Connect input layer to hidden layer - for (int i = 0; i < input.length; i++) { - for (int j = 0; j < hidden.length-1; j++) { - // Create the connection object and put it in both neurons - Connection c = new Connection(input[i],hidden[j]); - input[i].addConnection(c); - hidden[j].addConnection(c); - } - } - - // Connect the hidden layer to the output neuron - for (int i = 0; i < hidden.length; i++) { - Connection c = new Connection(hidden[i],output); - hidden[i].addConnection(c); - output.addConnection(c); - } - - } - - - public float feedForward(float[] inputVals) { - - // Feed the input with an array of inputs - for (int i = 0; i < inputVals.length; i++) { - input[i].input(inputVals[i]); - } - - // Have the hidden layer calculate its output - for (int i = 0; i < hidden.length-1; i++) { - hidden[i].calcOutput(); - } - - // Calculate the output of the output neuron - output.calcOutput(); - - // Return output - return output.getOutput(); - } - - public float train(float[] inputs, float answer) { - float result = feedForward(inputs); - - - // This is where the error correction all starts - // Derivative of sigmoid output function * diff between known and guess - float deltaOutput = result*(1-result) * (answer-result); - - - // BACKPROPOGATION - // This is easier b/c we just have one output - // Apply Delta to connections between hidden and output - ArrayList connections = output.getConnections(); - for (int i = 0; i < connections.size(); i++) { - Connection c = (Connection) connections.get(i); - Neuron neuron = c.getFrom(); - float output = neuron.getOutput(); - float deltaWeight = output*deltaOutput; - c.adjustWeight(LEARNING_CONSTANT*deltaWeight); - } - - // ADJUST HIDDEN WEIGHTS - for (int i = 0; i < hidden.length; i++) { - connections = hidden[i].getConnections(); - float sum = 0; - // Sum output delta * hidden layer connections (just one output) - for (int j = 0; j < connections.size(); j++) { - Connection c = (Connection) connections.get(j); - // Is this a connection from hidden layer to next layer (output)? - if (c.getFrom() == hidden[i]) { - sum += c.getWeight()*deltaOutput; - } - } - // Then adjust the weights coming in based: - // Above sum * derivative of sigmoid output function for hidden neurons - for (int j = 0; j < connections.size(); j++) { - Connection c = (Connection) connections.get(j); - // Is this a connection from previous layer (input) to hidden layer? - if (c.getTo() == hidden[i]) { - float output = hidden[i].getOutput(); - float deltaHidden = output * (1 - output); // Derivative of sigmoid(x) - deltaHidden *= sum; // Would sum for all outputs if more than one output - Neuron neuron = c.getFrom(); - float deltaWeight = neuron.getOutput()*deltaHidden; - c.adjustWeight(LEARNING_CONSTANT*deltaWeight); - } - } - } - - return result; - } -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Neuron.java b/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Neuron.java deleted file mode 100644 index 234780016..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/Neuron.java +++ /dev/null @@ -1,81 +0,0 @@ -//Daniel Shiffman -//The Nature of Code, Fall 2006 -//Neural Network - -//Generic Neuron Class -//Can be a bias neuron (true or false) - -package nn; - -import java.util.ArrayList; - -public class Neuron { - - protected float output; - protected ArrayList connections; - protected boolean bias = false; - - // A regular Neuron - public Neuron() { - output = 0; - // Using an arraylist to store list of connections to other neurons - connections = new ArrayList(); - bias = false; - } - - // Constructor for a bias neuron - public Neuron(int i) { - output = i; - connections = new ArrayList(); - bias = true; - } - - // Function to calculate output of this neuron - // Output is sum of all inputs*weight of connections - public void calcOutput() { - if (bias) { - // do nothing - } else { - float sum = 0; - float bias = 0; - //System.out.println("Looking through " + connections.size() + " connections"); - for (int i = 0; i < connections.size(); i++) { - Connection c = (Connection) connections.get(i); - Neuron from = c.getFrom(); - Neuron to = c.getTo(); - // Is this connection moving forward to us - // Ignore connections that we send our output to - if (to == this) { - // This isn't really necessary - // But I am treating the bias individually in case I need to at some point - if (from.bias) { - bias = from.getOutput()*c.getWeight(); - } else { - sum += from.getOutput()*c.getWeight(); - } - } - } - // Output is result of sigmoid function - output = f(bias+sum); - } - } - - void addConnection(Connection c) { - connections.add(c); - } - - float getOutput() { - return output; - } - - // Sigmoid function - public static float f(float x) { - return 1.0f / (1.0f + (float) Math.exp(-x)); - } - - public ArrayList getConnections() { - return connections; - } - - -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/OutputNeuron.java b/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/OutputNeuron.java deleted file mode 100644 index abe8daee4..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/code/src/OutputNeuron.java +++ /dev/null @@ -1,7 +0,0 @@ -package nn; - -public class OutputNeuron extends Neuron { - public OutputNeuron() { - super(); - } -} diff --git a/java/examples/Books/Nature of Code/chp10_nn/xor/data/GillSans-16.vlw b/java/examples/Books/Nature of Code/chp10_nn/xor/data/GillSans-16.vlw deleted file mode 100755 index 380eee76c..000000000 Binary files a/java/examples/Books/Nature of Code/chp10_nn/xor/data/GillSans-16.vlw and /dev/null differ 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 deleted file mode 100755 index eab37c5c7..000000000 --- a/java/examples/Books/Nature of Code/chp10_nn/xor/xor.pde +++ /dev/null @@ -1,114 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/teaching/nature - -// XOR Multi-Layered Neural Network Example -// Neural network code is all in the "code" folder - -import processing.opengl.*; -import nn.*; - -ArrayList inputs; // List of training input values -Network nn; // Neural Network Object -int count; // Total training interations -Landscape land; // Solution space -float theta = 0.0; // Angle of rotation -PFont f; // Font - - -void setup() { - - size(400,400,OPENGL); - - // Create a landscape object - land = new Landscape(20,300,300); - - f = createFont("Courier",12,true); - - nn = new Network(2,4); - - // Create a list of 4 training inputs - inputs = new ArrayList(); - float[] input = new float[2]; - input[0] = 1; - input[1] = 0; - inputs.add((float []) input.clone()); - input[0] = 0; - input[1] = 1; - inputs.add((float []) input.clone()); - input[0] = 1; - input[1] = 1; - inputs.add((float []) input.clone()); - input[0] = 0; - input[1] = 0; - inputs.add((float []) input.clone()); -} - -void draw() { - - int trainingIterationsPerFrame = 5; - - for (int i = 0; i < trainingIterationsPerFrame; i++) { - // Pick a random training input - int pick = int(random(inputs.size())); - // Grab that input - 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; - // Train that sucker! - float result = nn.train(inp,known); - count++; - } - - // Ok, visualize the solution space - background(175); - pushMatrix(); - translate(width/2,height/2+20,-160); - rotateX(PI/3); - rotateZ(theta); - - // Put a little BOX on screen - pushMatrix(); - stroke(50); - noFill(); - translate(-10,-10,0); - box(280); - - // Draw the landscape - popMatrix(); - land.calculate(nn); - land.render(); - theta += 0.0025; - popMatrix(); - - // Display overal neural net stats - networkStatus(); - -} - - -void networkStatus() { - float mse = 0.0; - - textFont(f); - fill(0); - text("Your friendly neighborhood neural network solving XOR.",10,20); - text("Total iterations: " + count,10,40); - - for (int i = 0; i < inputs.size(); 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); - //System.out.println("For: " + inp[0] + " " + inp[1] + ": " + result); - mse += (result - known)*(result - known); - } - - float rmse = sqrt(mse/4.0); - DecimalFormat df = new DecimalFormat("0.000"); - text("Root mean squared error: " + df.format(rmse), 10,60); - -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_10_motion101_acceleration/Mover.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_10_motion101_acceleration/Mover.pde deleted file mode 100644 index e7fc0c200..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_10_motion101_acceleration/Mover.pde +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Acceleration with Vectors - * by Daniel Shiffman. - * - * Demonstration of the basics of motion with vector. - * A "Mover" object stores location, velocity, and acceleration as vectors - * The motion is controlled by affecting the acceleration (in this case towards the mouse) - */ - - -class Mover { - - // The Mover tracks location, velocity, and acceleration - PVector location; - PVector velocity; - PVector acceleration; - // The Mover's maximum speed - float topspeed; - - Mover() { - // Start in the center - location = new PVector(width/2,height/2); - velocity = new PVector(0,0); - topspeed = 5; - } - - void update() { - - // Compute a vector that points from location to mouse - PVector mouse = new PVector(mouseX,mouseY); - PVector acceleration = PVector.sub(mouse,location); - // Set magnitude of acceleration - //acceleration.setMag(0.2); - acceleration.normalize(); - acceleration.mult(0.2); - - // Velocity changes according to acceleration - velocity.add(acceleration); - // Limit the velocity by topspeed - velocity.limit(topspeed); - // Location changes by velocity - location.add(velocity); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x,location.y,48,48); - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pde deleted file mode 100644 index 6822430f0..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_10_motion101_acceleration/NOC_1_10_motion101_acceleration.pde +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Acceleration with Vectors - * by Daniel Shiffman. - * - * Demonstration of the basics of motion with vector. - * A "Mover" object stores location, velocity, and acceleration as vectors - * The motion is controlled by affecting the acceleration (in this case towards the mouse) - */ - -// A Mover object -Mover mover; - -void setup() { - size(800,200); - smooth(); - mover = new Mover(); -} - -void draw() { - background(255); - - // Update the location - mover.update(); - // Display the Mover - mover.display(); -} - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_11_motion101_acceleration_array/Mover.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_11_motion101_acceleration_array/Mover.pde deleted file mode 100644 index 19114ebf6..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_11_motion101_acceleration_array/Mover.pde +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Acceleration with Vectors - * by Daniel Shiffman. - * - * Demonstration of the basics of motion with vector. - * A "Mover" object stores location, velocity, and acceleration as vectors - * The motion is controlled by affecting the acceleration (in this case towards the mouse) - */ - - -class Mover { - - // The Mover tracks location, velocity, and acceleration - PVector location; - PVector velocity; - PVector acceleration; - // The Mover's maximum speed - float topspeed; - - Mover() { - // Start in the center - location = new PVector(random(width),random(height)); - velocity = new PVector(0,0); - topspeed = 5; - } - - void update() { - - // Compute a vector that points from location to mouse - PVector mouse = new PVector(mouseX,mouseY); - PVector acceleration = PVector.sub(mouse,location); - // Set magnitude of acceleration - //acceleration.setMag(0.2); - acceleration.normalize(); - acceleration.mult(0.2); - - // Velocity changes according to acceleration - velocity.add(acceleration); - // Limit the velocity by topspeed - velocity.limit(topspeed); - // Location changes by velocity - location.add(velocity); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(127,200); - ellipse(location.x,location.y,48,48); - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pde deleted file mode 100644 index b1cafa931..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_11_motion101_acceleration_array/NOC_1_11_motion101_acceleration_array.pde +++ /dev/null @@ -1,21 +0,0 @@ -Mover[] movers = new Mover[20]; - -void setup() { - size(800,200); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(); - } -} - -void draw() { - - background(255); - - for (int i = 0; i < movers.length; i++) { - movers[i].update(); - movers[i].display(); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pde deleted file mode 100644 index 6dfb4b6a1..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_1_bouncingball_novectors/NOC_1_1_bouncingball_novectors.pde +++ /dev/null @@ -1,38 +0,0 @@ -// The Nature of Code -// Daniel Shiffman -// Draft book - -// Example 1-1: Bouncing Ball, no vectors -float x = 100; -float y = 100; -float xspeed = 2.5; -float yspeed = 2; - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - - - // Add the current speed to the location. - x = x + xspeed; - y = y + yspeed; - - if ((x > width) || (x < 0)) { - xspeed = xspeed * -1; - } - if ((y > height) || (y < 0)) { - yspeed = yspeed * -1; - } - - - // Display circle at x location - stroke(0); - strokeWeight(2); - fill(127); - ellipse(x, y, 48, 48); -} - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pde deleted file mode 100644 index 49d7912c2..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_2_bouncingball_vectors/NOC_1_2_bouncingball_vectors.pde +++ /dev/null @@ -1,38 +0,0 @@ -// The Nature of Code -// Daniel Shiffman -// Draft book - -// Example 1-2: Bouncing Ball, with PVector! -PVector location; -PVector velocity; - -void setup() { - size(200,200); - smooth(); - background(255); - location = new PVector(100,100); - velocity = new PVector(2.5,5); -} - -void draw() { - noStroke(); - fill(255,10); - rect(0,0,width,height); - - // Add the current speed to the location. - location.add(velocity); - - if ((location.x > width) || (location.x < 0)) { - velocity.x = velocity.x * -1; - } - if ((location.y > height) || (location.y < 0)) { - velocity.y = velocity.y * -1; - } - - // Display circle at x location - stroke(0); - fill(175); - ellipse(location.x,location.y,16,16); -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pde deleted file mode 100644 index b0f89414e..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_3_vector_subtraction/NOC_1_3_vector_subtraction.pde +++ /dev/null @@ -1,26 +0,0 @@ -// The Nature of Code -// Daniel Shiffman -// Draft book - -// Example 1-3: Vector subtraction - -void setup() { - size(800,200); - smooth(); -} - -void draw() { - background(255); - - PVector mouse = new PVector(mouseX,mouseY); - PVector center = new PVector(width/2,height/2); - mouse.sub(center); - - translate(width/2,height/2); - strokeWeight(2); - stroke(0); - line(0,0,mouse.x,mouse.y); - -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pde deleted file mode 100644 index fb71aa400..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_4_vector_multiplication/NOC_1_4_vector_multiplication.pde +++ /dev/null @@ -1,28 +0,0 @@ -// The Nature of Code -// Daniel Shiffman -// Draft book - -// Example 1-4: Vector multiplication - -void setup() { - size(800,200); - smooth(); -} - -void draw() { - background(255); - - PVector mouse = new PVector(mouseX,mouseY); - PVector center = new PVector(width/2,height/2); - mouse.sub(center); - - // Multiplying a vector! The vector is now half its original size (multiplied by 0.5). - mouse.mult(0.5); - - translate(width/2,height/2); - strokeWeight(2); - stroke(0); - line(0,0,mouse.x,mouse.y); -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pde deleted file mode 100644 index 45a576288..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_5_vector_magnitude/NOC_1_5_vector_magnitude.pde +++ /dev/null @@ -1,31 +0,0 @@ -// The Nature of Code -// Daniel Shiffman -// Draft book - -// Example 1-5: Vector magnitude - -void setup() { - size(800,200); - smooth(); -} - -void draw() { - background(255); - - PVector mouse = new PVector(mouseX,mouseY); - PVector center = new PVector(width/2,height/2); - mouse.sub(center); - - float m = mouse.mag(); - fill(0); - noStroke(); - rect(0,0,m,10); - - translate(width/2,height/2); - stroke(0); - strokeWeight(2); - line(0,0,mouse.x,mouse.y); - -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pde deleted file mode 100644 index 63ee12326..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_6_vector_normalize/NOC_1_6_vector_normalize.pde +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Normalize - * by Daniel Shiffman. - * - * Demonstration of normalizing a vector. - * Normalizing a vector sets its length to 1. - */ - -void setup() { - size(800,200); - smooth(); -} - -void draw() { - background(255); - - // A vector that points to the mouse location - PVector mouse = new PVector(mouseX,mouseY); - // A vector that points to the center of the window - PVector center = new PVector(width/2,height/2); - // Subtract center from mouse which results in a vector that points from center to mouse - mouse.sub(center); - - // Normalize the vector - mouse.normalize(); - - // Multiply its length by 50 - mouse.mult(150); - - translate(width/2,height/2); - // Draw the resulting vector - stroke(0); - strokeWeight(2); - line(0,0,mouse.x,mouse.y); - -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_7_motion101/Mover.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_7_motion101/Mover.pde deleted file mode 100644 index 70357ce71..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_7_motion101/Mover.pde +++ /dev/null @@ -1,39 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - - Mover() { - location = new PVector(random(width), random(height)); - velocity = new PVector(random(-2, 2), random(-2, 2)); - } - - void update() { - location.add(velocity); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x, location.y, 48, 48); - } - - void checkEdges() { - - if (location.x > width) { - location.x = 0; - } - else if (location.x < 0) { - location.x = width; - } - - if (location.y > height) { - location.y = 0; - } - else if (location.y < 0) { - location.y = height; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pde deleted file mode 100644 index 987429706..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_7_motion101/NOC_1_7_motion101.pde +++ /dev/null @@ -1,16 +0,0 @@ -Mover mover; - -void setup() { - size(800,200); - smooth(); - mover = new Mover(); -} - -void draw() { - background(255); - - mover.update(); - mover.checkEdges(); - mover.display(); -} - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_8_motion101_acceleration/Mover.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_8_motion101_acceleration/Mover.pde deleted file mode 100644 index 0f815f24a..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_8_motion101_acceleration/Mover.pde +++ /dev/null @@ -1,45 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float topspeed; - - Mover() { - location = new PVector(width/2, height/2); - velocity = new PVector(0, 0); - acceleration = new PVector(-0.001, 0.01); - topspeed = 10; - } - - void update() { - velocity.add(acceleration); - velocity.limit(topspeed); - location.add(velocity); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x, location.y, 48, 48); - } - - void checkEdges() { - - if (location.x > width) { - location.x = 0; - } - else if (location.x < 0) { - location.x = width; - } - - if (location.y > height) { - location.y = 0; - } - else if (location.y < 0) { - location.y = height; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pde deleted file mode 100644 index 34b8281b0..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_8_motion101_acceleration/NOC_1_8_motion101_acceleration.pde +++ /dev/null @@ -1,17 +0,0 @@ -Mover mover; - -void setup() { - size(800,200); - smooth(); - mover = new Mover(); -} - -void draw() { - background(255); - - mover.update(); - mover.checkEdges(); - mover.display(); -} - - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_9_motion101_acceleration/Mover.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_9_motion101_acceleration/Mover.pde deleted file mode 100644 index 4be6262ee..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_9_motion101_acceleration/Mover.pde +++ /dev/null @@ -1,49 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float topspeed; - - Mover() { - location = new PVector(width/2, height/2); - velocity = new PVector(0, 0); - topspeed = 6; - } - - void update() { - - acceleration = new PVector(random(-1, 1), random(-1, 1)); - acceleration.normalize(); - acceleration.mult(random(2)); - - velocity.add(acceleration); - velocity.limit(topspeed); - location.add(velocity); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x, location.y, 48, 48); - } - - void checkEdges() { - - if (location.x > width) { - location.x = 0; - } - else if (location.x < 0) { - location.x = width; - } - - if (location.y > height) { - location.y = 0; - } - else if (location.y < 0) { - location.y = height; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_9_motion101_acceleration/NOC_1_9_motion101_acceleration.pde b/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_9_motion101_acceleration/NOC_1_9_motion101_acceleration.pde deleted file mode 100644 index e505f1d66..000000000 --- a/java/examples/Books/Nature of Code/chp1_vectors/NOC_1_9_motion101_acceleration/NOC_1_9_motion101_acceleration.pde +++ /dev/null @@ -1,15 +0,0 @@ -Mover mover; - -void setup() { - size(800,200); - smooth(); - mover = new Mover(); -} - -void draw() { - background(255); - mover.update(); - mover.checkEdges(); - mover.display(); -} - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/Attractor.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/Attractor.pde deleted file mode 100644 index d0ce84406..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/Attractor.pde +++ /dev/null @@ -1,74 +0,0 @@ -// Attraction -// Daniel Shiffman - -// A class for a draggable attractive body in our world - -class Attractor { - float mass; // Mass, tied to size - PVector location; // Location - boolean dragging = false; // Is the object being dragged? - boolean rollover = false; // Is the mouse over the ellipse? - PVector drag; // holds the offset for when object is clicked on - - Attractor() { - location = new PVector(width/2,height/2); - mass = 10; - drag = new PVector(0.0,0.0); - } - - PVector attract(Mover m) { - PVector force = PVector.sub(location,m.location); // Calculate direction of force - float d = force.mag(); // Distance between objects - d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - float strength = (g * mass * m.mass) / (d * d); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - // Method to display - void display() { - ellipseMode(CENTER); - stroke(0); - if (dragging) fill (50); - else if (rollover) fill(100); - else fill(0); - ellipse(location.x,location.y,mass*6,mass*6); - } - - // The methods below are for mouse interaction - void clicked(int mx, int my) { - float d = dist(mx,my,location.x,location.y); - if (d < mass) { - dragging = true; - drag.x = location.x-mx; - drag.y = location.y-my; - } - } - - void rollover(int mx, int my) { - float d = dist(mx,my,location.x,location.y); - if (d < mass) { - rollover = true; - } - else { - rollover = false; - } - } - - void stopDragging() { - dragging = false; - } - - - - void drag() { - if (dragging) { - location.x = mouseX + drag.x; - location.y = mouseY + drag.y; - } - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/Mover.pde deleted file mode 100644 index 8b2214570..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/Mover.pde +++ /dev/null @@ -1,69 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x , float y) { - mass = m; - location = new PVector(x,y); - velocity = new PVector(0,0); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - fill(175,200); - ellipse(location.x,location.y,mass*2,mass*2); - } - - PVector repel(Mover m) { - PVector force = PVector.sub(location,m.location); // Calculate direction of force - float distance = force.mag(); // Distance between objects - distance = constrain(distance,1.0,10000.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction - - float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude - force.mult(-1*strength); // Get force vector --> magnitude * direction - return force; - } - - void checkEdges() { - - if (location.x > width) { - location.x = width; - velocity.x *= -1; - } - else if (location.x < 0) { - location.x = 0; - velocity.x *= -1; - } - - if (location.y > height) { - location.y = height; - velocity.y *= -1; - } - else if (location.y < 0) { - location.y = 0; - velocity.y *= -1; - } - - } - -} - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/NOC_02forces_attractrepel.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/NOC_02forces_attractrepel.pde deleted file mode 100644 index 1a4c37a61..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_attractrepel/NOC_02forces_attractrepel.pde +++ /dev/null @@ -1,52 +0,0 @@ -Mover[] movers = new Mover[20]; - -Attractor a; - -float g = 1; - -void setup() { - size(800,200); - smooth(); - a = new Attractor(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(4,12),random(width),random(height)); - } -} - -void draw() { - background(255); - - a.display(); - - - for (int i = 0; i < movers.length; i++) { - for (int j = 0; j < movers.length; j++) { - if (i != j) { - PVector force = movers[j].repel(movers[i]); - movers[i].applyForce(force); - } - } - - PVector force = a.attract(movers[i]); - movers[i].applyForce(force); - movers[i].update(); - movers[i].display(); - } - - - -} - - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/Attractor.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/Attractor.pde deleted file mode 100644 index 36b144ae3..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/Attractor.pde +++ /dev/null @@ -1,39 +0,0 @@ -// Attraction -// Daniel Shiffman - -// A class for a draggable attractive body in our world - -class Attractor { - float mass; // Mass, tied to size - PVector location; // Location - float g; - - Attractor() { - location = new PVector(0,0); - mass = 20; - g = 0.4; - } - - - PVector attract(Mover m) { - PVector force = PVector.sub(location,m.location); // Calculate direction of force - float distance = force.mag(); // Distance between objects - distance = constrain(distance,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - // Method to display - void display() { - stroke(255); - noFill(); - pushMatrix(); - translate(location.x,location.y,location.z); - sphere(mass*2); - popMatrix(); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/Mover.pde deleted file mode 100644 index 5c9c79ea3..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/Mover.pde +++ /dev/null @@ -1,51 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x, float y, float z) { - mass = m; - location = new PVector(x,y,z); - velocity = new PVector(1,0); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - noStroke(); - fill(255); - pushMatrix(); - translate(location.x,location.y,location.z); - sphere(mass*8); - popMatrix(); - } - - void checkEdges() { - - if (location.x > width) { - location.x = 0; - } - else if (location.x < 0) { - location.x = width; - } - - if (location.y > height) { - velocity.y *= -1; - location.y = height; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/NOC_02forces_many_attraction_3D.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/NOC_02forces_many_attraction_3D.pde deleted file mode 100644 index e88b686c2..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/NOC_02forces_many_attraction_3D.pde +++ /dev/null @@ -1,50 +0,0 @@ -import processing.opengl.*; - -Mover[] movers = new Mover[10]; - -Attractor a; - -float angle = 0; - -void setup() { - size(800,200,OPENGL); - smooth(); - background(255); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.1,2),random(-width/2,width/2),random(-height/2,height/2),random(-100,100)); - } - a = new Attractor(); -} - -void draw() { - background(0); - sphereDetail(8); - lights(); - translate(width/2,height/2); - rotateY(angle); - - - a.display(); - - for (int i = 0; i < movers.length; i++) { - PVector force = a.attract(movers[i]); - movers[i].applyForce(force); - - movers[i].update(); - movers[i].display(); - } - - angle += 0.003; - -} - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/sketch.properties b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_attraction_3D/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/Mover.pde deleted file mode 100644 index b092ef546..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/Mover.pde +++ /dev/null @@ -1,70 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x, float y) { - mass = m; - location = new PVector(x, y); - velocity = new PVector(0, 0); - acceleration = new PVector(0, 0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force, mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - fill(175, 200); - ellipse(location.x, location.y, mass*16, mass*16); - } - - PVector attract(Mover m) { - PVector force = PVector.sub(location, m.location); // Calculate direction of force - float distance = force.mag(); // Distance between objects - distance = constrain(distance, 5.0, 25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction - - float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - void boundaries() { - - float d = 50; - - PVector force = new PVector(0, 0); - - if (location.x < d) { - force.x = 1; - } - else if (location.x > width -d) { - force.x = -1; - } - - if (location.y < d) { - force.y = 1; - } - else if (location.y > height-d) { - force.y = -1; - } - - force.normalize(); - force.mult(0.1); - - applyForce(force); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/NOC_02forces_many_mutual_boundaries.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/NOC_02forces_many_mutual_boundaries.pde deleted file mode 100644 index 3ccfbe320..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/NOC_02forces_many_mutual_boundaries.pde +++ /dev/null @@ -1,44 +0,0 @@ -Mover[] movers = new Mover[20]; - -float g = 0.4; - -void setup() { - size(800,200); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(1,2),random(width),random(height)); - } -} - -void draw() { - background(255); - - - for (int i = 0; i < movers.length; i++) { - for (int j = 0; j < movers.length; j++) { - if (i != j) { - PVector force = movers[j].attract(movers[i]); - movers[i].applyForce(force); - } - } - - movers[i].boundaries(); - - movers[i].update(); - movers[i].display(); - } - -} - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/sketch.properties b/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/sketch.properties deleted file mode 100644 index 6d28cd598..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_02forces_many_mutual_boundaries/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=JavaScript diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_1_forces/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_1_forces/Mover.pde deleted file mode 100644 index 2e13412b5..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_1_forces/Mover.pde +++ /dev/null @@ -1,53 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover() { - location = new PVector(30,30); - velocity = new PVector(0,0); - acceleration = new PVector(0,0); - mass = 1; - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x,location.y,48,48); - } - - void checkEdges() { - - if (location.x > width) { - location.x = width; - velocity.x *= -1; - } else if (location.x < 0) { - velocity.x *= -1; - location.x = 0; - } - - if (location.y > height) { - velocity.y *= -1; - location.y = height; - } - - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_1_forces/NOC_2_1_forces.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_1_forces/NOC_2_1_forces.pde deleted file mode 100644 index c87fd78e7..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_1_forces/NOC_2_1_forces.pde +++ /dev/null @@ -1,27 +0,0 @@ -Mover m; - -void setup() { - size(800,200); - smooth(); - m = new Mover(); -} - -void draw() { - background(255); - - PVector wind = new PVector(0.01,0); - PVector gravity = new PVector(0,0.1); - m.applyForce(wind); - m.applyForce(gravity); - - - m.update(); - m.display(); - m.checkEdges(); - -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_2_forces_many/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_2_forces_many/Mover.pde deleted file mode 100644 index 02f1804bb..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_2_forces_many/Mover.pde +++ /dev/null @@ -1,53 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x , float y) { - mass = m; - location = new PVector(x,y); - velocity = new PVector(0,0); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(0,127); - ellipse(location.x,location.y,mass*16,mass*16); - } - - void checkEdges() { - - if (location.x > width) { - location.x = width; - velocity.x *= -1; - } else if (location.x < 0) { - velocity.x *= -1; - location.x = 0; - } - - if (location.y > height) { - velocity.y *= -1; - location.y = height; - } - - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_2_forces_many/NOC_2_2_forces_many.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_2_forces_many/NOC_2_2_forces_many.pde deleted file mode 100644 index bf22b65af..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_2_forces_many/NOC_2_2_forces_many.pde +++ /dev/null @@ -1,36 +0,0 @@ -Mover[] movers = new Mover[20]; - -void setup() { - size(800,200); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.1,4),0,0); - } -} - -void draw() { - background(255); - - for (int i = 0; i < movers.length; i++) { - - PVector wind = new PVector(0.01,0); - PVector gravity = new PVector(0,0.1); - - movers[i].applyForce(wind); - movers[i].applyForce(gravity); - - movers[i].update(); - movers[i].display(); - movers[i].checkEdges(); - } - -} - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_3_forces_many_realgravity/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_3_forces_many_realgravity/Mover.pde deleted file mode 100644 index 02f1804bb..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_3_forces_many_realgravity/Mover.pde +++ /dev/null @@ -1,53 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x , float y) { - mass = m; - location = new PVector(x,y); - velocity = new PVector(0,0); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(0,127); - ellipse(location.x,location.y,mass*16,mass*16); - } - - void checkEdges() { - - if (location.x > width) { - location.x = width; - velocity.x *= -1; - } else if (location.x < 0) { - velocity.x *= -1; - location.x = 0; - } - - if (location.y > height) { - velocity.y *= -1; - location.y = height; - } - - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_3_forces_many_realgravity/NOC_2_3_forces_many_realgravity.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_3_forces_many_realgravity/NOC_2_3_forces_many_realgravity.pde deleted file mode 100644 index 377b98f02..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_3_forces_many_realgravity/NOC_2_3_forces_many_realgravity.pde +++ /dev/null @@ -1,34 +0,0 @@ -Mover[] movers = new Mover[20]; - -void setup() { - size(800, 200); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(1, 4), 0, 0); - } -} - -void draw() { - background(255); - - for (int i = 0; i < movers.length; i++) { - - PVector wind = new PVector(0.01, 0); - PVector gravity = new PVector(0, 0.1*movers[i].mass); - - movers[i].applyForce(wind); - movers[i].applyForce(gravity); - - movers[i].update(); - movers[i].display(); - movers[i].checkEdges(); - } -} - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_friction/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_friction/Mover.pde deleted file mode 100644 index 7fb1353b5..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_friction/Mover.pde +++ /dev/null @@ -1,53 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x , float y) { - mass = m; - location = new PVector(x,y); - velocity = new PVector(0,0); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(0,127); - ellipse(location.x,location.y,mass*16,mass*16); - } - - void checkEdges() { - - if (location.x > width) { - location.x = width; - velocity.x *= -1; - } else if (location.x < 0) { - location.x = 0; - velocity.x *= -1; - } - - if (location.y > height) { - velocity.y *= -1; - location.y = height; - } - - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_friction/NOC_2_4_forces_friction.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_friction/NOC_2_4_forces_friction.pde deleted file mode 100644 index fccf7341c..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_friction/NOC_2_4_forces_friction.pde +++ /dev/null @@ -1,42 +0,0 @@ -Mover[] movers = new Mover[5]; - -void setup() { - size(383, 200); - randomSeed(1); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(1, 4), random(width), 0); - } -} - -void draw() { - background(255); - - for (int i = 0; i < movers.length; i++) { - - PVector wind = new PVector(0.01, 0); - PVector gravity = new PVector(0, 0.1*movers[i].mass); - - float c = 0.05; - PVector friction = movers[i].velocity.get(); - friction.mult(-1); - friction.normalize(); - friction.mult(c); - - movers[i].applyForce(friction); - movers[i].applyForce(wind); - movers[i].applyForce(gravity); - - movers[i].update(); - movers[i].display(); - movers[i].checkEdges(); - } -} - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_nofriction/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_nofriction/Mover.pde deleted file mode 100644 index 7fb1353b5..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_nofriction/Mover.pde +++ /dev/null @@ -1,53 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x , float y) { - mass = m; - location = new PVector(x,y); - velocity = new PVector(0,0); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(0,127); - ellipse(location.x,location.y,mass*16,mass*16); - } - - void checkEdges() { - - if (location.x > width) { - location.x = width; - velocity.x *= -1; - } else if (location.x < 0) { - location.x = 0; - velocity.x *= -1; - } - - if (location.y > height) { - velocity.y *= -1; - location.y = height; - } - - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_nofriction/NOC_2_4_forces_nofriction.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_nofriction/NOC_2_4_forces_nofriction.pde deleted file mode 100644 index 23f0304e2..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_4_forces_nofriction/NOC_2_4_forces_nofriction.pde +++ /dev/null @@ -1,42 +0,0 @@ -Mover[] movers = new Mover[5]; - -void setup() { - size(383, 200); - randomSeed(1); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(1, 4), random(width), 0); - } -} - -void draw() { - background(255); - - for (int i = 0; i < movers.length; i++) { - - PVector wind = new PVector(0.01, 0); - PVector gravity = new PVector(0, 0.1*movers[i].mass); - - float c = 0.05; - PVector friction = movers[i].velocity.get(); - friction.mult(-1); - friction.normalize(); - friction.mult(c); - - //movers[i].applyForce(friction); - movers[i].applyForce(wind); - movers[i].applyForce(gravity); - - movers[i].update(); - movers[i].display(); - movers[i].checkEdges(); - } -} - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/Liquid.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/Liquid.pde deleted file mode 100644 index ab0e3ee2d..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/Liquid.pde +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Forces (Gravity and Fluid Resistence) with Vectors - * by Daniel Shiffman. - * - * Demonstration of multiple force acting on bodies (Mover class) - * Bodies experience gravity continuously - * Bodies experience fluid resistance when in "water" - */ - - // Liquid class - class Liquid { - - - // Liquid is a rectangle - float x,y,w,h; - // Coefficient of drag - float c; - - Liquid(float x_, float y_, float w_, float h_, float c_) { - x = x_; - y = y_; - w = w_; - h = h_; - c = c_; - } - - // Is the Mover in the Liquid? - boolean contains(Mover m) { - PVector l = m.location; - if (l.x > x && l.x < x + w && l.y > y && l.y < y + h) { - return true; - } - else { - return false; - } - } - - // Calculate drag force - PVector drag(Mover m) { - // Magnitude is coefficient * speed squared - float speed = m.velocity.mag(); - float dragMagnitude = c * speed * speed; - - // Direction is inverse of velocity - PVector dragForce = m.velocity.get(); - dragForce.mult(-1); - - // Scale according to magnitude - // dragForce.setMag(dragMagnitude); - dragForce.normalize(); - dragForce.mult(dragMagnitude); - return dragForce; - } - - void display() { - noStroke(); - fill(50); - rect(x,y,w,h); - } - -} - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/Mover.pde deleted file mode 100644 index eaad60a81..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/Mover.pde +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Forces (Gravity and Fluid Resistence) with Vectors - * by Daniel Shiffman. - * - * Demonstration of multiple force acting on bodies (Mover class) - * Bodies experience gravity continuously - * Bodies experience fluid resistance when in "water" - */ - - -class Mover { - - // location, velocity, and acceleration - PVector location; - PVector velocity; - PVector acceleration; - - // Mass is tied to size - float mass; - - Mover(float m, float x, float y) { - mass = m; - location = new PVector(x, y); - velocity = new PVector(0, 0); - acceleration = new PVector(0, 0); - } - - // Newton's 2nd law: F = M * A - // or A = F / M - void applyForce(PVector force) { - // Divide by mass - PVector f = PVector.div(force, mass); - // Accumulate all forces in acceleration - acceleration.add(f); - } - - void update() { - - // Velocity changes according to acceleration - velocity.add(acceleration); - // Location changes by velocity - location.add(velocity); - // We must clear acceleration each frame - acceleration.mult(0); - } - - // Draw Mover - void display() { - stroke(0); - strokeWeight(2); - fill(127, 200); - ellipse(location.x, location.y, mass*16, mass*16); - } - - // Bounce off bottom of window - void checkEdges() { - if (location.y > height) { - velocity.y *= -0.9; // A little dampening when hitting the bottom - location.y = height; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/NOC_2_5_fluidresistance.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/NOC_2_5_fluidresistance.pde deleted file mode 100644 index d1a2201c4..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/NOC_2_5_fluidresistance.pde +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Forces (Gravity and Fluid Resistence) with Vectors - * by Daniel Shiffman. - * - * Demonstration of multiple force acting on bodies (Mover class) - * Bodies experience gravity continuously - * Bodies experience fluid resistance when in "water" - */ - -// Five moving bodies -Mover[] movers = new Mover[11]; - -// Liquid -Liquid liquid; - -void setup() { - size(800, 200); - smooth(); - reset(); - // Create liquid object - liquid = new Liquid(0, height/2, width, height/2, 0.1); -} - -void draw() { - background(255); - - // Draw water - liquid.display(); - - for (int i = 0; i < movers.length; i++) { - - // Is the Mover in the liquid? - if (liquid.contains(movers[i])) { - // Calculate drag force - PVector dragForce = liquid.drag(movers[i]); - // Apply drag force to Mover - movers[i].applyForce(dragForce); - } - - // Gravity is scaled by mass here! - PVector gravity = new PVector(0, 0.1*movers[i].mass); - // Apply gravity - movers[i].applyForce(gravity); - - // Update and display - movers[i].update(); - movers[i].display(); - movers[i].checkEdges(); - } - - fill(0); - text("click mouse to reset",10,30); - -} - -void mousePressed() { - reset(); -} - -// Restart all the Mover objects randomly -void reset() { - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.5, 3), 40+i*70, 0); - } -} - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/sketch.properties b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/Liquid.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/Liquid.pde deleted file mode 100644 index ab0e3ee2d..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/Liquid.pde +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Forces (Gravity and Fluid Resistence) with Vectors - * by Daniel Shiffman. - * - * Demonstration of multiple force acting on bodies (Mover class) - * Bodies experience gravity continuously - * Bodies experience fluid resistance when in "water" - */ - - // Liquid class - class Liquid { - - - // Liquid is a rectangle - float x,y,w,h; - // Coefficient of drag - float c; - - Liquid(float x_, float y_, float w_, float h_, float c_) { - x = x_; - y = y_; - w = w_; - h = h_; - c = c_; - } - - // Is the Mover in the Liquid? - boolean contains(Mover m) { - PVector l = m.location; - if (l.x > x && l.x < x + w && l.y > y && l.y < y + h) { - return true; - } - else { - return false; - } - } - - // Calculate drag force - PVector drag(Mover m) { - // Magnitude is coefficient * speed squared - float speed = m.velocity.mag(); - float dragMagnitude = c * speed * speed; - - // Direction is inverse of velocity - PVector dragForce = m.velocity.get(); - dragForce.mult(-1); - - // Scale according to magnitude - // dragForce.setMag(dragMagnitude); - dragForce.normalize(); - dragForce.mult(dragMagnitude); - return dragForce; - } - - void display() { - noStroke(); - fill(50); - rect(x,y,w,h); - } - -} - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/Mover.pde deleted file mode 100644 index 5500dc39d..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/Mover.pde +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Forces (Gravity and Fluid Resistence) with Vectors - * by Daniel Shiffman. - * - * Demonstration of multiple force acting on bodies (Mover class) - * Bodies experience gravity continuously - * Bodies experience fluid resistance when in "water" - */ - - -class Mover { - - // location, velocity, and acceleration - PVector location; - PVector velocity; - PVector acceleration; - - // Mass is tied to size - float mass; - - Mover(float m, float x, float y) { - mass = m; - location = new PVector(x, y); - velocity = new PVector(0, 0); - acceleration = new PVector(0, 0); - } - - // Newton's 2nd law: F = M * A - // or A = F / M - void applyForce(PVector force) { - // Divide by mass - PVector f = PVector.div(force, mass); - // Accumulate all forces in acceleration - acceleration.add(f); - } - - void update() { - - // Velocity changes according to acceleration - velocity.add(acceleration); - // Location changes by velocity - location.add(velocity); - // We must clear acceleration each frame - acceleration.mult(0); - } - - // Draw Mover - void display() { - stroke(0); - strokeWeight(2*2.25); - fill(127,200); - ellipse(location.x, location.y, mass*16, mass*16); - } - - // Bounce off bottom of window - void checkEdges() { - if (location.y > height) { - velocity.y *= -0.9; // A little dampening when hitting the bottom - location.y = height; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/NOC_2_5_fluidresistance_sequence.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/NOC_2_5_fluidresistance_sequence.pde deleted file mode 100644 index aa712c059..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/NOC_2_5_fluidresistance_sequence.pde +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Forces (Gravity and Fluid Resistence) with Vectors - * by Daniel Shiffman. - * - * Demonstration of multiple force acting on bodies (Mover class) - * Bodies experience gravity continuously - * Bodies experience fluid resistance when in "water" - */ - -// Five moving bodies -Mover[] movers = new Mover[5]; - -// Liquid -Liquid liquid; - -void setup() { - size(450, 450); - smooth(); - randomSeed(1); - reset(); - // Create liquid object - liquid = new Liquid(0, height/2, width, height/2, 0.1); -} - -void draw() { - background(255); - - // Draw water - liquid.display(); - - for (int i = 0; i < movers.length; i++) { - - // Is the Mover in the liquid? - if (liquid.contains(movers[i])) { - // Calculate drag force - PVector dragForce = liquid.drag(movers[i]); - // Apply drag force to Mover - movers[i].applyForce(dragForce); - } - - // Gravity is scaled by mass here! - PVector gravity = new PVector(0, 0.1*movers[i].mass); - // Apply gravity - movers[i].applyForce(gravity); - - // Update and display - movers[i].update(); - movers[i].display(); - movers[i].checkEdges(); - } - - fill(255); - //text("click mouse to reset",10,30); - - if (frameCount % 20 == 0) saveFrame("ch2_05_####.png"); -} - -void mousePressed() { - reset(); -} - -// Restart all the Mover objects randomly -void reset() { - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.5*2.25,3*2.25), 20*2.25+i*40*2.25, 0); - } -} - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/sketch.properties b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_5_fluidresistance_sequence/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/Attractor.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/Attractor.pde deleted file mode 100644 index 62115f590..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/Attractor.pde +++ /dev/null @@ -1,77 +0,0 @@ -// Attraction -// Daniel Shiffman - -// A class for a draggable attractive body in our world - -class Attractor { - float mass; // Mass, tied to size - float G; // Gravitational Constant - PVector location; // Location - boolean dragging = false; // Is the object being dragged? - boolean rollover = false; // Is the mouse over the ellipse? - PVector dragOffset; // holds the offset for when object is clicked on - - Attractor() { - location = new PVector(width/2,height/2); - mass = 20; - G = 1; - dragOffset = new PVector(0.0,0.0); - } - - PVector attract(Mover m) { - PVector force = PVector.sub(location,m.location); // Calculate direction of force - float d = force.mag(); // Distance between objects - d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - // Method to display - void display() { - ellipseMode(CENTER); - strokeWeight(4); - stroke(0); - if (dragging) fill (50); - else if (rollover) fill(100); - else fill(175,200); - ellipse(location.x,location.y,mass*2,mass*2); - } - - // The methods below are for mouse interaction - void clicked(int mx, int my) { - float d = dist(mx,my,location.x,location.y); - if (d < mass) { - dragging = true; - dragOffset.x = location.x-mx; - dragOffset.y = location.y-my; - } - } - - void hover(int mx, int my) { - float d = dist(mx,my,location.x,location.y); - if (d < mass) { - rollover = true; - } - else { - rollover = false; - } - } - - void stopDragging() { - dragging = false; - } - - - - void drag() { - if (dragging) { - location.x = mouseX + dragOffset.x; - location.y = mouseY + dragOffset.y; - } - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/Mover.pde deleted file mode 100644 index ec2069b99..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/Mover.pde +++ /dev/null @@ -1,51 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover() { - location = new PVector(400,50); - velocity = new PVector(1,0); - acceleration = new PVector(0,0); - mass = 1; - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x,location.y,16,16); - } - - void checkEdges() { - - if (location.x > width) { - location.x = 0; - } else if (location.x < 0) { - location.x = width; - } - - if (location.y > height) { - velocity.y *= -1; - location.y = height; - } - - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/NOC_2_6_attraction.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/NOC_2_6_attraction.pde deleted file mode 100644 index 320ddc5ba..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/NOC_2_6_attraction.pde +++ /dev/null @@ -1,36 +0,0 @@ -Mover m; -Attractor a; - -void setup() { - size(800,200); - smooth(); - m = new Mover(); - a = new Attractor(); -} - -void draw() { - background(255); - - PVector force = a.attract(m); - m.applyForce(force); - m.update(); - - a.drag(); - a.hover(mouseX,mouseY); - - a.display(); - m.display(); -} - -void mousePressed() { - a.clicked(mouseX,mouseY); -} - -void mouseReleased() { - a.stopDragging(); -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/sketch.properties b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/sketch.properties deleted file mode 100644 index 6d28cd598..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_6_attraction/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=JavaScript diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/Attractor.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/Attractor.pde deleted file mode 100644 index 62115f590..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/Attractor.pde +++ /dev/null @@ -1,77 +0,0 @@ -// Attraction -// Daniel Shiffman - -// A class for a draggable attractive body in our world - -class Attractor { - float mass; // Mass, tied to size - float G; // Gravitational Constant - PVector location; // Location - boolean dragging = false; // Is the object being dragged? - boolean rollover = false; // Is the mouse over the ellipse? - PVector dragOffset; // holds the offset for when object is clicked on - - Attractor() { - location = new PVector(width/2,height/2); - mass = 20; - G = 1; - dragOffset = new PVector(0.0,0.0); - } - - PVector attract(Mover m) { - PVector force = PVector.sub(location,m.location); // Calculate direction of force - float d = force.mag(); // Distance between objects - d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - // Method to display - void display() { - ellipseMode(CENTER); - strokeWeight(4); - stroke(0); - if (dragging) fill (50); - else if (rollover) fill(100); - else fill(175,200); - ellipse(location.x,location.y,mass*2,mass*2); - } - - // The methods below are for mouse interaction - void clicked(int mx, int my) { - float d = dist(mx,my,location.x,location.y); - if (d < mass) { - dragging = true; - dragOffset.x = location.x-mx; - dragOffset.y = location.y-my; - } - } - - void hover(int mx, int my) { - float d = dist(mx,my,location.x,location.y); - if (d < mass) { - rollover = true; - } - else { - rollover = false; - } - } - - void stopDragging() { - dragging = false; - } - - - - void drag() { - if (dragging) { - location.x = mouseX + dragOffset.x; - location.y = mouseY + dragOffset.y; - } - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/Mover.pde deleted file mode 100644 index 91712f09d..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/Mover.pde +++ /dev/null @@ -1,33 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x, float y) { - mass = m; - location = new PVector(random(width), random(height)); - velocity = new PVector(1, 0); - acceleration = new PVector(0, 0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force, mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(0,100); - ellipse(location.x, location.y, mass*25, mass*25); - } -} - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/NOC_2_7_attraction_many.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/NOC_2_7_attraction_many.pde deleted file mode 100644 index a213c79be..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_7_attraction_many/NOC_2_7_attraction_many.pde +++ /dev/null @@ -1,46 +0,0 @@ -Mover[] movers = new Mover[10]; - -Attractor a; - -void setup() { - size(800, 200); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.1, 2), random(width), random(height)); - } - a = new Attractor(); - } - -void draw() { - background(255); - - a.display(); - a.drag(); - a.hover(mouseX, mouseY); - - for (int i = 0; i < movers.length; i++) { - PVector force = a.attract(movers[i]); - movers[i].applyForce(force); - - movers[i].update(); - movers[i].display(); - } -} - -void mousePressed() { - a.clicked(mouseX, mouseY); -} - -void mouseReleased() { - a.stopDragging(); -} - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/Mover.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/Mover.pde deleted file mode 100644 index 30c49d701..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/Mover.pde +++ /dev/null @@ -1,48 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - Mover(float m, float x, float y) { - mass = m; - location = new PVector(x, y); - velocity = new PVector(0, 0); - acceleration = new PVector(0, 0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force, mass); - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(0, 100); - ellipse(location.x, location.y, mass*24, mass*24); - } - - PVector attract(Mover m) { - PVector force = PVector.sub(location, m.location); // Calculate direction of force - float distance = force.mag(); // Distance between objects - distance = constrain(distance, 5.0, 25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction - - float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - -} - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/NOC_2_8_mutual_attraction.pde b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/NOC_2_8_mutual_attraction.pde deleted file mode 100644 index 905140372..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/NOC_2_8_mutual_attraction.pde +++ /dev/null @@ -1,42 +0,0 @@ -Mover[] movers = new Mover[20]; - -float g = 0.4; - -void setup() { - size(800,200); - smooth(); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.1,2),random(width),random(height)); - } -} - -void draw() { - background(255); - - - for (int i = 0; i < movers.length; i++) { - for (int j = 0; j < movers.length; j++) { - if (i != j) { - PVector force = movers[j].attract(movers[i]); - movers[i].applyForce(force); - } - } - - movers[i].update(); - movers[i].display(); - } - -} - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/sketch.properties b/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/sketch.properties deleted file mode 100644 index 6d28cd598..000000000 --- a/java/examples/Books/Nature of Code/chp2_forces/NOC_2_8_mutual_attraction/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=JavaScript diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/AdditiveWave/AdditiveWave.pde b/java/examples/Books/Nature of Code/chp3_oscillation/AdditiveWave/AdditiveWave.pde deleted file mode 100644 index 888f4b142..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/AdditiveWave/AdditiveWave.pde +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Additive Wave - * by Daniel Shiffman. - * - * Create a more complex wave by adding two waves together. - */ - -// Maybe better for this answer to be OOP??? - -int xspacing = 8; // How far apart should each horizontal location be spaced -int w; // Width of entire wave -int maxwaves = 5; // total # of waves to add together - -float theta = 0.0; -float[] amplitude = new float[maxwaves]; // Height of wave -float[] dx = new float[maxwaves]; // Value for incrementing X, to be calculated as a function of period and xspacing -float[] yvalues; // Using an array to store height values for the wave (not entirely necessary) - -void setup() { - size(640,360); - colorMode(RGB, 255, 255, 255, 100); - smooth(); - w = width + 16; - - for (int i = 0; i < maxwaves; i++) { - amplitude[i] = random(10,30); - float period = random(100,300); // How many pixels before the wave repeats - dx[i] = (TWO_PI / period) * xspacing; - } - - yvalues = new float[w/xspacing]; -} - -void draw() { - background(0); - calcWave(); - renderWave(); -} - -void calcWave() { - // Increment theta (try different values for 'angular velocity' here - theta += 0.02; - - // Set all height values to zero - for (int i = 0; i < yvalues.length; i++) { - yvalues[i] = 0; - } - - // Accumulate wave height values - for (int j = 0; j < maxwaves; j++) { - float x = theta; - for (int i = 0; i < yvalues.length; i++) { - // Every other wave is cosine instead of sine - if (j % 2 == 0) yvalues[i] += sin(x)*amplitude[j]; - else yvalues[i] += cos(x)*amplitude[j]; - x+=dx[j]; - } - } -} - -void renderWave() { - // A simple way to draw the wave with an ellipse at each location - noStroke(); - fill(255,50); - ellipseMode(CENTER); - for (int x = 0; x < yvalues.length; x++) { - ellipse(x*xspacing,height/2+yvalues[x],16,16); - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/AttractionArrayWithOscillation.pde b/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/AttractionArrayWithOscillation.pde deleted file mode 100644 index abfa74f4e..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/AttractionArrayWithOscillation.pde +++ /dev/null @@ -1,46 +0,0 @@ -// Attraction Array with Oscillating objects around each Crawler -// Daniel Shiffman -// Nature of Code, Spring 2012 - -// Click and drag attractive body to move throughout space - -Crawler[] crawlers = new Crawler[6]; -Attractor a; - -void setup() { - size(640,360); - smooth(); - - // Some random bodies - for (int i = 0; i < crawlers.length; i++) { - crawlers[i] = new Crawler(); - } - // Create an attractive body - a = new Attractor(new PVector(width/2,height/2),20,0.4); -} - -void draw() { - background(255); - a.rollover(mouseX,mouseY); - a.go(); - - for (int i = 0; i < crawlers.length; i++) { - // Calculate a force exerted by "attractor" on "Crawler" - PVector f = a.attract(crawlers[i]); - // Apply that force to the Crawler - crawlers[i].applyForce(f); - // Update and render - crawlers[i].update(); - crawlers[i].display(); - } - - -} - -void mousePressed() { - a.clicked(mouseX,mouseY); -} - -void mouseReleased() { - a.stopDragging(); -} diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Attractor.pde b/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Attractor.pde deleted file mode 100644 index b725c771a..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Attractor.pde +++ /dev/null @@ -1,81 +0,0 @@ -// Attraction -// Daniel Shiffman -// Nature of Code, Spring 2009 - - -// A class for a draggable attractive body in our world - -class Attractor { - float mass; // Mass, tied to size - float G; // Gravitational Constant - PVector loc; // Location - boolean dragging = false; // Is the object being dragged? - boolean rollover = false; // Is the mouse over the ellipse? - PVector drag; // holds the offset for when object is clicked on - - Attractor(PVector l_,float m_, float g_) { - loc = l_.get(); - mass = m_; - G = g_; - drag = new PVector(0.0,0.0); - } - - void go() { - render(); - drag(); - } - - PVector attract(Crawler c) { - PVector dir = PVector.sub(loc,c.loc); // Calculate direction of force - float d = dir.mag(); // Distance between objects - d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - dir.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - float force = (G * mass * c.mass) / (d * d); // Calculate gravitional force magnitude - dir.mult(force); // Get force vector --> magnitude * direction - return dir; - } - - // Method to display - void render() { - ellipseMode(CENTER); - stroke(0,100); - if (dragging) fill (50); - else if (rollover) fill(100); - else fill(175,50); - ellipse(loc.x,loc.y,mass*2,mass*2); - } - - // The methods below are for mouse interaction - void clicked(int mx, int my) { - float d = dist(mx,my,loc.x,loc.y); - if (d < mass) { - dragging = true; - drag.x = loc.x-mx; - drag.y = loc.y-my; - } - } - - void rollover(int mx, int my) { - float d = dist(mx,my,loc.x,loc.y); - if (d < mass) { - rollover = true; - } else { - rollover = false; - } - } - - void stopDragging() { - dragging = false; - } - - - - void drag() { - if (dragging) { - loc.x = mouseX + drag.x; - loc.y = mouseY + drag.y; - } - } - -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Crawler.pde b/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Crawler.pde deleted file mode 100644 index 35ae63222..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Crawler.pde +++ /dev/null @@ -1,56 +0,0 @@ -// Attraction -// Daniel Shiffman -// Nature of Code, Spring 2012 - -// A class to describe a thing in our world, has vectors for location, velocity, and acceleration -// Also includes scalar values for mass, maximum velocity, and elasticity - -class Crawler { - PVector loc; - PVector vel; - PVector acc; - float mass; - - Oscillator osc; - - Crawler() { - acc = new PVector(); - vel = new PVector(random(-1,1),random(-1,1)); - loc = new PVector(random(width),random(height)); - mass = random(8,16); - osc = new Oscillator(mass*2); - } - - void applyForce(PVector force) { - PVector f = force.get(); - f.div(mass); - acc.add(f); - } - - // Method to update location - void update() { - vel.add(acc); - loc.add(vel); - // Multiplying by 0 sets the all the components to 0 - acc.mult(0); - - osc.update(vel.mag()/10); - } - - // Method to display - void display() { - float angle = vel.heading2D(); - pushMatrix(); - translate(loc.x,loc.y); - rotate(angle); - ellipseMode(CENTER); - stroke(0); - fill(175,100); - ellipse(0,0,mass*2,mass*2); - - osc.display(loc); - popMatrix(); - - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Oscillator.pde b/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Oscillator.pde deleted file mode 100644 index 624156fbf..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/AttractionArrayWithOscillation/Oscillator.pde +++ /dev/null @@ -1,35 +0,0 @@ -// Attraction Array with Oscillating objects around each thing -// Daniel Shiffman -// Nature of Code, Spring 2009 - -class Oscillator { - - // Because we are going to oscillate along the x and y axis we can use PVector for two angles, amplitudes, etc.! - float theta; - float amplitude; - - Oscillator(float r) { - - // Initialize randomly - theta = 0; - amplitude = r; - - } - - // Update theta and offset - void update(float thetaVel) { - theta += thetaVel; - } - - // Display based on a location - void display(PVector loc) { - float x = map(cos(theta),-1,1,0,amplitude); - - stroke(0); - fill(50); - line(0,0,x,0); - ellipse(x,0,8,8); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_01_exercise_baton/Exercise_3_01_exercise_baton.pde b/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_01_exercise_baton/Exercise_3_01_exercise_baton.pde deleted file mode 100644 index 3eadcc85e..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_01_exercise_baton/Exercise_3_01_exercise_baton.pde +++ /dev/null @@ -1,24 +0,0 @@ -float angle = 0; - -void setup() { - size(750, 150); - smooth(); -} - -void draw() { - background(255); - - fill(127); - stroke(0); - rectMode(CENTER); - translate(width/2, height/2); - rotate(angle); - line(-50, 0, 50, 0); - stroke(0); - strokeWeight(2); - fill(127); - ellipse(50, 0, 16, 16); - ellipse(-50, 0, 16, 16); - angle += 0.05; -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_04_spiral/Exercise_3_04_spiral.pde b/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_04_spiral/Exercise_3_04_spiral.pde deleted file mode 100644 index 94611d5b0..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_04_spiral/Exercise_3_04_spiral.pde +++ /dev/null @@ -1,30 +0,0 @@ -// Nature of Code -// Daniel Shiffman -// http://www.learningprocessing.com - -// A Polar coordinate, radius now starts at 0 to spiral outwards -float r = 0; -float theta = 0; - -void setup() { - size(750,200); - background(255); - smooth(); -} - -void draw() { - // Polar to Cartesian conversion - float x = r * cos(theta); - float y = r * sin(theta); - - // Draw an ellipse at x,y - noStroke(); - fill(0); - // Adjust for center of window - ellipse(x+width/2, y+height/2, 16, 16); - - // Increment the angle - theta += 0.01; - // Increment the radius - r += 0.05; -} diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_05_asteroids/Exercise_3_05_asteroids.pde b/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_05_asteroids/Exercise_3_05_asteroids.pde deleted file mode 100644 index 239de3fd2..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_05_asteroids/Exercise_3_05_asteroids.pde +++ /dev/null @@ -1,41 +0,0 @@ -// Nature of Code 2011 -// Daniel Shiffman -// Chapter 3: Asteroids exercise -// http://www.shiffman.net - -// Mover object -Spaceship ship; - -void setup() { - size(750, 200); - smooth(); - ship = new Spaceship(); -} - -void draw() { - background(255); - - // Update location - ship.update(); - // Wrape edges - ship.wrapEdges(); - // Draw ship - ship.display(); - - - fill(0); - text("left right arrows to turn, z to thrust",10,height-5); - - // Turn or thrust the ship depending on what key is pressed - if (keyPressed) { - if (key == CODED && keyCode == LEFT) { - ship.turn(-0.03); - } else if (key == CODED && keyCode == RIGHT) { - ship.turn(0.03); - } else if (key == 'z' || key == 'Z') { - ship.thrust(); - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_05_asteroids/Spaceship.pde b/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_05_asteroids/Spaceship.pde deleted file mode 100644 index 1910c7243..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_05_asteroids/Spaceship.pde +++ /dev/null @@ -1,97 +0,0 @@ -// Nature of Code 2011 -// Daniel Shiffman -// Chapter 3: Asteroids - -class Spaceship { - // All of our regular motion stuff - PVector location; - PVector velocity; - PVector acceleration; - - // Arbitrary damping to slow down ship - float damping = 0.995; - float topspeed = 6; - - // Variable for heading! - float heading = 0; - - // Size - float r = 16; - - // Are we thrusting (to color boosters) - boolean thrusting = false; - - Spaceship() { - location = new PVector(width/2,height/2); - velocity = new PVector(); - acceleration = new PVector(); - } - - // Standard Euler integration - void update() { - velocity.add(acceleration); - velocity.mult(damping); - velocity.limit(topspeed); - location.add(velocity); - acceleration.mult(0); - } - - // Newton's law: F = M * A - void applyForce(PVector force) { - PVector f = force.get(); - //f.div(mass); // ignoring mass right now - acceleration.add(f); - } - - // Turn changes angle - void turn(float a) { - heading += a; - } - - // Apply a thrust force - void thrust() { - // Offset the angle since we drew the ship vertically - float angle = heading - PI/2; - // Polar to cartesian for force vector! - PVector force = new PVector(cos(angle),sin(angle)); - force.mult(0.1); - applyForce(force); - // To draw booster - thrusting = true; - } - - void wrapEdges() { - float buffer = r*2; - if (location.x > width + buffer) location.x = -buffer; - else if (location.x < -buffer) location.x = width+buffer; - if (location.y > height + buffer) location.y = -buffer; - else if (location.y < -buffer) location.y = height+buffer; - } - - - // Draw the ship - void display() { - stroke(0); - strokeWeight(2); - pushMatrix(); - translate(location.x,location.y+r); - rotate(heading); - fill(175); - if (thrusting) fill(255,0,0); - // Booster rockets - rect(-r/2,r,r/3,r/2); - rect(r/2,r,r/3,r/2); - fill(175); - // A triangle - beginShape(); - vertex(-r,r); - vertex(0,-r); - vertex(r,r); - endShape(CLOSE); - rectMode(CENTER); - popMatrix(); - - thrusting = false; - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_10_OOPWave/Exercise_3_10_OOPWave.pde b/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_10_OOPWave/Exercise_3_10_OOPWave.pde deleted file mode 100644 index be26ff0f4..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_10_OOPWave/Exercise_3_10_OOPWave.pde +++ /dev/null @@ -1,31 +0,0 @@ -// Sine Wave -// Daniel Shiffman - - -// Two wave objects -Wave wave0; -Wave wave1; - -void setup() { - size(750,200); - smooth(); - // Initialize a wave with starting point, width, amplitude, and period - wave0 = new Wave(new PVector(50,75),100,20,500); - wave1 = new Wave(new PVector(300,100),300,40,220); - -} - -void draw() { - background(255); - - // Update and display waves - wave0.calculate(); - wave0.display(); - - wave1.calculate(); - wave1.display(); - - -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_10_OOPWave/Wave.pde b/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_10_OOPWave/Wave.pde deleted file mode 100644 index 1ecf976c0..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_10_OOPWave/Wave.pde +++ /dev/null @@ -1,45 +0,0 @@ -class Wave { - - int xspacing = 8; // How far apart should each horizontal location be spaced - int w; // Width of entire wave - - PVector origin; // Where does the wave's first point start - float theta = 0.0; // Start angle at 0 - float amplitude; // Height of wave - float period; // How many pixels before the wave repeats - float dx; // Value for incrementing X, to be calculated as a function of period and xspacing - float[] yvalues; // Using an array to store height values for the wave (not entirely necessary) - - Wave(PVector o, int w_, float a, float p) { - origin = o.get(); - w = w_; - period = p; - amplitude = a; - dx = (TWO_PI / period) * xspacing; - yvalues = new float[w/xspacing]; - } - - - void calculate() { - // Increment theta (try different values for 'angular velocity' here - theta += 0.02; - - // For every x value, calculate a y value with sine function - float x = theta; - for (int i = 0; i < yvalues.length; i++) { - yvalues[i] = sin(x)*amplitude; - x+=dx; - } - } - - void display() { - // A simple way to draw the wave with an ellipse at each location - for (int x = 0; x < yvalues.length; x++) { - stroke(0); - fill(0,50); - ellipseMode(CENTER); - ellipse(origin.x+x*xspacing,origin.y+yvalues[x],48,48); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_11_AdditiveWave/Exercise_3_11_AdditiveWave.pde b/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_11_AdditiveWave/Exercise_3_11_AdditiveWave.pde deleted file mode 100644 index 316721bef..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/Exercise_3_11_AdditiveWave/Exercise_3_11_AdditiveWave.pde +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Additive Wave - * by Daniel Shiffman. - * - * Create a more complex wave by adding two waves together. - */ - -// Maybe better for this answer to be OOP??? - -int xspacing = 8; // How far apart should each horizontal location be spaced -int w; // Width of entire wave -int maxwaves = 5; // total # of waves to add together - -float theta = 0.0; -float[] amplitude = new float[maxwaves]; // Height of wave -float[] dx = new float[maxwaves]; // Value for incrementing X, to be calculated as a function of period and xspacing -float[] yvalues; // Using an array to store height values for the wave (not entirely necessary) - -void setup() { - size(750,200); - smooth(); - w = width + 16; - - for (int i = 0; i < maxwaves; i++) { - amplitude[i] = random(10,30); - float period = random(100,300); // How many pixels before the wave repeats - dx[i] = (TWO_PI / period) * xspacing; - } - - yvalues = new float[w/xspacing]; -} - -void draw() { - background(255); - calcWave(); - renderWave(); -} - -void calcWave() { - // Increment theta (try different values for 'angular velocity' here - theta += 0.02; - - // Set all height values to zero - for (int i = 0; i < yvalues.length; i++) { - yvalues[i] = 0; - } - - // Accumulate wave height values - for (int j = 0; j < maxwaves; j++) { - float x = theta; - for (int i = 0; i < yvalues.length; i++) { - // Every other wave is cosine instead of sine - if (j % 2 == 0) yvalues[i] += sin(x)*amplitude[j]; - else yvalues[i] += cos(x)*amplitude[j]; - x+=dx[j]; - } - } -} - -void renderWave() { - // A simple way to draw the wave with an ellipse at each location - stroke(0); - fill(127,50); - ellipseMode(CENTER); - for (int x = 0; x < yvalues.length; x++) { - ellipse(x*xspacing,height/2+yvalues[x],48,48); - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_03spring_exercise_sine/NOC_03spring_exercise_sine.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_03spring_exercise_sine/NOC_03spring_exercise_sine.pde deleted file mode 100644 index 26ba2c139..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_03spring_exercise_sine/NOC_03spring_exercise_sine.pde +++ /dev/null @@ -1,21 +0,0 @@ -float angle = 0; -float aVelocity = 0.05; - -void setup() { - size(640,360); - smooth(); -} - -void draw() { - background(255); - - float x = width/2; - float y = map(sin(angle),-1,1,50,250); - angle += aVelocity; - - ellipseMode(CENTER); - stroke(0); - fill(175); - line(x,0,x,y); - ellipse(x,y,20,20); -} diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_01_angular_motion/NOC_3_01_angular_motion.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_01_angular_motion/NOC_3_01_angular_motion.pde deleted file mode 100644 index f88335f6b..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_01_angular_motion/NOC_3_01_angular_motion.pde +++ /dev/null @@ -1,30 +0,0 @@ -float angle = 0; -float aVelocity = 0; -float aAcceleration = 0.0001; - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - - - fill(127); - stroke(0); - - translate(width/2, height/2); - rectMode(CENTER); - rotate(angle); - stroke(0); - strokeWeight(2); - fill(127); - line(-60, 0, 60, 0); - ellipse(60, 0, 16, 16); - ellipse(-60, 0, 16, 16); - - angle += aVelocity; - aVelocity += aAcceleration; -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_01_angular_motion_trail/NOC_3_01_angular_motion_trail.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_01_angular_motion_trail/NOC_3_01_angular_motion_trail.pde deleted file mode 100644 index c33166cf9..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_01_angular_motion_trail/NOC_3_01_angular_motion_trail.pde +++ /dev/null @@ -1,35 +0,0 @@ -float angle = 0; -float aVelocity = 0; -float aAcceleration = 0.0001; - -void setup() { - size(800, 200); - background(255); - smooth(); -} - -void draw() { - //background(255); - rectMode(CORNER); - noStroke(); - fill(255,5); - rect(0,0,width,height); - - - fill(127); - stroke(0); - - translate(width/2, height/2); - rectMode(CENTER); - rotate(angle); - stroke(0); - strokeWeight(2); - fill(127); - line(-60, 0, 60, 0); - ellipse(60, 0, 16, 16); - ellipse(-60, 0, 16, 16); - - angle += aVelocity; - aVelocity += aAcceleration; -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/Attractor.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/Attractor.pde deleted file mode 100644 index 56628fdf1..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/Attractor.pde +++ /dev/null @@ -1,36 +0,0 @@ -// Attraction -// Daniel Shiffman - -// A class for a draggable attractive body in our world - -class Attractor { - float mass; // Mass, tied to size - PVector location; // Location - float g; - - Attractor() { - location = new PVector(width/2, height/2); - mass = 20; - g = 0.4; - } - - - PVector attract(Mover m) { - PVector force = PVector.sub(location, m.location); // Calculate direction of force - float distance = force.mag(); // Distance between objects - distance = constrain(distance, 5.0, 25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - // Method to display - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x, location.y, 48, 48); - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/Mover.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/Mover.pde deleted file mode 100644 index 1ff6e5750..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/Mover.pde +++ /dev/null @@ -1,49 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - float angle = 0; - float aVelocity = 0; - float aAcceleration = 0; - - Mover(float m, float x, float y) { - mass = m; - location = new PVector(x,y); - velocity = new PVector(random(-1,1),random(-1,1)); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - - velocity.add(acceleration); - location.add(velocity); - - aAcceleration = acceleration.x / 10.0; - aVelocity += aAcceleration; - aVelocity = constrain(aVelocity,-0.1,0.1); - angle += aVelocity; - - acceleration.mult(0); - } - - void display() { - stroke(0); - fill(175,200); - rectMode(CENTER); - pushMatrix(); - translate(location.x,location.y); - rotate(angle); - rect(0,0,mass*16,mass*16); - popMatrix(); - } - -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/NOC_3_02_forces_angular_mot_trails.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/NOC_3_02_forces_angular_mot_trails.pde deleted file mode 100644 index ec850608d..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_mot_trails/NOC_3_02_forces_angular_mot_trails.pde +++ /dev/null @@ -1,44 +0,0 @@ -Mover[] movers = new Mover[20]; - -Attractor a; - -void setup() { - size(800,200); - smooth(); - background(255); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.1,2),random(width),random(height)); - } - a = new Attractor(); -} - -void draw() { - //background(255); - - rectMode(CORNER); - noStroke(); - fill(255,5); - rect(0,0,width,height); - - a.display(); - - for (int i = 0; i < movers.length; i++) { - PVector force = a.attract(movers[i]); - movers[i].applyForce(force); - - movers[i].update(); - movers[i].display(); - } - -} - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/Attractor.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/Attractor.pde deleted file mode 100644 index 56628fdf1..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/Attractor.pde +++ /dev/null @@ -1,36 +0,0 @@ -// Attraction -// Daniel Shiffman - -// A class for a draggable attractive body in our world - -class Attractor { - float mass; // Mass, tied to size - PVector location; // Location - float g; - - Attractor() { - location = new PVector(width/2, height/2); - mass = 20; - g = 0.4; - } - - - PVector attract(Mover m) { - PVector force = PVector.sub(location, m.location); // Calculate direction of force - float distance = force.mag(); // Distance between objects - distance = constrain(distance, 5.0, 25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects - force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - float strength = (g * mass * m.mass) / (distance * distance); // Calculate gravitional force magnitude - force.mult(strength); // Get force vector --> magnitude * direction - return force; - } - - // Method to display - void display() { - stroke(0); - strokeWeight(2); - fill(127); - ellipse(location.x, location.y, 48, 48); - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/Mover.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/Mover.pde deleted file mode 100644 index 1ff6e5750..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/Mover.pde +++ /dev/null @@ -1,49 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float mass; - - float angle = 0; - float aVelocity = 0; - float aAcceleration = 0; - - Mover(float m, float x, float y) { - mass = m; - location = new PVector(x,y); - velocity = new PVector(random(-1,1),random(-1,1)); - acceleration = new PVector(0,0); - } - - void applyForce(PVector force) { - PVector f = PVector.div(force,mass); - acceleration.add(f); - } - - void update() { - - velocity.add(acceleration); - location.add(velocity); - - aAcceleration = acceleration.x / 10.0; - aVelocity += aAcceleration; - aVelocity = constrain(aVelocity,-0.1,0.1); - angle += aVelocity; - - acceleration.mult(0); - } - - void display() { - stroke(0); - fill(175,200); - rectMode(CENTER); - pushMatrix(); - translate(location.x,location.y); - rotate(angle); - rect(0,0,mass*16,mass*16); - popMatrix(); - } - -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/NOC_3_02_forces_angular_motion.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/NOC_3_02_forces_angular_motion.pde deleted file mode 100644 index c5c454ea5..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_02_forces_angular_motion/NOC_3_02_forces_angular_motion.pde +++ /dev/null @@ -1,39 +0,0 @@ -Mover[] movers = new Mover[20]; - -Attractor a; - -void setup() { - size(800,200); - smooth(); - background(255); - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(0.1,2),random(width),random(height)); - } - a = new Attractor(); -} - -void draw() { - background(255); - - a.display(); - - for (int i = 0; i < movers.length; i++) { - PVector force = a.attract(movers[i]); - movers[i].applyForce(force); - - movers[i].update(); - movers[i].display(); - } - -} - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity/Mover.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity/Mover.pde deleted file mode 100644 index b3f9f770b..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity/Mover.pde +++ /dev/null @@ -1,65 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float topspeed; - - float xoff, yoff; - - float r = 16; - - Mover() { - location = new PVector(width/2, height/2); - velocity = new PVector(0, 0); - topspeed = 4; - xoff = 1000; - yoff = 0; - } - - void update() { - - PVector mouse = new PVector(mouseX, mouseY); - PVector dir = PVector.sub(mouse, location); - dir.normalize(); - dir.mult(0.5); - acceleration = dir; - - velocity.add(acceleration); - velocity.limit(topspeed); - location.add(velocity); - } - - void display() { - float theta = velocity.heading2D(); - - stroke(0); - strokeWeight(2); - fill(127); - pushMatrix(); - rectMode(CENTER); - translate(location.x, location.y); - rotate(theta); - rect(0, 0, 30, 10); - popMatrix(); - } - - void checkEdges() { - - if (location.x > width) { - location.x = 0; - } - else if (location.x < 0) { - location.x = width; - } - - if (location.y > height) { - location.y = 0; - } - else if (location.y < 0) { - location.y = height; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity/NOC_3_03_pointing_velocity.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity/NOC_3_03_pointing_velocity.pde deleted file mode 100644 index eb3d55ee4..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity/NOC_3_03_pointing_velocity.pde +++ /dev/null @@ -1,17 +0,0 @@ -Mover mover; - -void setup() { - size(800,200); - smooth(); - mover = new Mover(); -} - -void draw() { - background(255); - - mover.update(); - mover.checkEdges(); - mover.display(); -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity_trail/Mover.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity_trail/Mover.pde deleted file mode 100644 index b3f9f770b..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity_trail/Mover.pde +++ /dev/null @@ -1,65 +0,0 @@ -class Mover { - - PVector location; - PVector velocity; - PVector acceleration; - float topspeed; - - float xoff, yoff; - - float r = 16; - - Mover() { - location = new PVector(width/2, height/2); - velocity = new PVector(0, 0); - topspeed = 4; - xoff = 1000; - yoff = 0; - } - - void update() { - - PVector mouse = new PVector(mouseX, mouseY); - PVector dir = PVector.sub(mouse, location); - dir.normalize(); - dir.mult(0.5); - acceleration = dir; - - velocity.add(acceleration); - velocity.limit(topspeed); - location.add(velocity); - } - - void display() { - float theta = velocity.heading2D(); - - stroke(0); - strokeWeight(2); - fill(127); - pushMatrix(); - rectMode(CENTER); - translate(location.x, location.y); - rotate(theta); - rect(0, 0, 30, 10); - popMatrix(); - } - - void checkEdges() { - - if (location.x > width) { - location.x = 0; - } - else if (location.x < 0) { - location.x = width; - } - - if (location.y > height) { - location.y = 0; - } - else if (location.y < 0) { - location.y = height; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity_trail/NOC_3_03_pointing_velocity_trail.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity_trail/NOC_3_03_pointing_velocity_trail.pde deleted file mode 100644 index d0d7f776e..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_03_pointing_velocity_trail/NOC_3_03_pointing_velocity_trail.pde +++ /dev/null @@ -1,25 +0,0 @@ -Mover mover; - -void setup() { - size(800,200); - background(255); - smooth(); - mover = new Mover(); -} - -void draw() { - if (mousePressed) { - //background(255); - rectMode(CORNER); - noStroke(); - fill(255,5); - rect(0,0,width,height); - - mover.update(); - mover.checkEdges(); - mover.display(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_04_PolarToCartesian/NOC_3_04_PolarToCartesian.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_04_PolarToCartesian/NOC_3_04_PolarToCartesian.pde deleted file mode 100644 index 070f60e14..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_04_PolarToCartesian/NOC_3_04_PolarToCartesian.pde +++ /dev/null @@ -1,50 +0,0 @@ -/** - * PolarToCartesian - * by Daniel Shiffman. - * - * Convert a polar coordinate (r,theta) to cartesian (x,y): - * x = r * cos(theta) - * y = r * sin(theta) - */ - -float r; -float theta; - - -void setup() { - size(800, 200); - smooth(); - - // Initialize all values - r = height * 0.45; - theta = 0; -} - -void draw() { - - background(255); - - // Translate the origin point to the center of the screen - translate(width/2, height/2); - - // Convert polar to cartesian - float x = r * cos(theta); - float y = r * sin(theta); - - // Draw the ellipse at the cartesian coordinate - ellipseMode(CENTER); - fill(127); - stroke(0); - strokeWeight(2); - line(0,0,x,y); - ellipse(x, y, 48, 48); - - // Increase the angle over time - theta += 0.02; - - -} - - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_04_PolarToCartesian_trail/NOC_3_04_PolarToCartesian_trail.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_04_PolarToCartesian_trail/NOC_3_04_PolarToCartesian_trail.pde deleted file mode 100644 index 6e8a2ca40..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_04_PolarToCartesian_trail/NOC_3_04_PolarToCartesian_trail.pde +++ /dev/null @@ -1,54 +0,0 @@ -/** - * PolarToCartesian - * by Daniel Shiffman. - * - * Convert a polar coordinate (r,theta) to cartesian (x,y): - * x = r * cos(theta) - * y = r * sin(theta) - */ - -float r; -float theta; - - -void setup() { - size(800, 200); - background(255); - smooth(); - - // Initialize all values - r = height * 0.45; - theta = 0; -} - -void draw() { - - //background(255); - noStroke(); - fill(255,5); - rect(0,0,width,height); - - // Translate the origin point to the center of the screen - translate(width/2, height/2); - - // Convert polar to cartesian - float x = r * cos(theta); - float y = r * sin(theta); - - // Draw the ellipse at the cartesian coordinate - ellipseMode(CENTER); - fill(127); - stroke(0); - strokeWeight(2); - line(0,0,x,y); - ellipse(x, y, 48, 48); - - // Increase the angle over time - theta += 0.02; - - -} - - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_05_simple_harmonic_mot_trail/NOC_3_05_simple_harmonic_mot_trail.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_05_simple_harmonic_mot_trail/NOC_3_05_simple_harmonic_mot_trail.pde deleted file mode 100644 index 3676ac9f8..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_05_simple_harmonic_mot_trail/NOC_3_05_simple_harmonic_mot_trail.pde +++ /dev/null @@ -1,20 +0,0 @@ -void setup() { - size(800,200); - background(255); -} - -void draw() { - noStroke(); - fill(255,5); - rect(0,0,width,height); - float period = 120; - float amplitude = 300; - // Calculating horizontal location according to formula for simple harmonic motion - float x = amplitude * cos(TWO_PI * frameCount / period); - stroke(0); - strokeWeight(2); - fill(127); - translate(width/2,height/2); - line(0,0,x,0); - ellipse(x,0,48,48); -} diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_05_simple_harmonic_motion/NOC_3_05_simple_harmonic_motion.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_05_simple_harmonic_motion/NOC_3_05_simple_harmonic_motion.pde deleted file mode 100644 index e03a0cb6b..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_05_simple_harmonic_motion/NOC_3_05_simple_harmonic_motion.pde +++ /dev/null @@ -1,18 +0,0 @@ -void setup() { - size(800,200); -} - -void draw() { - background(255); - - float period = 120; - float amplitude = 300; - // Calculating horizontal location according to formula for simple harmonic motion - float x = amplitude * cos(TWO_PI * frameCount / period); - stroke(0); - strokeWeight(2); - fill(127); - translate(width/2,height/2); - line(0,0,x,0); - ellipse(x,0,48,48); -} diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_06_simple_harmonic_motion/NOC_3_06_simple_harmonic_motion.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_06_simple_harmonic_motion/NOC_3_06_simple_harmonic_motion.pde deleted file mode 100644 index 1ce795598..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_06_simple_harmonic_motion/NOC_3_06_simple_harmonic_motion.pde +++ /dev/null @@ -1,23 +0,0 @@ -float angle = 0; -float aVelocity = 0.03; - -void setup() { - size(640,360); - - smooth(); -} - -void draw() { - background(255); - - float amplitude = 300; - float x = amplitude * cos(angle); - angle += aVelocity; - - ellipseMode(CENTER); - stroke(0); - fill(175); - translate(width/2,height/2); - line(0,0,x,0); - ellipse(x,0,20,20); -} diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects/NOC_3_07_oscillating_objects.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects/NOC_3_07_oscillating_objects.pde deleted file mode 100644 index 5f9403c8d..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects/NOC_3_07_oscillating_objects.pde +++ /dev/null @@ -1,33 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Exercise 13-6: Encapsulate Example 13-6 into an Oscillator object. Create an array -// of Oscillators, each moving at diff erent rates along the x and y axes. Here is some code for the -// Oscillator class to help you get started. - -// An array of objects -Oscillator[] oscillators = new Oscillator[10]; - -void setup() { - size(800,200); - smooth(); - // Initialize all objects - for (int i = 0; i < oscillators.length; i++) { - oscillators[i] = new Oscillator(); - } - background(255); -} - -void draw() { - background(255); - // Run all objects - for (int i = 0; i < oscillators.length; i++) { - oscillators[i].oscillate(); - oscillators[i].display(); - } -} - - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects/Oscillator.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects/Oscillator.pde deleted file mode 100644 index 3377681b6..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects/Oscillator.pde +++ /dev/null @@ -1,33 +0,0 @@ -class Oscillator { - - PVector angle; - PVector velocity; - PVector amplitude; - - Oscillator() { - angle = new PVector(); - velocity = new PVector(random(-0.05, 0.05), random(-0.05, 0.05)); - amplitude = new PVector(random(20,width/2), random(20,height/2)); - } - - void oscillate() { - angle.add(velocity); - } - - void display() { - - float x = sin(angle.x)*amplitude.x; - float y = sin(angle.y)*amplitude.y; - - pushMatrix(); - translate(width/2, height/2); - stroke(0); - strokeWeight(2); - fill(127,127); - line(0, 0, x, y); - ellipse(x, y, 32, 32); - popMatrix(); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects_trail/NOC_3_07_oscillating_objects_trail.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects_trail/NOC_3_07_oscillating_objects_trail.pde deleted file mode 100644 index 2d8c739b3..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects_trail/NOC_3_07_oscillating_objects_trail.pde +++ /dev/null @@ -1,36 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Exercise 13-6: Encapsulate Example 13-6 into an Oscillator object. Create an array -// of Oscillators, each moving at diff erent rates along the x and y axes. Here is some code for the -// Oscillator class to help you get started. - -// An array of objects -Oscillator[] oscillators = new Oscillator[10]; - -void setup() { - size(800,200); - smooth(); - // Initialize all objects - for (int i = 0; i < oscillators.length; i++) { - oscillators[i] = new Oscillator(); - } - background(255); -} - -void draw() { - rectMode(CORNER); - noStroke(); - fill(255,10); - rect(0,0,width,height); - // Run all objects - for (int i = 0; i < oscillators.length; i++) { - oscillators[i].oscillate(); - oscillators[i].display(); - } -} - - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects_trail/Oscillator.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects_trail/Oscillator.pde deleted file mode 100644 index 3377681b6..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_07_oscillating_objects_trail/Oscillator.pde +++ /dev/null @@ -1,33 +0,0 @@ -class Oscillator { - - PVector angle; - PVector velocity; - PVector amplitude; - - Oscillator() { - angle = new PVector(); - velocity = new PVector(random(-0.05, 0.05), random(-0.05, 0.05)); - amplitude = new PVector(random(20,width/2), random(20,height/2)); - } - - void oscillate() { - angle.add(velocity); - } - - void display() { - - float x = sin(angle.x)*amplitude.x; - float y = sin(angle.y)*amplitude.y; - - pushMatrix(); - translate(width/2, height/2); - stroke(0); - strokeWeight(2); - fill(127,127); - line(0, 0, x, y); - ellipse(x, y, 32, 32); - popMatrix(); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_08_static_wave_lines/NOC_3_08_static_wave_lines.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_08_static_wave_lines/NOC_3_08_static_wave_lines.pde deleted file mode 100644 index 91a3d91d4..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_08_static_wave_lines/NOC_3_08_static_wave_lines.pde +++ /dev/null @@ -1,19 +0,0 @@ -float angle = 0; -float angleVel = 0.1; - -size(800,200); -background(255); -smooth(); - -stroke(0); -strokeWeight(2); -noFill(); - -beginShape(); -for (int x = 0; x <= width; x += 5) { - float y = map(sin(angle),-1,1,0,height); - vertex(x,y); - angle +=angleVel; -} -endShape(); - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_exercise_additive_wave/NOC_3_09_exercise_additive_wave.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_exercise_additive_wave/NOC_3_09_exercise_additive_wave.pde deleted file mode 100644 index 888f4b142..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_exercise_additive_wave/NOC_3_09_exercise_additive_wave.pde +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Additive Wave - * by Daniel Shiffman. - * - * Create a more complex wave by adding two waves together. - */ - -// Maybe better for this answer to be OOP??? - -int xspacing = 8; // How far apart should each horizontal location be spaced -int w; // Width of entire wave -int maxwaves = 5; // total # of waves to add together - -float theta = 0.0; -float[] amplitude = new float[maxwaves]; // Height of wave -float[] dx = new float[maxwaves]; // Value for incrementing X, to be calculated as a function of period and xspacing -float[] yvalues; // Using an array to store height values for the wave (not entirely necessary) - -void setup() { - size(640,360); - colorMode(RGB, 255, 255, 255, 100); - smooth(); - w = width + 16; - - for (int i = 0; i < maxwaves; i++) { - amplitude[i] = random(10,30); - float period = random(100,300); // How many pixels before the wave repeats - dx[i] = (TWO_PI / period) * xspacing; - } - - yvalues = new float[w/xspacing]; -} - -void draw() { - background(0); - calcWave(); - renderWave(); -} - -void calcWave() { - // Increment theta (try different values for 'angular velocity' here - theta += 0.02; - - // Set all height values to zero - for (int i = 0; i < yvalues.length; i++) { - yvalues[i] = 0; - } - - // Accumulate wave height values - for (int j = 0; j < maxwaves; j++) { - float x = theta; - for (int i = 0; i < yvalues.length; i++) { - // Every other wave is cosine instead of sine - if (j % 2 == 0) yvalues[i] += sin(x)*amplitude[j]; - else yvalues[i] += cos(x)*amplitude[j]; - x+=dx[j]; - } - } -} - -void renderWave() { - // A simple way to draw the wave with an ellipse at each location - noStroke(); - fill(255,50); - ellipseMode(CENTER); - for (int x = 0; x < yvalues.length; x++) { - ellipse(x*xspacing,height/2+yvalues[x],16,16); - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave/NOC_3_09_wave.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave/NOC_3_09_wave.pde deleted file mode 100644 index 5eb61b28b..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave/NOC_3_09_wave.pde +++ /dev/null @@ -1,28 +0,0 @@ - -float startAngle = 0; -float angleVel = 0.23; - -void setup() { - size(800,200); - smooth(); -} - -void draw() { - background(255); - - startAngle += 0.015; - float angle = startAngle; - - for (int x = 0; x <= width; x += 24) { - float y = map(sin(angle),-1,1,0,height); - stroke(0); - fill(0,50); - strokeWeight(2); - ellipse(x,y,48,48); - angle += angleVel; - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_a/NOC_3_09_wave_a.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_a/NOC_3_09_wave_a.pde deleted file mode 100644 index b4a96efba..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_a/NOC_3_09_wave_a.pde +++ /dev/null @@ -1,28 +0,0 @@ - -float startAngle = 0; -float angleVel = 0.05; - -void setup() { - size(250,200); - smooth(); -} - -void draw() { - background(255); - - startAngle += 0.015; - float angle = startAngle; - - for (int x = 0; x <= width; x += 24) { - float y = map(sin(angle),-1,1,0,height); - stroke(0); - fill(0,50); - strokeWeight(2); - ellipse(x,y,48,48); - angle += angleVel; - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_b/NOC_3_09_wave_b.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_b/NOC_3_09_wave_b.pde deleted file mode 100644 index 9749647db..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_b/NOC_3_09_wave_b.pde +++ /dev/null @@ -1,28 +0,0 @@ - -float startAngle = 0; -float angleVel = 0.2; - -void setup() { - size(250,200); - smooth(); -} - -void draw() { - background(255); - - startAngle += 0.015; - float angle = startAngle; - - for (int x = 0; x <= width; x += 24) { - float y = map(sin(angle),-1,1,0,height); - stroke(0); - fill(0,50); - strokeWeight(2); - ellipse(x,y,48,48); - angle += angleVel; - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_c/NOC_3_09_wave_c.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_c/NOC_3_09_wave_c.pde deleted file mode 100644 index 74349d4f4..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_09_wave_c/NOC_3_09_wave_c.pde +++ /dev/null @@ -1,28 +0,0 @@ - -float startAngle = 0; -float angleVel = 0.4; - -void setup() { - size(250,200); - smooth(); -} - -void draw() { - background(255); - - startAngle += 0.015; - float angle = startAngle; - - for (int x = 0; x <= width; x += 24) { - float y = map(sin(angle),-1,1,0,height); - stroke(0); - fill(0,50); - strokeWeight(2); - ellipse(x,y,48,48); - angle += angleVel; - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExample/NOC_3_10_PendulumExample.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExample/NOC_3_10_PendulumExample.pde deleted file mode 100644 index 2a455823a..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExample/NOC_3_10_PendulumExample.pde +++ /dev/null @@ -1,43 +0,0 @@ -// Pendulum -// Daniel Shiffman - -// A simple pendulum simulation -// Given a pendulum with an angle theta (0 being the pendulum at rest) and a radius r -// we can use sine to calculate the angular component of the gravitational force. - -// Gravity Force = Mass * Gravitational Constant; -// Pendulum Force = Gravity Force * sine(theta) -// Angular Acceleration = Pendulum Force / Mass = gravitational acceleration * sine(theta); - -// Note this is an ideal world scenario with no tension in the -// pendulum arm, a more realistic formula might be: -// Angular Acceleration = (g / R) * sine(theta) - -// For a more substantial explanation, visit: -// http://www.myphysicslab.com/pendulum1.html - -Pendulum p; - -void setup() { - size(800,200); - smooth(); - - // Make a new Pendulum with an origin location and armlength - p = new Pendulum(new PVector(width/2,0),175); - -} - -void draw() { - - background(255); - p.go(); -} - -void mousePressed() { - p.clicked(mouseX,mouseY); -} - -void mouseReleased() { - p.stopDragging(); -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExample/Pendulum.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExample/Pendulum.pde deleted file mode 100644 index 260f793b2..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExample/Pendulum.pde +++ /dev/null @@ -1,95 +0,0 @@ -// Pendulum -// Daniel Shiffman - -// A Simple Pendulum Class -// Includes functionality for user can click and drag the pendulum - -class Pendulum { - - PVector location; // Location of pendulum ball - PVector origin; // Location of arm origin - float r; // Length of arm - float angle; // Pendulum arm angle - float aVelocity; // Angle velocity - float aAcceleration; // Angle acceleration - - float ballr; // Ball radius - float damping; // Arbitary damping amount - - boolean dragging = false; - - // This constructor could be improved to allow a greater variety of pendulums - Pendulum(PVector origin_, float r_) { - // Fill all variables - origin = origin_.get(); - location = new PVector(); - r = r_; - angle = PI/4; - - aVelocity = 0.0; - aAcceleration = 0.0; - damping = 0.995; // Arbitrary damping - ballr = 48.0; // Arbitrary ball radius - } - - void go() { - update(); - drag(); //for user interaction - display(); - } - - // Function to update location - void update() { - // As long as we aren't dragging the pendulum, let it swing! - if (!dragging) { - float gravity = 0.4; // Arbitrary constant - aAcceleration = (-1 * gravity / r) * sin(angle); // Calculate acceleration (see: http://www.myphysicslab.com/pendulum1.html) - aVelocity += aAcceleration; // Increment velocity - aVelocity *= damping; // Arbitrary damping - angle += aVelocity; // Increment angle - } - } - - void display() { - location.set(r*sin(angle), r*cos(angle), 0); // Polar to cartesian conversion - location.add(origin); // Make sure the location is relative to the pendulum's origin - - stroke(0); - strokeWeight(2); - // Draw the arm - line(origin.x, origin.y, location.x, location.y); - ellipseMode(CENTER); - fill(175); - if (dragging) fill(0); - // Draw the ball - ellipse(location.x, location.y, ballr, ballr); - } - - - // The methods below are for mouse interaction - - // This checks to see if we clicked on the pendulum ball - void clicked(int mx, int my) { - float d = dist(mx, my, location.x, location.y); - if (d < ballr) { - dragging = true; - } - } - - // This tells us we are not longer clicking on the ball - void stopDragging() { - aVelocity = 0; // No velocity once you let go - dragging = false; - } - - void drag() { - // If we are draging the ball, we calculate the angle between the - // pendulum origin and mouse location - // we assign that angle to the pendulum - if (dragging) { - PVector diff = PVector.sub(origin, new PVector(mouseX, mouseY)); // Difference between 2 points - angle = atan2(-1*diff.y, diff.x) - radians(90); // Angle relative to vertical axis - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExampleSimplified/NOC_3_10_PendulumExampleSimplified.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExampleSimplified/NOC_3_10_PendulumExampleSimplified.pde deleted file mode 100644 index b40d7fea8..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExampleSimplified/NOC_3_10_PendulumExampleSimplified.pde +++ /dev/null @@ -1,34 +0,0 @@ -// Pendulum -// Daniel Shiffman - -// A simple pendulum simulation -// Given a pendulum with an angle theta (0 being the pendulum at rest) and a radius r -// we can use sine to calculate the angular component of the gravitational force. - -// Gravity Force = Mass * Gravitational Constant; -// Pendulum Force = Gravity Force * sine(theta) -// Angular Acceleration = Pendulum Force / Mass = Gravitational Constant * sine(theta); - -// Note this is an ideal world scenario with no tension in the -// pendulum arm, a more realistic formula might be: -// Angular Acceleration = (G / R) * sine(theta) - -// For a more substantial explanation, visit: -// http://www.myphysicslab.com/pendulum1.html - -Pendulum p; - -void setup() { - size(800,200); - smooth(); - - // Make a new Pendulum with an origin location and armlength - p = new Pendulum(new PVector(width/2,0),175); - -} - -void draw() { - background(255); - p.go(); -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExampleSimplified/Pendulum.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExampleSimplified/Pendulum.pde deleted file mode 100644 index 72d1e8cf9..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_10_PendulumExampleSimplified/Pendulum.pde +++ /dev/null @@ -1,61 +0,0 @@ -// Pendulum -// Daniel Shiffman - -// A Simple Pendulum Class -// Includes functionality for user can click and drag the pendulum - -class Pendulum { - - PVector location; // Location of pendulum ball - PVector origin; // Location of arm origin - float r; // Length of arm - float angle; // Pendulum arm angle - float aVelocity; // Angle velocity - float aAcceleration; // Angle acceleration - float damping; // Arbitary damping amount - - // This constructor could be improved to allow a greater variety of pendulums - Pendulum(PVector origin_, float r_) { - // Fill all variables - origin = origin_.get(); - location = new PVector(); - r = r_; - angle = PI/4; - - aVelocity = 0.0; - aAcceleration = 0.0; - damping = 0.995; // Arbitrary damping - } - - void go() { - update(); - display(); - } - - // Function to update location - void update() { - float gravity = 0.4; // Arbitrary constant - aAcceleration = (-1 * gravity / r) * sin(angle); // Calculate acceleration (see: http://www.myphysicslab.com/pendulum1.html) - aVelocity += aAcceleration; // Increment velocity - aVelocity *= damping; // Arbitrary damping - angle += aVelocity; // Increment angle - } - - void display() { - location.set(r*sin(angle), r*cos(angle), 0); // Polar to cartesian conversion - location.add(origin); // Make sure the location is relative to the pendulum's origin - - stroke(0); - strokeWeight(2); - // Draw the arm - line(origin.x, origin.y, location.x, location.y); - ellipseMode(CENTER); - fill(175); - // Draw the ball - ellipse(location.x, location.y, 48, 48); - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/Mover.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/Mover.pde deleted file mode 100644 index b25dcda65..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/Mover.pde +++ /dev/null @@ -1,78 +0,0 @@ -// Nature of Code 2011 -// Daniel Shiffman -// Chapter 3: Oscillation - -// Bob class, just like our regular Mover (location, velocity, acceleration, mass) - -class Bob { - PVector location; - PVector velocity; - PVector acceleration; - float mass = 24; - - // Arbitrary damping to simulate friction / drag - float damping = 0.98; - - // For mouse interaction - PVector dragOffset; - boolean dragging = false; - - // Constructor - Bob(float x, float y) { - location = new PVector(x,y); - velocity = new PVector(); - acceleration = new PVector(); - dragOffset = new PVector(); - } - - // Standard Euler integration - void update() { - velocity.add(acceleration); - velocity.mult(damping); - location.add(velocity); - acceleration.mult(0); - } - - // Newton's law: F = M * A - void applyForce(PVector force) { - PVector f = force.get(); - f.div(mass); - acceleration.add(f); - } - - - // Draw the bob - void display() { - stroke(0); - strokeWeight(2); - fill(175); - if (dragging) { - fill(50); - } - ellipse(location.x,location.y,mass*2,mass*2); - } - - // The methods below are for mouse interaction - - // This checks to see if we clicked on the mover - void clicked(int mx, int my) { - float d = dist(mx,my,location.x,location.y); - if (d < mass) { - dragging = true; - dragOffset.x = location.x-mx; - dragOffset.y = location.y-my; - } - } - - void stopDragging() { - dragging = false; - } - - void drag(int mx, int my) { - if (dragging) { - location.x = mx + dragOffset.x; - location.y = my + dragOffset.y; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/NOC_3_11_spring.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/NOC_3_11_spring.pde deleted file mode 100644 index 57c8c7212..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/NOC_3_11_spring.pde +++ /dev/null @@ -1,61 +0,0 @@ -// Nature of Code 2011 -// Daniel Shiffman -// Chapter 3: Oscillation -// Mover attached to spring connection -// PVector -// http://www.shiffman.net - - -// Mover object -Bob bob; - -// Spring object -Spring spring; - -void setup() { - size(800,200); - smooth(); - // Create objects at starting location - // Note third argument in Spring constructor is "rest length" - spring = new Spring(width/2,10,100); - bob = new Bob(width/2,100); - -} - -void draw() { - background(255); - // Apply a gravity force to the bob - PVector gravity = new PVector(0,2); - bob.applyForce(gravity); - - // Connect the bob to the spring (this calculates the force) - spring.connect(bob); - // Constrain spring distance between min and max - spring.constrainLength(bob,30,200); - - // Update bob - bob.update(); - // If it's being dragged - bob.drag(mouseX,mouseY); - - // Draw everything - spring.displayLine(bob); // Draw a line between spring and bob - bob.display(); - spring.display(); - - fill(0); - text("click on bob to drag",10,height-5); -} - - -// For mouse interaction with bob - -void mousePressed() { - bob.clicked(mouseX,mouseY); -} - -void mouseReleased() { - bob.stopDragging(); -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/Spring.pde b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/Spring.pde deleted file mode 100644 index 565fbb307..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/Spring.pde +++ /dev/null @@ -1,75 +0,0 @@ -// Nature of Code 2011 -// Daniel Shiffman -// Chapter 3: Oscillation - -// Class to describe an anchor point that can connect to "Bob" objects via a spring -// Thank you: http://www.myphysicslab.com/spring2d.html - -class Spring { - - // Location - PVector anchor; - - // Rest length and spring constant - float len; - float k = 0.2; - - // Constructor - Spring(float x, float y, int l) { - anchor = new PVector(x, y); - len = l; - } - - // Calculate spring force - void connect(Bob b) { - // Vector pointing from anchor to bob location - PVector force = PVector.sub(b.location, anchor); - // What is distance - float d = force.mag(); - // Stretch is difference between current distance and rest length - float stretch = d - len; - - // Calculate force according to Hooke's Law - // F = k * stretch - force.normalize(); - force.mult(-1 * k * stretch); - b.applyForce(force); - } - - // Constrain the distance between bob and anchor between min and max - void constrainLength(Bob b, float minlen, float maxlen) { - PVector dir = PVector.sub(b.location, anchor); - float d = dir.mag(); - // Is it too short? - if (d < minlen) { - dir.normalize(); - dir.mult(minlen); - // Reset location and stop from moving (not realistic physics) - b.location = PVector.add(anchor, dir); - b.velocity.mult(0); - // Is it too long? - } - else if (d > maxlen) { - dir.normalize(); - dir.mult(maxlen); - // Reset location and stop from moving (not realistic physics) - b.location = PVector.add(anchor, dir); - b.velocity.mult(0); - } - } - - void display() { - stroke(0); - fill(175); - strokeWeight(2); - rectMode(CENTER); - rect(anchor.x, anchor.y, 10, 10); - } - - void displayLine(Bob b) { - strokeWeight(2); - stroke(0); - line(b.location.x, b.location.y, anchor.x, anchor.y); - } -} - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/sketch.properties b/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/sketch.properties deleted file mode 100644 index b3cbe600e..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/NOC_3_11_spring/sketch.properties +++ /dev/null @@ -1,2 +0,0 @@ -mode.id=processing.mode.java.JavaMode -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/OOPWaveParticles.pde b/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/OOPWaveParticles.pde deleted file mode 100644 index 9cf9e3b3d..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/OOPWaveParticles.pde +++ /dev/null @@ -1,31 +0,0 @@ -// Sine Wave -// Daniel Shiffman - - -// Two wave objects -Wave wave0; -Wave wave1; - -void setup() { - size(640,360); - smooth(); - // Initialize a wave with starting point, width, amplitude, and period - wave0 = new Wave(new PVector(200,75),100,20,500); - wave1 = new Wave(new PVector(150,250),300,40,220); - -} - -void draw() { - background(255); - - // Update and display waves - wave0.calculate(); - wave0.display(); - - wave1.calculate(); - wave1.display(); - - -} - - diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/Particle.pde b/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/Particle.pde deleted file mode 100644 index ca6d677d2..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/Particle.pde +++ /dev/null @@ -1,20 +0,0 @@ - -class Particle { - PVector location; - - Particle() { - location = new PVector(); - } - - void setLocation(float x, float y) { - location.x = x; - location.y = y; - } - - void display() { - fill(random(255)); - ellipse(location.x,location.y,16,16); - } - - -} diff --git a/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/Wave.pde b/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/Wave.pde deleted file mode 100644 index cd54393be..000000000 --- a/java/examples/Books/Nature of Code/chp3_oscillation/OOPWaveParticles/Wave.pde +++ /dev/null @@ -1,52 +0,0 @@ -class Wave { - - int xspacing = 8; // How far apart should each horizontal location be spaced - int w; // Width of entire wave - - PVector origin; // Where does the wave's first point start - float theta = 0.0; // Start angle at 0 - float amplitude; // Height of wave - float period; // How many pixels before the wave repeats - float dx; // Value for incrementing X, to be calculated as a function of period and xspacing - //float[] yvalues; // Using an array to store height values for the wave (not entirely necessary) - Particle[] particles; - - Wave(PVector o, int w_, float a, float p) { - origin = o.get(); - w = w_; - period = p; - amplitude = a; - dx = (TWO_PI / period) * xspacing; - particles = new Particle[w/xspacing]; - for (int i = 0; i < particles.length; i++) { - particles[i] = new Particle(); - } - } - - - void calculate() { - // Increment theta (try different values for 'angular velocity' here - theta += 0.02; - - // For every x value, calculate a y value with sine function - float x = theta; - for (int i = 0; i < particles.length; i++) { - particles[i].setLocation(origin.x+i*xspacing,origin.y+sin(x)*amplitude); - x+=dx; - } - } - - void manipulate() { - // Loop through the array of particles and check stuff regarding the mouse - - } - - void display() { - - // A simple way to draw the wave with an ellipse at each location - for (int i = 0; i < particles.length; i++) { - particles[i].display(); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/CircleVsBlob.pde b/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/CircleVsBlob.pde deleted file mode 100644 index d552e3d74..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/CircleVsBlob.pde +++ /dev/null @@ -1,28 +0,0 @@ -// Smoke Particle System -// Daniel Shiffman - -// A basic smoke effect using a particle system -// Each particle is rendered as an alpha masked image - - -void setup() { - size(200,200); - smooth(); - PImage img = loadImage("texture.png"); - background(0); - image(img,0,0,width,height); - save("blob.tif"); - - background(0); - fill(255); - noStroke(); - ellipse(100,100,width,height); - save("circle.tif"); -} - -void draw() { - - -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/blob.tif b/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/blob.tif deleted file mode 100644 index 62b3058e4..000000000 Binary files a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/blob.tif and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/circle.tif b/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/circle.tif deleted file mode 100644 index 101ae8775..000000000 Binary files a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/circle.tif and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/data/texture.gif b/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/data/texture.gif deleted file mode 100644 index 17e84e806..000000000 Binary files a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/data/texture.gif and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/data/texture.psd b/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/data/texture.psd deleted file mode 100644 index 8208feb02..000000000 Binary files a/java/examples/Books/Nature of Code/chp4_systems/CircleVsBlob/data/texture.psd and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle/NOC_4_01_SingleParticle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle/NOC_4_01_SingleParticle.pde deleted file mode 100644 index 32f48596e..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle/NOC_4_01_SingleParticle.pde +++ /dev/null @@ -1,21 +0,0 @@ -Particle p; - -void setup() { - size(800,200); - p = new Particle(new PVector(width/2,20)); - background(255); - smooth(); -} - -void draw() { - background(255); - - p.run(); - if (p.isDead()) { - p = new Particle(new PVector(width/2,20)); - //println("Particle dead!"); - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle/Particle.pde deleted file mode 100644 index 7d87dc3f9..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle/Particle.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0, 0.05); - velocity = new PVector(random(-1, 1), random(-1, 0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0, lifespan); - strokeWeight(2); - fill(127, lifespan); - ellipse(location.x, location.y, 12, 12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle_trail/NOC_4_01_SingleParticle_trail.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle_trail/NOC_4_01_SingleParticle_trail.pde deleted file mode 100644 index 5548fb796..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle_trail/NOC_4_01_SingleParticle_trail.pde +++ /dev/null @@ -1,23 +0,0 @@ -Particle p; - -void setup() { - size(800, 200); - p = new Particle(new PVector(width/2, 20)); - background(255); - smooth(); -} - -void draw() { - if (mousePressed) { - noStroke(); - fill(255, 5); - rect(0, 0, width, height); - - p.run(); - if (p.isDead()) { - println("Particle dead!"); - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle_trail/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle_trail/Particle.pde deleted file mode 100644 index 815931e27..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_01_SingleParticle_trail/Particle.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0, 0.05); - velocity = new PVector(random(-1, 1), -1); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0, lifespan); - strokeWeight(2); - fill(127); - ellipse(location.x, location.y, 12, 12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_02_ArrayListParticles/NOC_4_02_ArrayListParticles.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_02_ArrayListParticles/NOC_4_02_ArrayListParticles.pde deleted file mode 100644 index 09d201c07..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_02_ArrayListParticles/NOC_4_02_ArrayListParticles.pde +++ /dev/null @@ -1,27 +0,0 @@ -ArrayList particles; - -void setup() { - size(800,200); - particles = new ArrayList(); - smooth(); -} - -void draw() { - background(255); - - particles.add(new Particle(new PVector(width/2,50))); - - // Using the iterator - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } -} - - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_02_ArrayListParticles/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_02_ArrayListParticles/Particle.pde deleted file mode 100644 index 17a447011..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_02_ArrayListParticles/Particle.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0, 0.05); - velocity = new PVector(random(-1, 1), random(-2, 0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0, lifespan); - strokeWeight(2); - fill(127, lifespan); - ellipse(location.x, location.y, 12, 12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/NOC_4_03_ParticleSystemClass.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/NOC_4_03_ParticleSystemClass.pde deleted file mode 100644 index a896b7f99..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/NOC_4_03_ParticleSystemClass.pde +++ /dev/null @@ -1,13 +0,0 @@ -ParticleSystem ps; - -void setup() { - size(800,200); - smooth(); - ps = new ParticleSystem(new PVector(width/2,50)); -} - -void draw() { - background(255); - ps.addParticle(); - ps.run(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/Particle.pde deleted file mode 100644 index a019abea2..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/Particle.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0,0.05); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0,lifespan); - strokeWeight(2); - fill(127,lifespan); - ellipse(location.x,location.y,12,12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/ParticleSystem.pde deleted file mode 100644 index 0df577d0e..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_03_ParticleSystemClass/ParticleSystem.pde +++ /dev/null @@ -1,31 +0,0 @@ -// Using Generics now! comment and annotate, etc. - -class ParticleSystem { - ArrayList particles; - PVector origin; - - ParticleSystem(PVector location) { - origin = location.get(); - particles = new ArrayList(); - } - - void addParticle() { - particles.add(new Particle(origin)); - } - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/NOC_4_04_SystemofSystems.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/NOC_4_04_SystemofSystems.pde deleted file mode 100644 index 6bfabb89d..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/NOC_4_04_SystemofSystems.pde +++ /dev/null @@ -1,30 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// Particles are generated each cycle through draw(), -// fall with gravity and fade out over time -// A ParticleSystem object manages a variable size (ArrayList) -// list of particles. - -ArrayList systems; - -void setup() { - size(800,200); - systems = new ArrayList(); - smooth(); -} - -void draw() { - background(255); - for (ParticleSystem ps: systems) { - ps.run(); - ps.addParticle(); - } -} - -void mousePressed() { - systems.add(new ParticleSystem(1,new PVector(mouseX,mouseY))); -} - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/Particle.pde deleted file mode 100644 index a019abea2..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/Particle.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0,0.05); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0,lifespan); - strokeWeight(2); - fill(127,lifespan); - ellipse(location.x,location.y,12,12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/ParticleSystem.pde deleted file mode 100644 index 870908e47..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems/ParticleSystem.pde +++ /dev/null @@ -1,52 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A class to describe a group 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 - - ParticleSystem(int num, PVector v) { - particles = new ArrayList(); // Initialize the arraylist - origin = v.get(); // Store the origin point - for (int i = 0; i < num; i++) { - particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist - } - } - - void run() { - // Using the Iterator b/c we are deleting from list while iterating - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } - - void addParticle() { - particles.add(new Particle(origin)); - } - - 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/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/NOC_4_04_SystemofSystems_b.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/NOC_4_04_SystemofSystems_b.pde deleted file mode 100644 index 70ec0bedc..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/NOC_4_04_SystemofSystems_b.pde +++ /dev/null @@ -1,32 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// Particles are generated each cycle through draw(), -// fall with gravity and fade out over time -// A ParticleSystem object manages a variable size (ArrayList) -// list of particles. - -ArrayList systems; - -void setup() { - size(800,200); - systems = new ArrayList(); - systems.add(new ParticleSystem(1,new PVector(100,25))); - - smooth(); -} - -void draw() { - background(255); - for (ParticleSystem ps: systems) { - ps.run(); - ps.addParticle(); - } -} - -void mousePressed() { - systems.add(new ParticleSystem(1,new PVector(mouseX,mouseY))); -} - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/Particle.pde deleted file mode 100644 index a019abea2..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/Particle.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0,0.05); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0,lifespan); - strokeWeight(2); - fill(127,lifespan); - ellipse(location.x,location.y,12,12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/ParticleSystem.pde deleted file mode 100644 index 870908e47..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_b/ParticleSystem.pde +++ /dev/null @@ -1,52 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A class to describe a group 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 - - ParticleSystem(int num, PVector v) { - particles = new ArrayList(); // Initialize the arraylist - origin = v.get(); // Store the origin point - for (int i = 0; i < num; i++) { - particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist - } - } - - void run() { - // Using the Iterator b/c we are deleting from list while iterating - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } - - void addParticle() { - particles.add(new Particle(origin)); - } - - 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/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/NOC_4_04_SystemofSystems_c.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/NOC_4_04_SystemofSystems_c.pde deleted file mode 100644 index dd84003d6..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/NOC_4_04_SystemofSystems_c.pde +++ /dev/null @@ -1,35 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// Particles are generated each cycle through draw(), -// fall with gravity and fade out over time -// A ParticleSystem object manages a variable size (ArrayList) -// list of particles. - -ArrayList systems; - -void setup() { - size(800,200); - systems = new ArrayList(); - systems.add(new ParticleSystem(1,new PVector(100,25))); - for (int i = 0; i < 6; i++) { - systems.add(new ParticleSystem(1,new PVector(random(width),random(height)))); - } - - smooth(); -} - -void draw() { - background(255); - for (ParticleSystem ps: systems) { - ps.run(); - ps.addParticle(); - } -} - -void mousePressed() { - systems.add(new ParticleSystem(1,new PVector(mouseX,mouseY))); -} - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/Particle.pde deleted file mode 100644 index a019abea2..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/Particle.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0,0.05); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0,lifespan); - strokeWeight(2); - fill(127,lifespan); - ellipse(location.x,location.y,12,12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/ParticleSystem.pde deleted file mode 100644 index 870908e47..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_04_SystemofSystems_c/ParticleSystem.pde +++ /dev/null @@ -1,52 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A class to describe a group 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 - - ParticleSystem(int num, PVector v) { - particles = new ArrayList(); // Initialize the arraylist - origin = v.get(); // Store the origin point - for (int i = 0; i < num; i++) { - particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist - } - } - - void run() { - // Using the Iterator b/c we are deleting from list while iterating - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } - - void addParticle() { - particles.add(new Particle(origin)); - } - - 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/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/Confetti.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/Confetti.pde deleted file mode 100644 index c0db00486..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/Confetti.pde +++ /dev/null @@ -1,25 +0,0 @@ -class Confetti extends Particle { - - // We could add variables for only Confetti here if we so - - Confetti(PVector l) { - super(l); - } - - // Inherits update() from parent - - // Override the display method - void display() { - rectMode(CENTER); - fill(127,lifespan); - stroke(0,lifespan); - strokeWeight(2); - pushMatrix(); - translate(location.x,location.y); - float theta = map(location.x,0,width,0,TWO_PI*2); - rotate(theta); - rect(0,0,12,12); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/NOC_4_05_ParticleSystemInheritancePolymorphism.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/NOC_4_05_ParticleSystemInheritancePolymorphism.pde deleted file mode 100644 index a896b7f99..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/NOC_4_05_ParticleSystemInheritancePolymorphism.pde +++ /dev/null @@ -1,13 +0,0 @@ -ParticleSystem ps; - -void setup() { - size(800,200); - smooth(); - ps = new ParticleSystem(new PVector(width/2,50)); -} - -void draw() { - background(255); - ps.addParticle(); - ps.run(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/Particle.pde deleted file mode 100644 index e51908f50..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/Particle.pde +++ /dev/null @@ -1,50 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0,0.05); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0,lifespan); - strokeWeight(2); - fill(127,lifespan); - ellipse(location.x,location.y,12,12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/ParticleSystem.pde deleted file mode 100644 index 8a5be1aaa..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_05_ParticleSystemInheritancePolymorphism/ParticleSystem.pde +++ /dev/null @@ -1,34 +0,0 @@ - -class ParticleSystem { - ArrayList particles; - PVector origin; - - ParticleSystem(PVector location) { - origin = location.get(); - particles = new ArrayList(); - } - - void addParticle() { - float r = random(1); - if (r < 0.5) { - particles.add(new Particle(origin)); - } else { - particles.add(new Confetti(origin)); - } - } - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } -} - - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/NOC_4_06_ParticleSystemForces.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/NOC_4_06_ParticleSystemForces.pde deleted file mode 100644 index 266c12103..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/NOC_4_06_ParticleSystemForces.pde +++ /dev/null @@ -1,18 +0,0 @@ -ParticleSystem ps; - -void setup() { - size(800,200); - smooth(); - ps = new ParticleSystem(new PVector(width/2,50)); -} - -void draw() { - background(255); - - // Apply gravity force to all Particles - PVector gravity = new PVector(0,0.1); - ps.applyForce(gravity); - - ps.addParticle(); - ps.run(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/Particle.pde deleted file mode 100644 index 120eb0e8c..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/Particle.pde +++ /dev/null @@ -1,57 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - float mass = 1; // Let's do something better here! - - Particle(PVector l) { - acceleration = new PVector(0,0); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - void applyForce(PVector force) { - PVector f = force.get(); - f.div(mass); - acceleration.add(f); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0,lifespan); - strokeWeight(2); - fill(127,lifespan); - ellipse(location.x,location.y,12,12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/ParticleSystem.pde deleted file mode 100644 index 5f2223739..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_06_ParticleSystemForces/ParticleSystem.pde +++ /dev/null @@ -1,34 +0,0 @@ - -class ParticleSystem { - ArrayList particles; - PVector origin; - - ParticleSystem(PVector location) { - origin = location.get(); - particles = new ArrayList(); - } - - void addParticle() { - particles.add(new Particle(origin)); - } - - // A function to apply a force to all Particles - void applyForce(PVector f) { - for (Particle p: particles) { - p.applyForce(f); - } - } - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/NOC_4_07_ParticleSystemForcesRepeller.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/NOC_4_07_ParticleSystemForcesRepeller.pde deleted file mode 100644 index 3450950b5..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/NOC_4_07_ParticleSystemForcesRepeller.pde +++ /dev/null @@ -1,23 +0,0 @@ -ParticleSystem ps; -Repeller repeller; - -void setup() { - size(800,200); - smooth(); - ps = new ParticleSystem(new PVector(width/2,50)); - repeller = new Repeller(width/2-20,height/2); -} - -void draw() { - background(255); - ps.addParticle(); - - // Apply gravity force to all Particles - PVector gravity = new PVector(0,0.1); - ps.applyForce(gravity); - - ps.applyRepeller(repeller); - - repeller.display(); - ps.run(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/Particle.pde deleted file mode 100644 index 120eb0e8c..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/Particle.pde +++ /dev/null @@ -1,57 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - float mass = 1; // Let's do something better here! - - Particle(PVector l) { - acceleration = new PVector(0,0); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - display(); - } - - void applyForce(PVector force) { - PVector f = force.get(); - f.div(mass); - acceleration.add(f); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - lifespan -= 2.0; - } - - // Method to display - void display() { - stroke(0,lifespan); - strokeWeight(2); - fill(127,lifespan); - ellipse(location.x,location.y,12,12); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/ParticleSystem.pde deleted file mode 100644 index ba513bf62..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/ParticleSystem.pde +++ /dev/null @@ -1,43 +0,0 @@ - -class ParticleSystem { - ArrayList particles; - PVector origin; - - ParticleSystem(PVector location) { - origin = location.get(); - particles = new ArrayList(); - } - - void addParticle() { - particles.add(new Particle(origin)); - } - - // A function to apply a force to all Particles - void applyForce(PVector f) { - for (Particle p: particles) { - p.applyForce(f); - } - } - - void applyRepeller(Repeller r) { - for (Particle p: particles) { - PVector force = r.repel(p); - p.applyForce(force); - } - } - - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/Repeller.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/Repeller.pde deleted file mode 100644 index 973f55a89..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_07_ParticleSystemForcesRepeller/Repeller.pde +++ /dev/null @@ -1,36 +0,0 @@ -// Particles + Forces -// Daniel Shiffman - -// A very basic Repeller class -class Repeller { - - // Gravitational Constant - float G = 100; - // Location - PVector location; - float r = 10; - - Repeller(float x, float y) { - location = new PVector(x,y); - } - - void display() { - stroke(0); - strokeWeight(2); - fill(175); - ellipse(location.x,location.y,48,48); - } - - // Calculate a force to push particle away from repeller - PVector repel(Particle p) { - PVector dir = PVector.sub(location,p.location); // Calculate direction of force - float d = dir.mag(); // Distance between objects - dir.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction) - d = constrain(d,5,100); // Keep distance within a reasonable range - float force = -1 * G / (d * d); // Repelling force is inversely proportional to distance - dir.mult(force); // Get force vector --> magnitude * direction - return dir; - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/NOC_4_08_ParticleSystemSmoke.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/NOC_4_08_ParticleSystemSmoke.pde deleted file mode 100644 index db907b374..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/NOC_4_08_ParticleSystemSmoke.pde +++ /dev/null @@ -1,54 +0,0 @@ -// Smoke Particle System -// Daniel Shiffman - -// A basic smoke effect using a particle system -// Each particle is rendered as an alpha masked image - -/* @pjs preload="processingjs/chapter04/_4_08_ParticleSystemSmoke/data/texture.png"; */ - -ParticleSystem ps; -Random generator; - -void setup() { - size(383,200); - generator = new Random(); - PImage img = loadImage("texture.png"); - ps = new ParticleSystem(0,new PVector(width/2,height-25),img); - smooth(); -} - -void draw() { - background(0); - - // Calculate a "wind" force based on mouse horizontal position - 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); - -} - -// 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.heading2D()); - // 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(); -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/Particle.pde deleted file mode 100644 index 709929f41..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/Particle.pde +++ /dev/null @@ -1,64 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class, renders the particle as an image - -// Created 2 May 2005 - -class Particle { - PVector loc; - PVector vel; - PVector acc; - float lifespan; - PImage img; - - Particle(PVector l,PImage img_) { - acc = new PVector(0,0); - float vx = (float) generator.nextGaussian()*0.3; - float vy = (float) generator.nextGaussian()*0.3 - 1.0; - vel = new PVector(vx,vy); - loc = l.get(); - lifespan = 100.0; - img = img_; - } - - void run() { - update(); - render(); - } - - // Method to apply a force vector to the Particle object - // Note we are ignoring "mass" here - void applyForce(PVector f) { - acc.add(f); - } - - // Method to update location - void update() { - vel.add(acc); - loc.add(vel); - lifespan -= 2.5; - acc.mult(0); // clear Acceleration - } - - // Method to display - void render() { - 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 (lifespan <= 0.0) { - return true; - } else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/ParticleSystem.pde deleted file mode 100644 index e193f63c0..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/ParticleSystem.pde +++ /dev/null @@ -1,62 +0,0 @@ -// Smoke Particle Syste -// Daniel Shiffman - -// A class to describe a group 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 - PImage img; - - ParticleSystem(int num, PVector v, PImage img_) { - 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 - } - } - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.dead()) { - it.remove(); - } - } - } - - // Method to add a force vector to all particles currently in the system - 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/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/data/texture.psd b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/data/texture.psd deleted file mode 100644 index 8208feb02..000000000 Binary files a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/data/texture.psd and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/sketch.properties b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/NOC_4_08_ParticleSystemSmoke_b.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/NOC_4_08_ParticleSystemSmoke_b.pde deleted file mode 100644 index db907b374..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/NOC_4_08_ParticleSystemSmoke_b.pde +++ /dev/null @@ -1,54 +0,0 @@ -// Smoke Particle System -// Daniel Shiffman - -// A basic smoke effect using a particle system -// Each particle is rendered as an alpha masked image - -/* @pjs preload="processingjs/chapter04/_4_08_ParticleSystemSmoke/data/texture.png"; */ - -ParticleSystem ps; -Random generator; - -void setup() { - size(383,200); - generator = new Random(); - PImage img = loadImage("texture.png"); - ps = new ParticleSystem(0,new PVector(width/2,height-25),img); - smooth(); -} - -void draw() { - background(0); - - // Calculate a "wind" force based on mouse horizontal position - 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); - -} - -// 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.heading2D()); - // 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(); -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/Particle.pde deleted file mode 100644 index 45a6e5b06..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/Particle.pde +++ /dev/null @@ -1,64 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class, renders the particle as an image - -// Created 2 May 2005 - -class Particle { - PVector loc; - PVector vel; - PVector acc; - float lifespan; - PImage img; - - Particle(PVector l,PImage img_) { - acc = new PVector(0,0); - float vx = (float) generator.nextGaussian()*0.3; - float vy = (float) generator.nextGaussian()*0.3 - 1.0; - vel = new PVector(vx,vy); - loc = l.get(); - lifespan = 100.0; - img = img_; - } - - void run() { - update(); - render(); - } - - // Method to apply a force vector to the Particle object - // Note we are ignoring "mass" here - void applyForce(PVector f) { - acc.add(f); - } - - // Method to update location - void update() { - vel.add(acc); - loc.add(vel); - lifespan -= 2.5; - acc.mult(0); // clear Acceleration - } - - // Method to display - void render() { - //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 (lifespan <= 0.0) { - return true; - } else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/ParticleSystem.pde deleted file mode 100644 index e193f63c0..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/ParticleSystem.pde +++ /dev/null @@ -1,62 +0,0 @@ -// Smoke Particle Syste -// Daniel Shiffman - -// A class to describe a group 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 - PImage img; - - ParticleSystem(int num, PVector v, PImage img_) { - 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 - } - } - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.dead()) { - it.remove(); - } - } - } - - // Method to add a force vector to all particles currently in the system - 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/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/data/texture.psd b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/data/texture.psd deleted file mode 100644 index 8208feb02..000000000 Binary files a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/data/texture.psd and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/sketch.properties b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/sketch.properties deleted file mode 100644 index b3cbe600e..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_08_ParticleSystemSmoke_b/sketch.properties +++ /dev/null @@ -1,2 +0,0 @@ -mode.id=processing.mode.java.JavaMode -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/NOC_4_09_AdditiveBlending.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/NOC_4_09_AdditiveBlending.pde deleted file mode 100644 index 00738c10d..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/NOC_4_09_AdditiveBlending.pde +++ /dev/null @@ -1,34 +0,0 @@ -// Smoke Particle System -// Daniel Shiffman - -// A basic smoke effect using a particle system -// Each particle is rendered as an alpha masked image - -ParticleSystem ps; - - -PImage img; - -void setup() { - size(800, 200, P2D); - - // Create an alpha masked image to be applied as the particle's texture - img = loadImage("texture.png"); - - ps = new ParticleSystem(0, new PVector(width/2, 50)); - smooth(); - -} - -void draw() { - - blendMode(ADD); - - background(0); - - ps.run(); - for (int i = 0; i < 10; i++) { - ps.addParticle(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/Particle.pde deleted file mode 100644 index 4f08e14f8..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/Particle.pde +++ /dev/null @@ -1,51 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - PVector loc; - PVector vel; - PVector acc; - float lifespan; - - // Another constructor (the one we are using here) - Particle(PVector l) { - // Boring example with constant acceleration - acc = new PVector(0,0.05,0); - vel = new PVector(random(-1,1),random(-1,0),0); - vel.mult(2); - loc = l.get(); - lifespan = 255; - } - - void run() { - update(); - render(); - } - - // Method to update location - void update() { - vel.add(acc); - loc.add(vel); - lifespan -= 2.0; - } - - // Method to display - void render() { - imageMode(CENTER); - tint(lifespan); - image(img,loc.x,loc.y); - } - - // Is the particle still useful? - boolean dead() { - if (lifespan <= 0.0) { - return true; - } else { - return false; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/ParticleSystem.pde deleted file mode 100644 index 1512cf487..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/ParticleSystem.pde +++ /dev/null @@ -1,53 +0,0 @@ -// Smoke Particle Syste -// Daniel Shiffman - -// A class to describe a group 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 - - PImage tex; - - ParticleSystem(int num, PVector v) { - particles = new ArrayList(); // Initialize the arraylist - origin = v.get(); // Store the origin point - for (int i = 0; i < num; i++) { - particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist - } - } - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.dead()) { - it.remove(); - } - } - } - - void addParticle() { - particles.add(new Particle(origin)); - } - - 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/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/data/texture.psd b/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/data/texture.psd deleted file mode 100644 index d532f15aa..000000000 Binary files a/java/examples/Books/Nature of Code/chp4_systems/NOC_4_09_AdditiveBlending/data/texture.psd and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/NOC_04_6ParticleSystemInheritance_pushpop.pde b/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/NOC_04_6ParticleSystemInheritance_pushpop.pde deleted file mode 100644 index fc0b2459e..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/NOC_04_6ParticleSystemInheritance_pushpop.pde +++ /dev/null @@ -1,13 +0,0 @@ -ParticleSystem ps; - -void setup() { - size(200,200); - smooth(); - ps = new ParticleSystem(new PVector(width/2,50)); -} - -void draw() { - background(255); - ps.addParticle(); - ps.run(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/Particle.pde b/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/Particle.pde deleted file mode 100644 index bc2e58cb3..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/Particle.pde +++ /dev/null @@ -1,61 +0,0 @@ -// Simple Particle System -// Daniel Shiffman - -// A simple Particle class - -class Particle { - - PVector location; - PVector velocity; - PVector acceleration; - float lifespan; - - Particle(PVector l) { - acceleration = new PVector(0,0.05); - velocity = new PVector(random(-1,1),random(-2,0)); - location = l.get(); - lifespan = 255.0; - } - - void run() { - update(); - push(); - display(); - pop(); - } - - // Method to update location - void update() { - velocity.add(acceleration); - location.add(velocity); - lifespan -= 2.0; - } - - - void push() { - pushMatrix(); - } - - void pop() { - popMatrix(); - } - - // Method to display - void display() { - stroke(0,lifespan); - fill(0,lifespan); - translate(location.x,location.y); - ellipse(0,0,8,8); - } - - // Is the particle still useful? - boolean isDead() { - if (lifespan < 0.0) { - return true; - } else { - return false; - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/ParticleChild.pde b/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/ParticleChild.pde deleted file mode 100644 index 4e2cc630f..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/ParticleChild.pde +++ /dev/null @@ -1,20 +0,0 @@ -class ParticleChild extends Particle { - - // We could add variables for only Confetti here if we so - - ParticleChild(PVector l) { - super(l); - } - - // Inherits update() from parent - - // Override the display method - void display() { - super.display(); - float theta = map(location.x,0,width,0,TWO_PI*2); - rotate(theta); - stroke(0); - line(0,0,50,0); - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/ParticleSystem.pde deleted file mode 100644 index fdba58bf1..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/ParticleSystemInheritance_pushpop/ParticleSystem.pde +++ /dev/null @@ -1,34 +0,0 @@ - -class ParticleSystem { - ArrayList particles; - PVector origin; - - ParticleSystem(PVector location) { - origin = location.get(); - particles = new ArrayList(); - } - - void addParticle() { - float r = random(1); - if (r < 0.5) { - particles.add(new Particle(origin)); - } else { - particles.add(new ParticleChild(origin)); - } - } - - void run() { - Iterator it = particles.iterator(); - while (it.hasNext()) { - Particle p = it.next(); - p.run(); - if (p.isDead()) { - it.remove(); - } - } - } -} - - - - diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/NOC_gl.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/NOC_gl.pde deleted file mode 100644 index d22e4bb0a..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/NOC_gl.pde +++ /dev/null @@ -1,11 +0,0 @@ - - -void renderImage(PImage img, Vec3D _loc, float _diam, color _col, float _alpha ) { - pushMatrix(); - translate( _loc.x, _loc.y, _loc.z ); - tint(red(_col), green(_col), blue(_col), _alpha); - imageMode(CENTER); - image(img,0,0,_diam,_diam); - popMatrix(); -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/emitter.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/emitter.pde deleted file mode 100644 index 479138397..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/emitter.pde +++ /dev/null @@ -1,89 +0,0 @@ - -/* -The emitter is just an object that follows the cursor and -can spawn new particle objects. It would be easier to just make -the location vector match the cursor position but I have opted -to use a velocity vector because later I will be allowing for -multiple emitters. -*/ - -class Emitter{ - Vec3D loc; - Vec3D vel; - Vec3D velToMouse; - - color myColor; - - ArrayList particles; - - Emitter( ){ - loc = new Vec3D(); - vel = new Vec3D(); - velToMouse = new Vec3D(); - - myColor = color( 1, 1, 1 ); - - particles = new ArrayList(); - } - - void exist(){ - setVelToMouse(); - findVelocity(); - setPosition(); - iterateListExist(); - render(); - - gl.glDisable( GL.GL_TEXTURE_2D ); - - if( ALLOWTRAILS ) - iterateListRenderTrails(); - } - - void setVelToMouse(){ - velToMouse.set( mouseX - loc.x, mouseY - loc.y, 0 ); - } - - void findVelocity(){ - vel.interpolateToSelf( velToMouse, .35 ); - } - - void setPosition(){ - loc.addSelf( vel ); - - if( ALLOWFLOOR ){ - if( loc.y > floorLevel ){ - loc.y = floorLevel; - vel.y = 0; - } - } - } - - void iterateListExist(){ - for( Iterator it = particles.iterator(); it.hasNext(); ){ - Particle p = (Particle) it.next(); - if( !p.ISDEAD ){ - p.exist(); - } else { - it.remove(); - } - } - } - - - void render(){ - renderImage( emitterImg, loc, 150, myColor, 1.0 ); - } - - void iterateListRenderTrails(){ - for( Iterator it = particles.iterator(); it.hasNext(); ){ - Particle p = (Particle) it.next(); - p.renderTrails(); - } - } - - void addParticles( int _amt ){ - for( int i=0; i<_amt; i++ ){ - particles.add( new Particle( loc, vel ) ); - } - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/flight404_particles_1_simple.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/flight404_particles_1_simple.pde deleted file mode 100644 index d7018b981..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/flight404_particles_1_simple.pde +++ /dev/null @@ -1,160 +0,0 @@ -// Updated version of flight404 Particle Emitter release 1 -// This works with Processing 1.0 -// All of the advanced openGL direct calls that use display lists, etc. have been stripped out -// It's my intention to redo this example using GlGraphics (http://glgraphics.sourceforge.net/) -// But for now, just want to make sure it works in principal - -// February 28 2011 -// Daniel Shiffman - -// Source Code release 1 -// Particle Emitter -// -// February 11th 2008 -// -// Built with Processing v.135 which you can download at http://www.processing.org/download -// -// Robert Hodgin -// flight404.com -// barbariangroup.com - -// features: -// Toxi's magnificent Vec3D library -// perlin noise flow fields -// ribbon trails -// OpenGL additive blending -// OpenGL display lists -// -// Uses the very useful Vec3D library by Karsten Schmidt (toxi) -// You can download it at http://code.google.com/p/toxiclibs/downloads/list -// -// Please post suggestions and improvements at the flight404 blog. When nicer/faster/better -// practices are suggested, I will incorporate them into the source and repost. I think that -// will be a reasonable system for now. -// -// Future additions will include: -// Rudimentary camera movement -// Magnetic repulsion -// More textures means more iron -// -// UPDATES -// -// February 11th 2008 -// Reorganized some of the OpenGL calls as per Simon Gelfius' suggestion. -// http://www.kinesis.be/ - - -import toxi.geom.*; -import processing.opengl.*; -import javax.media.opengl.*; - -PGraphicsOpenGL pgl; -GL gl; - - -Emitter emitter; -Vec3D gravity; -float floorLevel; - -PImage particleImg; -PImage emitterImg; - -int counter; - - -boolean ALLOWGRAVITY; // add gravity vector? -boolean ALLOWPERLIN; // add perlin noise flow field vector? -boolean ALLOWTRAILS; // render particle trails? -boolean ALLOWFLOOR; // add a floor? - // Turning on all of these options will make things - // slow down. - -void setup(){ - size( 600, 600, OPENGL ); - // Lately I have gotten into the habit of limiting the color range to be - // 0.0 to 1.0. It works this way in OpenGL so I might as well get used to it. - colorMode( RGB, 1.0 ); - - // Turn on 4X antialiasing - hint( ENABLE_OPENGL_4X_SMOOTH ); - - // More OpenGL necessity. - pgl = (PGraphicsOpenGL) g; - gl = pgl.gl; - - // Loads in a particle image from the data folder. Image size should be a power of 2. - particleImg = loadImage( "particle.png" ); - emitterImg = loadImage( "emitter.png" ); - - emitter = new Emitter(); - gravity = new Vec3D( 0, .35, 0 ); // gravity vector - floorLevel = 400; -} - -void draw(){ - background( 0.0 ); - perspective( PI/3.0, (float)width/(float)height, 1, 5000 ); - - // Turns on additive blending so we can draw a bunch of glowing images without - // needing to do any depth testing. - gl.glDepthMask(false); - gl.glEnable( GL.GL_BLEND ); - gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE); - - emitter.exist(); - - // If the mouse button is pressed, then add 10 new particles. - if( mousePressed ){ - if( ALLOWTRAILS && ALLOWFLOOR ){ - emitter.addParticles( 5 ); - } else { - emitter.addParticles( 10 ); - } - } - - counter ++; -} - - -void keyPressed(){ - if( key == 'g' || key == 'G' ) - ALLOWGRAVITY = !ALLOWGRAVITY; - - if( key == 'p' || key == 'P' ) - ALLOWPERLIN = !ALLOWPERLIN; - - if( key == 't' || key == 'T' ) - ALLOWTRAILS = !ALLOWTRAILS; - - if( key == 'f' || key == 'F' ) - ALLOWFLOOR = !ALLOWFLOOR; - -} - - -// This method should be nicer, but it isnt. I use getRads to get a perlin noise -// based angle in radians based on the x and y position of the object asking for it. -// Perlin noise is supposed to give you back a number between 0 and 1, but it wont -// necessarily give you numbers that range from 0 to 1. A usual result is more like -// .25 to .75. -// -// So the point of this method is to try to normalize the values to a -// range of 0 to 1. It's not perfect, and I still get weird results. -// For instance, the mult variable is supposed to be the multiplier for the range. -// So if i wanted a random angle between 0 and TWO_PI, I would set the mult = TWO_PI. -// But when I do that, I find the Perlin noise tends to give me a left-pointing angle. -// To counteract, I end up setting the mult to 10.0 in order to increase the chances -// that I get a nice range from at least 0 to TWO_PI. -float minNoise = 0.499; -float maxNoise = 0.501; -float getRads(float val1, float val2, float mult, float div){ - float rads = noise(val1/div, val2/div, counter/div); - - if (rads < minNoise) minNoise = rads; - if (rads > maxNoise) maxNoise = rads; - - rads -= minNoise; - rads *= 1.0/(maxNoise - minNoise); - - return rads * mult; -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/particle.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/particle.pde deleted file mode 100644 index 16441c9c5..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_1_simple/particle.pde +++ /dev/null @@ -1,208 +0,0 @@ -/* -General Structure notes. - My classes tend to have a similar naming scheme and flow. I start with the 'exist' method. - Exist is what an object needs to do every frame. Usually 'existing' consists of four main things. - 1) Find the velocity. This involves determining what influences there are on the velocity. - 2) Apply the velocity to the location. - 3) Render the object. - 4) Age the object. - - I also use the metaphor of aging and death. When first made, a particle's age will be zero. - Every frame, the age will increment. If the age reaches the lifeSpan (which is a random number - that I set in the constructor), then the boolean ISDEAD is set to true and the arraylist iterator - removes the dead element from the list. - */ - - - -class Particle { - int len; // number of elements in position array - Vec3D[] loc; // array of position vectors - Vec3D startLoc; // just used to make sure every loc[] is initialized to the same position - Vec3D vel; // velocity vector - Vec3D perlin; // perlin noise vector - float radius; // particle's size - float age; // current age of particle - int lifeSpan; // max allowed age of particle - float agePer; // range from 1.0 (birth) to 0.0 (death) - float bounceAge; // amount to age particle when it bounces off floor - boolean ISDEAD; // if age == lifeSpan, make particle die - boolean ISBOUNCING; // if particle hits the floor... - - - Particle( Vec3D _loc, Vec3D _vel ) { - radius = random( 10, 40 ); - len = (int)( radius ); - loc = new Vec3D[ len ]; - - // This confusing-looking line does three things at once. - // First, you make a random vector. - // new Vec3D().randomVector() - // Next, you multiply that vector by a random number from 0.0 to 5.0. - // scaleSelf( 5.0 ); - // Finally, you add this new vector to the original sent vector. - // _loc.add( ); - // This is just a way to make sure all the particles made this frame - // don't all start on the exact same pixel. This staggering will be useful - // when we incorporate magnetic repulsion in a later tutorial. - startLoc = new Vec3D( _loc.add( new Vec3D().randomVector().scaleSelf( random( 5.0 ) ) ) ); - - for( int i=0; i floorLevel ) { - ISBOUNCING = true; - } - else { - ISBOUNCING = false; - } - } - - if( ISBOUNCING ) { - vel.scaleSelf( .75 ); - vel.y *= -.5; - } - } - - void setPosition() { - // Every frame, the current location will be passed on to - // the next element in the location array. Think 'cursor trail effect'. - for( int i=len-1; i>0; i-- ) { - loc[i].set( loc[i-1] ); - } - - // Set the initial location. - // loc[0] represents the current position of the particle. - loc[0].addSelf( vel ); - } - - void render() { - // As the particle ages, it will gain blue but will lose red and green. - color c = color( agePer, agePer*.75, 1.0 - agePer ); - renderImage(particleImg, loc[0], radius * agePer, c, 1.0 ); - } - - void renderTrails() { - float xp, yp, zp; - float xOff, yOff, zOff; - beginShape(QUAD_STRIP); - for ( int i=0; i lifeSpan ) { - ISDEAD = true; - } - else { - // When spawned, the agePer is 1.0. - // When death occurs, the agePer is 0.0. - agePer = 1.0 - age/(float)lifeSpan; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/NOC_gl.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/NOC_gl.pde deleted file mode 100644 index 2f91c0f1b..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/NOC_gl.pde +++ /dev/null @@ -1,49 +0,0 @@ - -int squareList; -void initGL(){ - pgl.beginGL(); - squareList = gl.glGenLists(1); - gl.glNewList(squareList, GL.GL_COMPILE); - gl.glBegin(GL.GL_POLYGON); - gl.glTexCoord2f(0, 0); gl.glVertex2f(-.5, -.5); - gl.glTexCoord2f(1, 0); gl.glVertex2f( .5, -.5); - gl.glTexCoord2f(1, 1); gl.glVertex2f( .5, .5); - gl.glTexCoord2f(0, 1); gl.glVertex2f(-.5, .5); - gl.glEnd(); - gl.glEndList(); - pgl.endGL(); -} - -void renderImage( Vec3D _loc, float _diam, color _col, float _alpha ){ - gl.glPushMatrix(); - gl.glTranslatef( _loc.x, -_loc.y, _loc.z ); - pov.glReverseCamera(); - gl.glScalef( _diam, _diam, _diam ); - gl.glColor4f( red(_col), green(_col), blue(_col), _alpha ); - gl.glCallList( squareList ); - gl.glPopMatrix(); -} - -// This will allow you to draw images that are oriented to the floor plane. -void renderImageOnFloor( Vec3D _loc, float _diam, color _col, float _aa ){ - gl.glPushMatrix(); - gl.glTranslatef( _loc.x, -_loc.y, _loc.z ); - gl.glScalef( _diam, _diam, _diam ); - gl.glRotatef( 90, 1.0, 0.0, 0.0 ); - gl.glColor4f( red(_col), green(_col), blue(_col), _aa ); - gl.glCallList( squareList ); - gl.glPopMatrix(); -} - -// This will allow you to specify a rotation for images that are oriented perpendicular to the eyeNormal -// which is the vector pointing from the camera's eye to the camera's point of interest. -void renderImageAndRotate( Vec3D _loc, float _diam, color _col, float _aa, float _rot ){ - gl.glPushMatrix(); - gl.glTranslatef( _loc.x, -_loc.y, _loc.z ); - gl.glRotatef( degrees( _rot ), pov.eyeNormal.x, pov.eyeNormal.y, pov.eyeNormal.z ); - pov.glReverseCamera(); - gl.glScalef( _diam, _diam, _diam ); - gl.glColor4f( red(_col), green(_col), blue(_col), _aa ); - gl.glCallList( squareList ); - gl.glPopMatrix(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/cursor.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/cursor.pde deleted file mode 100644 index ee2909cb9..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/cursor.pde +++ /dev/null @@ -1,25 +0,0 @@ -class Cursor{ - Vec3D loc; - Vec3D vel; - - Cursor(){ - loc = new Vec3D(); - vel = new Vec3D(); - } - - void exist(){ - // 2.35 is an arbitrary number. Ideally, this cursor would function - // properly regardless of the camera's rotation and distance from the object. - // Im not sure how to make that happen... 3D interaction with the cursor has - // been low on my research list. Think of this as a crappy placeholder. - loc.set( ( mouseX - xMid ) * 2.25, ( mouseY - yMid ) * 2.25, 0 ); - } - - void render(){ - pushMatrix(); - translate( loc.x, loc.y, loc.z ); - fill( 1, 0, 0 ); - sphere( 10 ); - popMatrix(); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/emitter.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/emitter.pde deleted file mode 100644 index 04eb2045a..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/emitter.pde +++ /dev/null @@ -1,182 +0,0 @@ -class Emitter{ - Vec3D loc; - Vec3D vel; - Vec3D velToMouse; - float radius; - - Texture coronaTex; - Texture emitterTex; - Texture particleTex; - Texture reflectionTex; - - color myColor; - - ArrayList particles; - ArrayList nebulae; - - Emitter( ){ - - try { - coronaTex = TextureIO.newTexture(new File(dataPath("corona.png")), true); - emitterTex = TextureIO.newTexture(new File(dataPath("emitter.png")), true); - particleTex = TextureIO.newTexture(new File(dataPath("particle.png")), true); - reflectionTex = TextureIO.newTexture(new File(dataPath("reflection.png")), true); - } - catch (IOException e) { - println("Texture file is missing"); - exit(); // or handle it some other way - } - - loc = new Vec3D(); - vel = new Vec3D(); - velToMouse = new Vec3D(); - - radius = 100; - - myColor = color( 1, 1, 1 ); - - particles = new ArrayList(); - nebulae = new ArrayList(); - } - - void exist(){ - findVelocity(); - setPosition(); - iterateListExist(); - render(); - - gl.glDisable( GL.GL_TEXTURE_2D ); - - if( ALLOWTRAILS ) - iterateListRenderTrails(); - } - - void findVelocity(){ - Vec3D dirToMouse = new Vec3D( mouse.loc.sub( loc ).scale( .15 ) ); - vel.set( dirToMouse ); - } - - void setPosition(){ - loc.addSelf( vel ); - - if( ALLOWFLOOR ){ - if( loc.y > floorLevel ){ - loc.y = floorLevel; - vel.y = 0; - } - } - } - - void iterateListExist(){ - gl.glEnable( GL.GL_TEXTURE_2D ); - - - int mylength = particles.size(); - for( int i=mylength-1; i>=0; i-- ){ - Particle p = ( Particle )particles.get(i); - if( p.ISSPLIT ) - addParticles( p ); - - if ( !p.ISDEAD ){ - // pgl.bindTexture( images.particle ); - particleTex.bind(); - particleTex.enable(); - p.exist(); - particleTex.disable(); - - } - else { - particles.set( i, particles.get( particles.size() - 1 ) ); - particles.remove( particles.size() - 1 ); - } - } - - if( ALLOWFLOOR ){ - // pgl.bindTexture( images.reflection ); - reflectionTex.bind(); - reflectionTex.enable(); - for( Iterator it = particles.iterator(); it.hasNext(); ){ - Particle p = (Particle) it.next(); - p.renderReflection(); - } - reflectionTex.disable(); - } - - // pgl.bindTexture( images.corona ); - coronaTex.bind(); - coronaTex.enable(); - for( Iterator it = nebulae.iterator(); it.hasNext(); ){ - Nebula n = (Nebula) it.next(); - if( !n.ISDEAD ){ - n.exist(); - } - else { - it.remove(); - } - } - coronaTex.disable(); - } - - - void render(){ - // pgl.bindTexture( images.emitter ); - emitterTex.bind(); - emitterTex.enable(); - renderImage( loc, radius, myColor, 1.0 ); - emitterTex.enable(); - - if( ALLOWNEBULA ){ - nebulae.add( new Nebula( loc, 15.0, true ) ); - nebulae.add( new Nebula( loc, 45.0, true ) ); - } - - - if( ALLOWFLOOR ){ - // pgl.bindTexture( images.reflection ); - reflectionTex.bind(); - reflectionTex.enable(); - renderReflection(); - reflectionTex.disable(); - } - } - - void renderReflection(){ - float altitude = floorLevel - loc.y; - float reflectMaxAltitude = 300.0; - float yPer = 1.0 - altitude/reflectMaxAltitude; - - if( yPer > .05 ) - renderImageOnFloor( new Vec3D( loc.x, floorLevel, loc.z ), radius * 10.0, color( 0.5, 1.0, yPer*.25 ), yPer ); - - if( mousePressed ) - renderImageOnFloor( new Vec3D( loc.x, floorLevel, loc.z ), radius + ( yPer + 1.0 ) * radius * random( 2.0, 3.5 ), color( 1.0, 0, 0 ), yPer ); - } - - void iterateListRenderTrails(){ - for( Iterator it = particles.iterator(); it.hasNext(); ){ - Particle p = (Particle) it.next(); - p.renderTrails(); - } - } - - void addParticles( int _amt ){ - for( int i=0; i<_amt; i++ ){ - particles.add( new Particle( 1, loc, vel ) ); - } - - if( ALLOWNEBULA ){ - nebulae.add( new Nebula( loc, 40.0, false ) ); - nebulae.add( new Nebula( loc, 100.0, false ) ); - } - } - - void addParticles( Particle _p ){ - // play with amt if you want to control how many particles spawn when splitting - int amt = (int)( _p.radius * .15 ); - for( int i=0; i maxNoise) maxNoise = rads; - - rads -= minNoise; - rads *= 1.0/(maxNoise - minNoise); - - return rads * mult; -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/images.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/images.pde deleted file mode 100644 index 821c91230..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/images.pde +++ /dev/null @@ -1,13 +0,0 @@ -class Images{ - PImage particle; - PImage emitter; - PImage corona; - PImage reflection; - - Images(){ - particle = loadImage( "particle.png" ); - emitter = loadImage( "emitter.png" ); - corona = loadImage( "corona.png" ); - reflection = loadImage( "reflection.png" ); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/nebula.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/nebula.pde deleted file mode 100644 index 573378708..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/nebula.pde +++ /dev/null @@ -1,56 +0,0 @@ -class Nebula{ - Vec3D loc; - Vec3D vel; - float radius; - float scaleFac; - float age; - int lifeSpan; - float agePer; - float rot; - color c; - - boolean ISDEAD; - boolean ISGROUNDED; - - Nebula( Vec3D _loc, float _radius, boolean _ISGROUNDED ){ - loc = new Vec3D( _loc ); - vel = new Vec3D( pov.eyeNormal.scale( 2.0 ) ); - radius = random( _radius*.8, _radius*1.75 ); - - scaleFac = random( 1.005, 1.10 ); - age = 0; - lifeSpan = (int)random(10,30); - rot = random( TWO_PI ); - c = color( random(.75, 1.0), random(.5,.75), random(.2,.8) ); - ISGROUNDED = _ISGROUNDED; - - if( ISGROUNDED ){ - scaleFac = random( 1.01, 1.025 ); - vel.y -= random( 1.0 ); - radius *= 2.0; - } - } - - void exist(){ - move(); - render(); - checkAge(); - } - - void move(){ - radius *= scaleFac; - loc.addSelf( vel ); - } - - void render(){ - renderImageAndRotate( loc, radius, c, sin(agePer*PI) * .4, rot ); - } - - void checkAge(){ - age ++; - agePer = 1.0 - age/(float)lifeSpan; - - if (age > lifeSpan) - ISDEAD = true; - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/particle.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/particle.pde deleted file mode 100644 index 53cbf839b..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/particle.pde +++ /dev/null @@ -1,171 +0,0 @@ - -class Particle{ - int len; // number of elements in position array - Vec3D[] loc; // array of position vectors - Vec3D startLoc; // just used to make sure every loc[] is initialized to the same position - Vec3D vel; // velocity vector - Vec3D perlin; // perlin noise vector - float radius; // particle's size - float age; // current age of particle - int lifeSpan; // max allowed age of particle - float agePer; // range from 1.0 (birth) to 0.0 (death) - int gen; // number of times particle has been involved in a SPLIT - float bounceAge; // amount to age particle when it bounces off floor - float bounceVel; // speed at impact - boolean ISDEAD; // if age == lifeSpan, make particle die - boolean ISBOUNCING; // if particle hits the floor... - boolean ISSPLIT; // if particle hits the floor with enough speed... - - - Particle( int _gen, Vec3D _loc, Vec3D _vel ){ - gen = _gen; - radius = random( 10 - gen, 50 - ( gen-1)*10 ); - - len = (int)( radius*.5 ); - loc = new Vec3D[ len ]; - startLoc = new Vec3D( _loc.add( new Vec3D().randomVector().scaleSelf( random( 1.0 ) ) ) ); - - for( int i=0; i 1 ){ - vel.addSelf( new Vec3D().randomVector().scaleSelf( random( 7.0 ) ) ); - } else { - vel.addSelf( new Vec3D().randomVector().scaleSelf( random( 10.0 ) ) ); - } - - perlin = new Vec3D(); - - age = 0; - bounceAge = 2; - lifeSpan = (int)( radius ); - } - - void exist(){ - if( ALLOWPERLIN ) - findPerlin(); - - findVelocity(); - setPosition(); - render(); - setAge(); - } - - void findPerlin(){ - float xyRads = getRads( loc[0].x, loc[0].z, 20.0, 50.0 ); - float yRads = getRads( loc[0].x, loc[0].y, 20.0, 50.0 ); - perlin.set( cos(xyRads), -sin(yRads), sin(xyRads) ); - perlin.scaleSelf( .5 ); - } - - void findVelocity(){ - if( ALLOWGRAVITY ) - vel.addSelf( gravity ); - - if( ALLOWPERLIN ) - vel.addSelf( perlin ); - - if( ALLOWFLOOR ){ - if( loc[0].y + vel.y > floorLevel ){ - ISBOUNCING = true; - } else { - ISBOUNCING = false; - } - } - - // if the particle is moving fast enough, when it hits the ground it can - // split into a bunch of smaller particles. - if( ISBOUNCING ){ - bounceVel = vel.magnitude(); - - vel.scaleSelf( .7 ); - vel.y *= -( ( radius/40.0 ) * .5 ); - - if( bounceVel > 15.0 && gen < 4 ) - ISSPLIT = true; - - } else { - ISSPLIT = false; - } - } - - void setPosition(){ - for( int i=len-1; i>0; i-- ){ - loc[i].set( loc[i-1] ); - } - - loc[0].addSelf( vel ); - } - - void render(){ - color c = color( agePer - .5, agePer*.25, 1.5 - agePer ); - renderImage( loc[0], radius * agePer, c, 1.0 ); - - // Rendering two graphics here. Makes the particles more vivid, - // but will hinder the performance. - c = color( 1, agePer, agePer ); - renderImage( loc[0], radius * agePer * .5, c, agePer ); - } - - void renderReflection(){ - float altitude = floorLevel - loc[0].y; - float reflectMaxAltitude = 25.0; - float yPer = ( 1.0 - ( altitude/reflectMaxAltitude ) ) * .5; - - if( yPer > .05 ) - renderImageOnFloor( new Vec3D( loc[0].x, floorLevel, loc[0].z ), radius * agePer * 8.0 * yPer, color( agePer, agePer*.25, 0 ), yPer + random( .2 ) ); - } - - void renderTrails(){ - float xp, yp, zp; - float xOff, yOff, zOff; - - gl.glBegin( GL.GL_QUAD_STRIP ); - - for ( int i=0; i lifeSpan ){ - ISDEAD = true; - } else { - agePer = 1.0 - age/(float)lifeSpan; - } - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/pov.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/pov.pde deleted file mode 100644 index 1d5bf01d7..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/pov.pde +++ /dev/null @@ -1,62 +0,0 @@ -// Camera class which uses Kristian Damkjer's OCD library -// http://www.cise.ufl.edu/~kdamkjer/processing/libraries/ocd/ - -class POV{ - PApplet parent; - Camera cam; - - Vec3D eye; - Vec3D center; - - Vec3D eyeNormal; - - boolean ISDRAGGING; - - POV( PApplet _parent ){ - parent = _parent; - cam = new Camera( parent, 0, 100, 1500 ); - - eye = new Vec3D(); - center = new Vec3D(); - eyeNormal = new Vec3D(); - } - - void exist(){ - perspective( PI/3.0, (float)xSize/(float)ySize, .5, 5000 ); - if( ISDRAGGING ){ - cam.circle( radians( ( mouseX - pmouseX ) * .25 ) ); - cam.arc( radians( ( mouseY - pmouseY ) * .25 ) ); - } - - cam.feed(); - setPosition(); - } - - - // Code by JohnG from the Processing forum - // http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1170790832 - // - // Does the camera transformations in reverse to allow for images that always face the camera. - void glReverseCamera(){ - float deltaX = eye.x - center.x; - float deltaY = eye.y - center.y; - float deltaZ = eye.z - center.z; - - float angleZ = atan2( deltaY,deltaX ); - float hyp = sqrt( sq( deltaX ) + sq( deltaY ) ); - float angleY = atan2( hyp,deltaZ ); - - gl.glRotatef( degrees( angleZ ), 0, 0, 1.0 ); - gl.glRotatef( degrees( angleY ), 0, 1.0, 0 ); - } - - - void setPosition(){ - float[] e = cam.position(); - float[] c = cam.target(); - - eye.set( e[0], e[1], e[2] ); - center.set( c[0], c[1], c[2] ); - eyeNormal = eye.sub(center).normalize(); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/sketch.properties b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_GLtexture/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/NOC_gl.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/NOC_gl.pde deleted file mode 100644 index 44705cdbd..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/NOC_gl.pde +++ /dev/null @@ -1,31 +0,0 @@ - -void renderImage(PImage img, Vec3D _loc, float _diam, color _col, float _alpha ) { - pushMatrix(); - translate( _loc.x, _loc.y, _loc.z ); - pov.glReverseCamera(); - tint(red(_col), green(_col), blue(_col), _alpha); - imageMode(CENTER); - image(img,0,0,_diam,_diam); - popMatrix(); -} - -void renderImageOnFloor(PImage img, Vec3D _loc, float _diam, color _col, float _aa ) { - pushMatrix(); - translate( _loc.x, _loc.y, _loc.z ); - rotateX(PI/2); - //pov.glReverseCamera(); - tint(red(_col), green(_col), blue(_col), _aa); - imageMode(CENTER); - image(img,0,0,_diam,_diam); - popMatrix(); -} - -void renderImageAndRotate(PImage img, Vec3D _loc, float _diam, color _col, float _aa, float _rot ) { - pushMatrix(); - translate( _loc.x, _loc.y, _loc.z ); - pov.glReverseCamera(); - tint(red(_col), green(_col), blue(_col), _aa); - imageMode(CENTER); - image(img,0,0,_diam,_diam); - popMatrix(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/cursor.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/cursor.pde deleted file mode 100644 index ee2909cb9..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/cursor.pde +++ /dev/null @@ -1,25 +0,0 @@ -class Cursor{ - Vec3D loc; - Vec3D vel; - - Cursor(){ - loc = new Vec3D(); - vel = new Vec3D(); - } - - void exist(){ - // 2.35 is an arbitrary number. Ideally, this cursor would function - // properly regardless of the camera's rotation and distance from the object. - // Im not sure how to make that happen... 3D interaction with the cursor has - // been low on my research list. Think of this as a crappy placeholder. - loc.set( ( mouseX - xMid ) * 2.25, ( mouseY - yMid ) * 2.25, 0 ); - } - - void render(){ - pushMatrix(); - translate( loc.x, loc.y, loc.z ); - fill( 1, 0, 0 ); - sphere( 10 ); - popMatrix(); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/emitter.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/emitter.pde deleted file mode 100644 index c22f45bdf..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/emitter.pde +++ /dev/null @@ -1,149 +0,0 @@ -class Emitter{ - Vec3D loc; - Vec3D vel; - Vec3D velToMouse; - float radius; - - color myColor; - - ArrayList particles; - ArrayList nebulae; - - Emitter( ){ - loc = new Vec3D(); - vel = new Vec3D(); - velToMouse = new Vec3D(); - - radius = 100; - - myColor = color( 1, 1, 1 ); - - particles = new ArrayList(); - nebulae = new ArrayList(); - } - - void exist(){ - findVelocity(); - setPosition(); - iterateListExist(); - render(); - - gl.glDisable( GL.GL_TEXTURE_2D ); - - if( ALLOWTRAILS ) - iterateListRenderTrails(); - } - - void findVelocity(){ - Vec3D dirToMouse = new Vec3D( mouse.loc.sub( loc ).scale( .15 ) ); - vel.set( dirToMouse ); - } - - void setPosition(){ - loc.addSelf( vel ); - - if( ALLOWFLOOR ){ - if( loc.y > floorLevel ){ - loc.y = floorLevel; - vel.y = 0; - } - } - } - - void iterateListExist(){ - gl.glEnable( GL.GL_TEXTURE_2D ); - - - int mylength = particles.size(); - for( int i=mylength-1; i>=0; i-- ){ - Particle p = ( Particle )particles.get(i); - if( p.ISSPLIT ) - addParticles( p ); - - if ( !p.ISDEAD ){ - //pgl.bindTexture( images.particle ); - p.exist(); - - } else { - particles.set( i, particles.get( particles.size() - 1 ) ); - particles.remove( particles.size() - 1 ); - } - } - - if( ALLOWFLOOR ){ - //pgl.bindTexture( images.reflection ); - for( Iterator it = particles.iterator(); it.hasNext(); ){ - Particle p = (Particle) it.next(); - p.renderReflection(); - } - } - - //pgl.bindTexture( images.corona ); - for( Iterator it = nebulae.iterator(); it.hasNext(); ){ - Nebula n = (Nebula) it.next(); - if( !n.ISDEAD ){ - n.exist(); - } else { - it.remove(); - } - } - } - - - void render(){ - //pgl.bindTexture( images.emitter ); - renderImage( images.emitter,loc, radius, myColor, 1.0 ); - - - if( ALLOWNEBULA ){ - nebulae.add( new Nebula( loc, 15.0, true ) ); - nebulae.add( new Nebula( loc, 45.0, true ) ); - } - - - if( ALLOWFLOOR ){ - //pgl.bindTexture( images.reflection ); - renderReflection(images.reflection); - } - } - - void renderReflection(PImage img){ - float altitude = floorLevel - loc.y; - float reflectMaxAltitude = 300.0; - float yPer = 1.0 - altitude/reflectMaxAltitude; - - if( yPer > .05 ) - renderImageOnFloor(img, new Vec3D( loc.x, floorLevel, loc.z ), radius * 10.0, color( 0.5, 1.0, yPer*.25 ), yPer ); - - if( mousePressed ) - renderImageOnFloor(img, new Vec3D( loc.x, floorLevel, loc.z ), radius + ( yPer + 1.0 ) * radius * random( 2.0, 3.5 ), color( 1.0, 0, 0 ), yPer ); - } - - void iterateListRenderTrails(){ - for( Iterator it = particles.iterator(); it.hasNext(); ){ - Particle p = (Particle) it.next(); - p.renderTrails(); - } - } - - void addParticles( int _amt ){ - for( int i=0; i<_amt; i++ ){ - particles.add( new Particle( 1, loc, vel ) ); - } - - if( ALLOWNEBULA ){ - nebulae.add( new Nebula( loc, 40.0, false ) ); - nebulae.add( new Nebula( loc, 100.0, false ) ); - } - } - - void addParticles( Particle _p ){ - // play with amt if you want to control how many particles spawn when splitting - int amt = (int)( _p.radius * .15 ); - for( int i=0; i maxNoise) maxNoise = rads; - - rads -= minNoise; - rads *= 1.0/(maxNoise - minNoise); - - return rads * mult; -} - diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/images.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/images.pde deleted file mode 100644 index 821c91230..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/images.pde +++ /dev/null @@ -1,13 +0,0 @@ -class Images{ - PImage particle; - PImage emitter; - PImage corona; - PImage reflection; - - Images(){ - particle = loadImage( "particle.png" ); - emitter = loadImage( "emitter.png" ); - corona = loadImage( "corona.png" ); - reflection = loadImage( "reflection.png" ); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/nebula.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/nebula.pde deleted file mode 100644 index 3412c110f..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/nebula.pde +++ /dev/null @@ -1,56 +0,0 @@ -class Nebula{ - Vec3D loc; - Vec3D vel; - float radius; - float scaleFac; - float age; - int lifeSpan; - float agePer; - float rot; - color c; - - boolean ISDEAD; - boolean ISGROUNDED; - - Nebula( Vec3D _loc, float _radius, boolean _ISGROUNDED ){ - loc = new Vec3D( _loc ); - vel = new Vec3D( pov.eyeNormal.scale( 2.0 ) ); - radius = random( _radius*.8, _radius*1.75 ); - - scaleFac = random( 1.005, 1.10 ); - age = 0; - lifeSpan = (int)random(10,30); - rot = random( TWO_PI ); - c = color( random(.75, 1.0), random(.5,.75), random(.2,.8) ); - ISGROUNDED = _ISGROUNDED; - - if( ISGROUNDED ){ - scaleFac = random( 1.01, 1.025 ); - vel.y -= random( 1.0 ); - radius *= 2.0; - } - } - - void exist(){ - move(); - render(); - checkAge(); - } - - void move(){ - radius *= scaleFac; - loc.addSelf( vel ); - } - - void render(){ - renderImageAndRotate(images.corona, loc, radius, c, sin(agePer*PI) * .4, rot ); - } - - void checkAge(){ - age ++; - agePer = 1.0 - age/(float)lifeSpan; - - if (age > lifeSpan) - ISDEAD = true; - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/particle.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/particle.pde deleted file mode 100644 index 893d3631f..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/particle.pde +++ /dev/null @@ -1,172 +0,0 @@ - -class Particle{ - int len; // number of elements in position array - Vec3D[] loc; // array of position vectors - Vec3D startLoc; // just used to make sure every loc[] is initialized to the same position - Vec3D vel; // velocity vector - Vec3D perlin; // perlin noise vector - float radius; // particle's size - float age; // current age of particle - int lifeSpan; // max allowed age of particle - float agePer; // range from 1.0 (birth) to 0.0 (death) - int gen; // number of times particle has been involved in a SPLIT - float bounceAge; // amount to age particle when it bounces off floor - float bounceVel; // speed at impact - boolean ISDEAD; // if age == lifeSpan, make particle die - boolean ISBOUNCING; // if particle hits the floor... - boolean ISSPLIT; // if particle hits the floor with enough speed... - - - Particle( int _gen, Vec3D _loc, Vec3D _vel ){ - gen = _gen; - radius = random( 10 - gen, 50 - ( gen-1)*10 ); - - len = (int)( radius*.5 ); - loc = new Vec3D[ len ]; - startLoc = new Vec3D( _loc.add( new Vec3D().randomVector().scaleSelf( random( 1.0 ) ) ) ); - - for( int i=0; i 1 ){ - vel.addSelf( new Vec3D().randomVector().scaleSelf( random( 7.0 ) ) ); - } else { - vel.addSelf( new Vec3D().randomVector().scaleSelf( random( 10.0 ) ) ); - } - - perlin = new Vec3D(); - - age = 0; - bounceAge = 2; - lifeSpan = (int)( radius ); - } - - void exist(){ - if( ALLOWPERLIN ) - findPerlin(); - - findVelocity(); - setPosition(); - render(); - setAge(); - } - - void findPerlin(){ - float xyRads = getRads( loc[0].x, loc[0].z, 20.0, 50.0 ); - float yRads = getRads( loc[0].x, loc[0].y, 20.0, 50.0 ); - perlin.set( cos(xyRads), -sin(yRads), sin(xyRads) ); - perlin.scaleSelf( .5 ); - } - - void findVelocity(){ - if( ALLOWGRAVITY ) - vel.addSelf( gravity ); - - if( ALLOWPERLIN ) - vel.addSelf( perlin ); - - if( ALLOWFLOOR ){ - if( loc[0].y + vel.y > floorLevel ){ - ISBOUNCING = true; - } else { - ISBOUNCING = false; - } - } - - // if the particle is moving fast enough, when it hits the ground it can - // split into a bunch of smaller particles. - if( ISBOUNCING ){ - bounceVel = vel.magnitude(); - - vel.scaleSelf( .7 ); - vel.y *= -( ( radius/40.0 ) * .5 ); - - if( bounceVel > 15.0 && gen < 4 ) - ISSPLIT = true; - - } else { - ISSPLIT = false; - } - } - - void setPosition(){ - for( int i=len-1; i>0; i-- ){ - loc[i].set( loc[i-1] ); - } - - loc[0].addSelf( vel ); - } - - void render(){ - color c = color( agePer - .5, agePer*.25, 1.5 - agePer ); - renderImage(images.particle, loc[0], radius * agePer, c, 1.0 ); - - // Rendering two graphics here. Makes the particles more vivid, - // but will hinder the performance. - c = color( 1, agePer, agePer ); - renderImage(images.particle, loc[0], radius * agePer * .5, c, agePer ); - } - - void renderReflection(){ - float altitude = floorLevel - loc[0].y; - float reflectMaxAltitude = 25.0; - float yPer = ( 1.0 - ( altitude/reflectMaxAltitude ) ) * .5; - - if( yPer > .05 ) - renderImageOnFloor(images.particle, new Vec3D( loc[0].x, floorLevel, loc[0].z ), radius * agePer * 8.0 * yPer, color( agePer, agePer*.25, 0 ), yPer + random( .2 ) ); - } - - void renderTrails(){ - float xp, yp, zp; - float xOff, yOff, zOff; - - beginShape(QUAD_STRIP); - - for ( int i=0; i lifeSpan ){ - ISDEAD = true; - } else { - agePer = 1.0 - age/(float)lifeSpan; - } - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/pov.pde b/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/pov.pde deleted file mode 100644 index cc41c1360..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/flight404/flight404_particles_2_simple/pov.pde +++ /dev/null @@ -1,64 +0,0 @@ -// Camera class which uses Kristian Damkjer's OCD library -// http://www.cise.ufl.edu/~kdamkjer/processing/libraries/ocd/ - -class POV{ - PApplet parent; - Camera cam; - - Vec3D eye; - Vec3D center; - - Vec3D eyeNormal; - - boolean ISDRAGGING; - - POV( PApplet _parent ){ - parent = _parent; - cam = new Camera( parent, 0, -100, 1500 ); - - eye = new Vec3D(); - center = new Vec3D(); - eyeNormal = new Vec3D(); - } - - void exist(){ - perspective( PI/3.0, (float)xSize/(float)ySize, .5, 5000 ); - if( ISDRAGGING ){ - cam.circle( radians( ( mouseX - pmouseX ) * .25 ) ); - cam.arc( radians( ( mouseY - pmouseY ) * .25 ) ); - } - - cam.feed(); - setPosition(); - } - - - // Code by JohnG from the Processing forum - // http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1170790832 - // - // Does the camera transformations in reverse to allow for images that always face the camera. - void glReverseCamera(){ - float deltaX = eye.x - center.x; - float deltaY = eye.y - center.y; - float deltaZ = eye.z - center.z; - - float angleZ = atan2( deltaY,deltaX ); - float hyp = sqrt( sq( deltaX ) + sq( deltaY ) ); - float angleY = atan2( hyp,deltaZ ); - - rotateZ(angleZ); - rotateY(angleY); - //gl.glRotatef( degrees( angleZ ), 0, 0, 1.0 ); - //gl.glRotatef( degrees( angleY ), 0, 1.0, 0 ); - } - - - void setPosition(){ - float[] e = cam.position(); - float[] c = cam.target(); - - eye.set( e[0], e[1], e[2] ); - center.set( c[0], c[1], c[2] ); - eyeNormal = eye.sub(center).normalize(); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Circle.pde b/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Circle.pde deleted file mode 100644 index a25a7fa3d..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Circle.pde +++ /dev/null @@ -1,36 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-1: Inheritance - -class Circle extends Shape { - - // Inherits all instance variables from parent + adding one - color c; - - Circle(float x_, float y_, float r_, color c_) { - super(x_,y_,r_); // Call the parent constructor - c = c_; // Also deal with this new instance variable - } - - // Call the parent jiggle, but do some more stuff too - void jiggle() { - super.jiggle(); - // The Circle jiggles its size as well as its x,y location. - r += random(-1,1); - r = constrain(r,0,100); - } - - // The changeColor() function is unique to the Circle class. - void changeColor() { - c = color(random(255)); - } - - void display() { - ellipseMode(CENTER); - fill(c); - stroke(0); - ellipse(x,y,r,r); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Shape.pde b/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Shape.pde deleted file mode 100644 index a4d8a723a..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Shape.pde +++ /dev/null @@ -1,28 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-1: Inheritance - -class Shape { - float x; - float y; - float r; - - Shape(float x_, float y_, float r_) { - x = x_; - y = y_; - r = r_; - } - - void jiggle() { - x += random(-1,1); - y += random(-1,1); - } - - // A generic shape does not really know how to be displayed. - // This will be overridden in the child classes. - void display() { - point(x,y); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Square.pde b/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Square.pde deleted file mode 100644 index 51209d3da..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/Square.pde +++ /dev/null @@ -1,25 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-1: Inheritance - -class Square extends Shape { - // Variables are inherited from the parent. - // We could also add variables unique to the Square class if we so desire - - Square(float x_, float y_, float r_) { - // If the parent constructor takes arguments then super() needs to pass in those arguments. - super(x_,y_,r_); - } - - // Inherits jiggle() from parent - - // The square overrides its parent for display. - void display() { - rectMode(CENTER); - fill(175); - stroke(0); - rect(x,y,r,r); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/simpleInheritance.pde b/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/simpleInheritance.pde deleted file mode 100644 index f5ff6dd60..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simpleInheritance/simpleInheritance.pde +++ /dev/null @@ -1,27 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-1: Inheritance - -// Object oriented programming allows us to defi ne classes in terms of other classes. -// A class can be a subclass (aka " child " ) of a super class (aka "parent"). -// This is a simple example demonstrating this concept, known as "inheritance." -Square s; -Circle c; - -void setup() { - size(200,200); - smooth(); - // A square and circle - s = new Square(75,75,10); - c = new Circle(125,125,20,color(175)); -} - -void draw() { - background(255); - c.jiggle(); - s.jiggle(); - c.display(); - s.display(); -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Circle.pde b/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Circle.pde deleted file mode 100644 index 538f27a4a..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Circle.pde +++ /dev/null @@ -1,36 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-2: Polymorphism - -class Circle extends Shape { - - // Inherits all instance variables from parent + adding one - color c; - - Circle(float x_, float y_, float r_, color c_) { - super(x_,y_,r_); // Call the parent constructor - c = c_; // Also deal with this new instance variable - } - - // Call the parent jiggle, but do some more stuff too - void jiggle() { - super.jiggle(); - // The Circle jiggles its size as well as its x,y location. - r += random(-1,1); - r = constrain(r,0,100); - } - - // The changeColor() function is unique to the Circle class. - void changeColor() { - c = color(random(255)); - } - - void display() { - ellipseMode(CENTER); - fill(c); - stroke(0); - ellipse(x,y,r,r); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Shape.pde b/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Shape.pde deleted file mode 100644 index 54c7fe4a3..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Shape.pde +++ /dev/null @@ -1,28 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-2: Polymorphism - -class Shape { - float x; - float y; - float r; - - Shape(float x_, float y_, float r_) { - x = x_; - y = y_; - r = r_; - } - - void jiggle() { - x += random(-1,1); - y += random(-1,1); - } - - // A generic shape does not really know how to be displayed. - // This will be overridden in the child classes. - void display() { - point(x,y); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Square.pde b/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Square.pde deleted file mode 100644 index 489b99073..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/Square.pde +++ /dev/null @@ -1,25 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-2: Polymorphism - -class Square extends Shape { - // Variables are inherited from the parent. - // We could also add variables unique to the Square class if we so desire - - Square(float x_, float y_, float r_) { - // If the parent constructor takes arguments then super() needs to pass in those arguments. - super(x_,y_,r_); - } - - // Inherits jiggle() from parent - - // The square overrides its parent for display. - void display() { - rectMode(CENTER); - fill(175); - stroke(0); - rect(x,y,r,r); - } -} diff --git a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/simplePolymorphism.pde b/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/simplePolymorphism.pde deleted file mode 100644 index add4ed517..000000000 --- a/java/examples/Books/Nature of Code/chp4_systems/simplePolymorphism/simplePolymorphism.pde +++ /dev/null @@ -1,32 +0,0 @@ -// Learning Processing -// Daniel Shiffman -// http://www.learningprocessing.com - -// Example 22-2: Polymorphism - -// One array of Shapes -Shape[] shapes = new Shape[30]; - -void setup() { - size(200,200); - smooth(); - - for (int i = 0; i < shapes.length; i++ ) { - int r = int(random(2)); - // Randomly put either circles or squares in our array - if (r == 0) { - shapes[i] = new Circle(100,100,10,color(random(255),100)); - } else { - shapes[i] = new Square(100,100,10); - } - } -} - -void draw() { - background(255); - // Jiggle and display all shapes - for (int i = 0; i < shapes.length; i++ ) { - shapes[i].jiggle(); - shapes[i].display(); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/CollisionsEqualMass.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/CollisionsEqualMass.pde deleted file mode 100644 index 4abce573e..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/CollisionsEqualMass.pde +++ /dev/null @@ -1,33 +0,0 @@ -// Collisions -- Elastic, Equal Mass, Two objects only -// Nature of Code, Spring 2009 -// Daniel Shiffman - -// Based off of Chapter 9: Resolving Collisions -// Mathematics and Physics for Programmers by Danny Kodicek - -// A Thing class for idealized collisions - -Mover a; -Mover b; - -boolean showVectors = true; - -void setup() { - size(200,200); - smooth(); - a = new Mover(new PVector(random(5),random(-5,5)),new PVector(10,10)); - b = new Mover(new PVector(-2,1),new PVector(150,150)); -} - -void draw() { - background(255); - a.go(); - b.go(); - - // Note this function will ONLY WORK with two objects - // Needs to be revised in the case of an array of objects - a.collideEqualMass(b); -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/Mover.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/Mover.pde deleted file mode 100644 index 48d6389b3..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/Mover.pde +++ /dev/null @@ -1,99 +0,0 @@ -// Collisions -// Daniel Shiffman - -// A Thing class for idealized collision - -class Mover { - - PVector loc; - PVector vel; - float bounce = 1.0; - float r = 20; - boolean colliding = false; - - Mover(PVector v, PVector l) { - vel = v.get(); - loc = l.get(); - } - - // Main method to operate object - void go() { - update(); - borders(); - display(); - } - - // Method to update location - void update() { - loc.add(vel); - } - - // Check for bouncing off borders - void borders() { - if (loc.y > height) { - vel.y *= -bounce; - loc.y = height; - } - else if (loc.y < 0) { - vel.y *= -bounce; - loc.y = 0; - } - if (loc.x > width) { - vel.x *= -bounce; - loc.x = width; - } - else if (loc.x < 0) { - vel.x *= -bounce; - loc.x = 0; - } - } - - // Method to display - void display() { - ellipseMode(CENTER); - stroke(0); - fill(175,200); - ellipse(loc.x,loc.y,r*2,r*2); - if (showVectors) { - drawVector(vel,loc,10); - } - } - - void collideEqualMass(Mover other) { - float d = PVector.dist(loc,other.loc); - float sumR = r + other.r; - // Are they colliding? - if (!colliding && d < sumR) { - // Yes, make new velocities! - colliding = true; - // Direction of one object another - PVector n = PVector.sub(other.loc,loc); - n.normalize(); - - // Difference of velocities so that we think of one object as stationary - PVector u = PVector.sub(vel,other.vel); - - // Separate out components -- one in direction of normal - PVector un = componentVector(u,n); - // Other component - u.sub(un); - // These are the new velocities plus the velocity of the object we consider as stastionary - vel = PVector.add(u,other.vel); - other.vel = PVector.add(un,other.vel); - } - else if (d > sumR) { - colliding = false; - } - } -} - -PVector componentVector (PVector vector, PVector directionVector) { - //--! ARGUMENTS: vector, directionVector (2D vectors) - //--! RETURNS: the component vector of vector in the direction directionVector - //-- normalize directionVector - directionVector.normalize(); - directionVector.mult(vector.dot(directionVector)); - return directionVector; -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/drawVector.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/drawVector.pde deleted file mode 100644 index c3440240d..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/CollisionsEqualMass/drawVector.pde +++ /dev/null @@ -1,21 +0,0 @@ -void drawVector(PVector v, PVector loc, float scayl) { - pushMatrix(); - float arrowsize = 4; - // Translate to location to render vector - translate(loc.x,loc.y); - stroke(0); - // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate - rotate(v.heading2D()); - // 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(); -} - - -void mousePressed() { - showVectors = !showVectors; -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Blob.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Blob.pde deleted file mode 100644 index 43c0584a0..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Blob.pde +++ /dev/null @@ -1,196 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A blob skeleton -// Could be used to create blobbly characters a la Nokia Friends -// http://postspectacular.com/work/nokia/friends/start - -class Skeleton { - - // A list to keep track of all the bodies and joints - ArrayList bodies; - ArrayList joints; - - float bodyRadius; // The radius of each body that makes up the skeleton - float radius; // The radius of the entire blob - float totalPoints; // How many points make up the blob - - - // We should modify this constructor to receive arguments - // So that we can make many different types of blobs - Skeleton() { - - // Create the empty ArrayLists - bodies = new ArrayList(); - joints = new ArrayList(); - - // Where and how big is the blob - Vec2 center = new Vec2(width/2, height/2); - radius = 100; - totalPoints = 32; - bodyRadius = 10; - - // Initialize all the points in a circle - for (int i = 0; i < totalPoints; i++) { - // Look polar to cartesian coordinate transformation! - float theta = PApplet.map(i, 0, totalPoints, 0, TWO_PI); - float x = center.x + radius * sin(theta); - float y = center.y + radius * cos(theta); - - // Make each individual body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - - bd.fixedRotation = true; // no rotation! - bd.position.set(box2d.coordPixelsToWorld(x, y)); - Body body = box2d.createBody(bd); - - // The body is a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(bodyRadius); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - fd.density = 1; - fd.friction = 0.5; - fd.restitution = 0.3; - - // Finalize the body - body.createFixture(fd); - - // Store our own copy for later rendering - bodies.add(body); - } - - // Now connect the outline of the shape all with joints - for (int i = 0; i < bodies.size(); i++) { - DistanceJointDef djd = new DistanceJointDef(); - Body a = bodies.get(i); - int next = i+1; - if (i == bodies.size()-1) { - next = 0; - } - Body b = bodies.get(next); - // Connection between previous particle and this one - djd.bodyA = a; - djd.bodyB = b; - // Equilibrium length is distance between these bodies - Vec2 apos = a.getWorldCenter(); - Vec2 bpos = b.getWorldCenter(); - float d = dist(apos.x, apos.y, bpos.x, bpos.y); - djd.length = d; - // These properties affect how springy the joint is - djd.frequencyHz = 10; - djd.dampingRatio = 0.9; - - // Make the joint. - DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd); - joints.add(dj); - } - - - // Make some joints that cross the center of the blob between bodies - for (int i = 0; i < bodies.size(); i++) { - for (int j = i+2; j < bodies.size(); j+=4) { - DistanceJointDef djd = new DistanceJointDef(); - Body a = bodies.get(i); - Body b = bodies.get(j); - // Connection between two bides - djd.bodyA = a; - djd.bodyB = b; - // Equilibrium length is distance between these bodies - Vec2 apos = a.getWorldCenter(); - Vec2 bpos = b.getWorldCenter(); - float d = dist(apos.x, apos.y, bpos.x, bpos.y); - - djd.length = d; - // These properties affect how springy the joint is - djd.frequencyHz = 3; - djd.dampingRatio = 0.1; - - // Make the joint. - DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd); - joints.add(dj); - } - } - } - - - // Draw the skeleton as circles for bodies and lines for joints - void displaySkeleton() { - // Draw the outline - stroke(0); - strokeWeight(1); - for (Joint j: joints) { - Body a = j.getBodyA(); - Body b = j.getBodyB(); - Vec2 posa = box2d.getBodyPixelCoord(a); - Vec2 posb = box2d.getBodyPixelCoord(b); - line(posa.x, posa.y, posb.x, posb.y); - } - - // Draw the individual circles - for (Body b: bodies) { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(b); - // Get its angle of rotation - float a = b.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - rotate(a); - fill(175); - stroke(0); - strokeWeight(1); - ellipse(0, 0, bodyRadius*2, bodyRadius*2); - popMatrix(); - } - } - - - // Draw it as a creature - void displayCreature() { - // Let's compute the center! - Vec2 center = new Vec2(0, 0); - - // Make a curvy polygon - beginShape(); - stroke(175); - strokeWeight(bodyRadius*2); - fill(175); - for (Body b: bodies) { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(b); - curveVertex(pos.x, pos.y); - center.addLocal(pos); - } - endShape(CLOSE); - // Center is average of all points - center.mulLocal(1.0/bodies.size()); - - // Find angle between center and side body - Vec2 pos = box2d.getBodyPixelCoord(bodies.get(0)); - float dx = pos.x - center.x; - float dy = pos.y - center.y; - float angle = atan2(dy, dx)-PI/2; - - // Draw eyes and mouth relative to center - pushMatrix(); - strokeWeight(1); - stroke(0); - translate(center.x, center.y); - rotate(angle); - fill(0); - ellipse(-25, -50, 16, 16); - ellipse(25, -50, 16, 16); - line(-50, 50, 50, 50); - popMatrix(); - } - - Body getFirstBody() { - return bodies.get(0); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/BlobSkeleton.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/BlobSkeleton.pde deleted file mode 100644 index b22248de8..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/BlobSkeleton.pde +++ /dev/null @@ -1,112 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A blob skeleton -// Could be used to create blobbly characters a la Nokia Friends -// http://postspectacular.com/work/nokia/friends/start - -import pbox2d.*; - -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.joints.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; - -// Our "blob" object -Skeleton blob; - -// Just a single box this time -Box box; -// The Spring that will attach to the box from the mouse -Spring spring; - -// Draw creature design or skeleton? -boolean skeleton; - -void setup() { - size(640, 360); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Add some boundaries - boundaries = new ArrayList(); - boundaries.add(new Boundary(width/2, height-5, width, 10)); - boundaries.add(new Boundary(width/2, 5, width, 10)); - boundaries.add(new Boundary(width-5, height/2, 10, height)); - boundaries.add(new Boundary(5, height/2, 10, height)); - - // Make a new blob - blob = new Skeleton(); - - // Make the box - box = new Box(width/2, 100); - - // Make the spring (it doesn't really get initialized until the mouse is clicked) - spring = new Spring(); -} - -// When the mouse is released we're done with the spring -void mouseReleased() { - spring.destroy(); -} - -// When the mouse is pressed we. . . -void mousePressed() { - // Check to see if the mouse was clicked on the box - if (box.contains(mouseX, mouseY)) { - // And if so, bind the mouse location to the box with a spring - spring.bind(mouseX, mouseY, box); - } -} - -void draw() { - background(255); - - // We must always step through time! - - box2d.step(); - - - // Show the blob! - if (skeleton) { - blob.displaySkeleton(); - } - else { - blob.displayCreature(); - } - - // Show the boundaries! - for (Boundary wall: boundaries) { - wall.display(); - } - - // Always alert the spring to the new mouse location - spring.update(mouseX, mouseY); - - // Draw the box - box.display(); - // Draw the spring (it only appears when active) - spring.display(); - - fill(0); - text("Space bar to toggle creature/skeleton.\nClick and drag the box.", 20, height-30); -} - - -void keyPressed() { - if (key == ' ') { - skeleton = !skeleton; - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Boundary.pde deleted file mode 100644 index 7b5012517..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Boundary.pde +++ /dev/null @@ -1,54 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Box.pde deleted file mode 100644 index b38eecedf..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Box.pde +++ /dev/null @@ -1,86 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A rectangular box - -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x_, float y_) { - float x = x_; - float y = y_; - w = 50; - h = 50; - // Add the box to the box2d world - makeBody(new Vec2(x, y), w, h); - body.setUserData(this); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - boolean contains(float x, float y) { - Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); - Fixture f = body.getFixtureList(); - boolean inside = f.testPoint(worldPoint); - return inside; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(PConstants.CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - fill(50); - stroke(0); - rect(0, 0, w, h); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - // Define and create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - //body.setMassFromShapes(); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Spring.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Spring.pde deleted file mode 100644 index 5fc777cc2..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/BlobSkeleton/Spring.pde +++ /dev/null @@ -1,76 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// Class to describe the spring joint (displayed as a line) - -class Spring { - - // This is the box2d object we need to create - MouseJoint mouseJoint; - - Spring() { - // At first it doesn't exist - mouseJoint = null; - } - - // If it exists we set its target to the mouse location - void update(float x, float y) { - if (mouseJoint != null) { - // Always convert to world coordinates! - Vec2 mouseWorld = box2d.coordPixelsToWorld(x,y); - mouseJoint.setTarget(mouseWorld); - } - } - - void display() { - if (mouseJoint != null) { - // We can get the two anchor points - Vec2 v1 = new Vec2(0,0); - mouseJoint.getAnchorA(v1); - Vec2 v2 = new Vec2(0,0); - mouseJoint.getAnchorB(v2); - // Convert them to screen coordinates - v1 = box2d.coordWorldToPixels(v1); - v2 = box2d.coordWorldToPixels(v2); - // And just draw a line - stroke(0); - strokeWeight(1); - line(v1.x,v1.y,v2.x,v2.y); - } - } - - - // This is the key function where - // we attach the spring to an x,y location - // and the Box object's location - void bind(float x, float y, Box box) { - // Define the joint - MouseJointDef md = new MouseJointDef(); - // Body A is just a fake ground body for simplicity (there isn't anything at the mouse) - md.bodyA = box2d.getGroundBody(); - // Body 2 is the box's boxy - md.bodyB = box.body; - // Get the mouse location in world coordinates - Vec2 mp = box2d.coordPixelsToWorld(x,y); - // And that's the target - md.target.set(mp); - // Some stuff about how strong and bouncy the spring should be - md.maxForce = 1000.0 * box.body.m_mass; - md.frequencyHz = 5.0; - md.dampingRatio = 0.9; - - // Make the joint! - mouseJoint = (MouseJoint) box2d.world.createJoint(md); - } - - void destroy() { - // We can get rid of the joint when the mouse is released - if (mouseJoint != null) { - box2d.world.destroyJoint(mouseJoint); - mouseJoint = null; - } - } - -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Blob.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Blob.pde deleted file mode 100644 index 3cc99fb8b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Blob.pde +++ /dev/null @@ -1,117 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A blob skeleton -// Could be used to create blobbly characters a la Nokia Friends -// http://postspectacular.com/work/nokia/friends/start - -class Blob { - - // A list to keep track of all the points in our blob - ArrayList skeleton; - - float bodyRadius; // The radius of each body that makes up the skeleton - float radius; // The radius of the entire blob - float totalPoints; // How many points make up the blob - - - // We should modify this constructor to receive arguments - // So that we can make many different types of blobs - Blob() { - - // Create the empty - skeleton = new ArrayList(); - - // Let's make a volume of joints! - ConstantVolumeJointDef cvjd = new ConstantVolumeJointDef(); - - // Where and how big is the blob - Vec2 center = new Vec2(width/2, height/2); - radius = 100; - totalPoints = 20; - bodyRadius = 12; - - - // Initialize all the points - for (int i = 0; i < totalPoints; i++) { - // Look polar to cartesian coordinate transformation! - float theta = PApplet.map(i, 0, totalPoints, 0, TWO_PI); - float x = center.x + radius * sin(theta); - float y = center.y + radius * cos(theta); - - // Make each individual body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - - bd.fixedRotation = true; // no rotation! - bd.position.set(box2d.coordPixelsToWorld(x, y)); - Body body = box2d.createBody(bd); - - // The body is a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(bodyRadius); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - - // For filtering out collisions - //fd.filter.groupIndex = -2; - - // Parameters that affect physics - fd.density = 1; - - // Finalize the body - body.createFixture(fd); - // Add it to the volume - cvjd.addBody(body); - - - // Store our own copy for later rendering - skeleton.add(body); - } - - // These parameters control how stiff vs. jiggly the blob is - cvjd.frequencyHz = 10.0f; - cvjd.dampingRatio = 1.0f; - - // Put the joint thing in our world! - box2d.world.createJoint(cvjd); - } - - - // Time to draw the blob! - // Can you make it a cute character, a la http://postspectacular.com/work/nokia/friends/start - void display() { - - // Draw the outline - beginShape(); - noFill(); - stroke(0); - strokeWeight(1); - for (Body b: skeleton) { - Vec2 pos = box2d.getBodyPixelCoord(b); - vertex(pos.x, pos.y); - } - endShape(CLOSE); - - // Draw the individual circles - for (Body b: skeleton) { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(b); - // Get its angle of rotation - float a = b.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - rotate(a); - fill(175); - stroke(0); - strokeWeight(1); - ellipse(0, 0, bodyRadius*2, bodyRadius*2); - popMatrix(); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Blobby.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Blobby.pde deleted file mode 100644 index 8ebcdbfda..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Blobby.pde +++ /dev/null @@ -1,66 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A blob skeleton -// Could be used to create blobbly characters a la Nokia Friends -// http://postspectacular.com/work/nokia/friends/start - -// This seems to be broken with the Box2D 2.1.2 version I'm using - -import pbox2d.*; - -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.joints.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; - -// Our "blob" object -Blob blob; - - void setup() { - size(400,300); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Add some boundaries - boundaries = new ArrayList(); - boundaries.add(new Boundary(width/2,height-5,width,10)); - boundaries.add(new Boundary(width/2,5,width,10)); - boundaries.add(new Boundary(width-5,height/2,10,height)); - boundaries.add(new Boundary(5,height/2,10,height)); - - // Make a new blob - blob = new Blob(); -} - - void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // Show the blob! - blob.display(); - - // Show the boundaries! - for (Boundary wall: boundaries) { - wall.display(); - } - - -} - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Boundary.pde deleted file mode 100644 index 7b5012517..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Blobby/Boundary.pde +++ /dev/null @@ -1,54 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/Boundary.pde deleted file mode 100644 index 06c5bf80d..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/Boundary.pde +++ /dev/null @@ -1,56 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - - b.setUserData(this); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/CollisionListeningDeletionExercise.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/CollisionListeningDeletionExercise.pde deleted file mode 100644 index b99e9108e..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/CollisionListeningDeletionExercise.pde +++ /dev/null @@ -1,121 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of controlling an object with our own motion (by attaching a MouseJoint) -// Also demonstrates how to know which object was hit - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -Boundary wall; - -void setup() { - size(400, 300); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Turn on collision listening! - box2d.listenForCollisions(); - - // Create the empty list - particles = new ArrayList(); - - wall = new Boundary(width/2, height-5, width, 10); -} - -void draw() { - background(255); - - if (random(1) < 0.1) { - float sz = random(4, 8); - particles.add(new Particle(random(width), 20, sz)); - } - - - // We must always step through time! - box2d.step(); - - // Look at all particles - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - p.display(); - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - if (p.done()) { - particles.remove(i); - } - } - - wall.display(); -} - - -// Collision event functions! -void beginContact(Contact cp) { - // Get both shapes - Fixture f1 = cp.getFixtureA(); - Fixture f2 = cp.getFixtureB(); - // Get both bodies - Body b1 = f1.getBody(); - Body b2 = f2.getBody(); - - // Get our objects that reference these bodies - Object o1 = b1.getUserData(); - Object o2 = b2.getUserData(); - - if (o1.getClass() == Particle.class && o2.getClass() == Particle.class) { - Particle p1 = (Particle) o1; - p1.delete(); - Particle p2 = (Particle) o2; - p2.delete(); - } - - if (o1.getClass() == Boundary.class) { - Particle p = (Particle) o2; - p.change(); - } - if (o2.getClass() == Boundary.class) { - Particle p = (Particle) o1; - p.change(); - } - - -} - -// Objects stop touching each other -void endContact(Contact cp) { -} - - - - - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/Particle.pde deleted file mode 100644 index 7fe629d81..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionListeningDeletionExercise/Particle.pde +++ /dev/null @@ -1,96 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - color col; - - boolean delete = false; - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x, y, r); - body.setUserData(this); - col = color(175); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - void delete() { - delete = true; - } - - // Change color when hit - void change() { - col = color(255, 0, 0); - } - - // Is the particle ready for deletion? - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2 || delete) { - killBody(); - return true; - } - return false; - } - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - rotate(a); - fill(col); - stroke(0); - strokeWeight(1); - ellipse(0, 0, r*2, r*2); - // Let's add a line so we can see the rotation - line(0, 0, r, 0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x, y); - bd.type = BodyType.DYNAMIC; - body = box2d.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - body.setAngularVelocity(random(-10, 10)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Box.pde deleted file mode 100644 index 8eab06230..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Box.pde +++ /dev/null @@ -1,86 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A rectangular box - -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x_, float y_) { - float x = x_; - float y = y_; - w = 24; - h = 24; - // Add the box to the box2d world - makeBody(new Vec2(x, y), w, h); - body.setUserData(this); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - boolean contains(float x, float y) { - Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); - Fixture f = body.getFixtureList(); - boolean inside = f.testPoint(worldPoint); - return inside; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(PConstants.CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - fill(175); - stroke(0); - rect(0, 0, w, h); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - // Define and create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - //body.setMassFromShapes(); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/CollisionsAndControl.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/CollisionsAndControl.pde deleted file mode 100644 index 7377f497f..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/CollisionsAndControl.pde +++ /dev/null @@ -1,153 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of controlling an object with our own motion (by attaching a MouseJoint) -// Also demonstrates how to know which object was hit - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// Just a single box this time -Box box; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -// The Spring that will attach to the box from the mouse -Spring spring; - -// Perlin noise values -float xoff = 0; -float yoff = 1000; - - -void setup() { - size(400,300); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Turn on collision listening! - box2d.listenForCollisions(); - - // Make the box - box = new Box(width/2,height/2); - - // Make the spring (it doesn't really get initialized until the mouse is clicked) - spring = new Spring(); - spring.bind(width/2,height/2,box); - - // Create the empty list - particles = new ArrayList(); - - -} - -void draw() { - background(255); - - if (random(1) < 0.2) { - float sz = random(4,8); - particles.add(new Particle(width/2,-20,sz)); - } - - - // We must always step through time! - box2d.step(); - - // Make an x,y coordinate out of perlin noise - float x = noise(xoff)*width; - float y = noise(yoff)*height; - xoff += 0.01; - yoff += 0.01; - - // This is tempting but will not work! - // box.body.setXForm(box2d.screenToWorld(x,y),0); - - // Instead update the spring which pulls the mouse along - if (mousePressed) { - spring.update(mouseX,mouseY); - spring.display(); - } else { - spring.update(x,y); - } - box.body.setAngularVelocity(0); - - // Look at all particles - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - p.display(); - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - if (p.done()) { - particles.remove(i); - } - } - - // Draw the box - box.display(); - - // Draw the spring - // spring.display(); -} - - -// Collision event functions! -void beginContact(Contact cp) { - // Get both fixtures - Fixture f1 = cp.getFixtureA(); - Fixture f2 = cp.getFixtureB(); - // Get both bodies - Body b1 = f1.getBody(); - Body b2 = f2.getBody(); - // Get our objects that reference these bodies - Object o1 = b1.getUserData(); - Object o2 = b2.getUserData(); - - // If object 1 is a Box, then object 2 must be a particle - // Note we are ignoring particle on particle collisions - if (o1.getClass() == Box.class) { - Particle p = (Particle) o2; - p.change(); - } - // If object 2 is a Box, then object 1 must be a particle - else if (o2.getClass() == Box.class) { - Particle p = (Particle) o1; - p.change(); - } -} - - -// Objects stop touching each other -void endContact(Contact cp) { -} - - - - - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Particle.pde deleted file mode 100644 index d21b5f819..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Particle.pde +++ /dev/null @@ -1,92 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - color col; - - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x, y, r); - body.setUserData(this); - col = color(175); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Change color when hit - void change() { - col = color(255, 0, 0); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - rotate(a); - fill(col); - stroke(0); - strokeWeight(1); - ellipse(0, 0, r*2, r*2); - // Let's add a line so we can see the rotation - line(0, 0, r, 0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x, y); - bd.type = BodyType.DYNAMIC; - body = box2d.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - body.setAngularVelocity(random(-10, 10)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Spring.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Spring.pde deleted file mode 100644 index a9fcbc6c5..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControl/Spring.pde +++ /dev/null @@ -1,82 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// Class to describe the spring joint (displayed as a line) - -class Spring { - - // This is the box2d object we need to create - MouseJoint mouseJoint; - - Spring() { - // At first it doesn't exist - mouseJoint = null; - } - - // If it exists we set its target to the mouse location - void update(float x, float y) { - if (mouseJoint != null) { - // Always convert to world coordinates! - Vec2 mouseWorld = box2d.coordPixelsToWorld(x,y); - mouseJoint.setTarget(mouseWorld); - } - } - - void display() { - if (mouseJoint != null) { - // We can get the two anchor points - Vec2 v1 = new Vec2(0,0); - mouseJoint.getAnchorA(v1); - Vec2 v2 = new Vec2(0,0); - mouseJoint.getAnchorB(v2); - // Convert them to screen coordinates - v1 = box2d.coordWorldToPixels(v1); - v2 = box2d.coordWorldToPixels(v2); - // And just draw a line - stroke(0); - strokeWeight(1); - line(v1.x,v1.y,v2.x,v2.y); - } - } - - - // This is the key function where - // we attach the spring to an x,y location - // and the Box object's location - void bind(float x, float y, Box box) { - // Define the joint - MouseJointDef md = new MouseJointDef(); - - // Body A is just a fake ground body for simplicity (there isn't anything at the mouse) - md.bodyA = box2d.getGroundBody(); - // Body 2 is the box's boxy - md.bodyB = box.body; - // Get the mouse location in world coordinates - Vec2 mp = box2d.coordPixelsToWorld(x,y); - // And that's the target - md.target.set(mp); - // Some stuff about how strong and bouncy the spring should be - md.maxForce = 1000.0 * box.body.m_mass; - md.frequencyHz = 5.0; - md.dampingRatio = 0.9; - - // Wake up body! - //box.body.wakeUp(); - - // Make the joint! - mouseJoint = (MouseJoint) box2d.world.createJoint(md); - } - - void destroy() { - // We can get rid of the joint when the mouse is released - if (mouseJoint != null) { - box2d.world.destroyJoint(mouseJoint); - mouseJoint = null; - } - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Box.pde deleted file mode 100644 index 8eab06230..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Box.pde +++ /dev/null @@ -1,86 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A rectangular box - -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x_, float y_) { - float x = x_; - float y = y_; - w = 24; - h = 24; - // Add the box to the box2d world - makeBody(new Vec2(x, y), w, h); - body.setUserData(this); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - boolean contains(float x, float y) { - Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); - Fixture f = body.getFixtureList(); - boolean inside = f.testPoint(worldPoint); - return inside; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(PConstants.CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - fill(175); - stroke(0); - rect(0, 0, w, h); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - // Define and create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - //body.setMassFromShapes(); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/CollisionsAndControlInterface.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/CollisionsAndControlInterface.pde deleted file mode 100644 index 64c6ec3e9..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/CollisionsAndControlInterface.pde +++ /dev/null @@ -1,114 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// Basic example of controlling an object with our own motion (by attaching a MouseJoint) -// Also demonstrates how to know which object was hit - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// Just a single box this time -Box box; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -// The Spring that will attach to the box from the mouse -Spring spring; - -// Perlin noise values -float xoff = 0; -float yoff = 1000; - - -void setup() { - size(400,300); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Add a listener to listen for collisions! - box2d.world.setContactListener(new CustomListener()); - - // Make the box - box = new Box(width/2,height/2); - - // Make the spring (it doesn't really get initialized until the mouse is clicked) - spring = new Spring(); - spring.bind(width/2,height/2,box); - - // Create the empty list - particles = new ArrayList(); - - -} - -void draw() { - background(255); - - if (random(1) < 0.2) { - float sz = random(4,8); - particles.add(new Particle(width/2,-20,sz)); - } - - - // We must always step through time! - box2d.step(); - - // Make an x,y coordinate out of perlin noise - float x = noise(xoff)*width; - float y = noise(yoff)*height; - xoff += 0.01; - yoff += 0.01; - - // This is tempting but will not work! - // box.body.setXForm(box2d.screenToWorld(x,y),0); - - // Instead update the spring which pulls the mouse along - if (mousePressed) { - spring.update(mouseX,mouseY); - } else { - spring.update(x,y); - } - //box.body.setAngularVelocity(0); - - // Look at all particles - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - p.display(); - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - if (p.done()) { - particles.remove(i); - } - } - - // Draw the box - box.display(); - - // Draw the spring - // spring.display(); -} - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/ContactListener.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/ContactListener.pde deleted file mode 100644 index dafe17f7e..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/ContactListener.pde +++ /dev/null @@ -1,57 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// ContactListener to listen for collisions! - -import org.jbox2d.callbacks.ContactImpulse; -import org.jbox2d.callbacks.ContactListener; -import org.jbox2d.collision.Manifold; -import org.jbox2d.dynamics.contacts.Contact; - - class CustomListener implements ContactListener { - CustomListener() { - } - - // This function is called when a new collision occurs - void beginContact(Contact cp) { - // Get both fixtures - Fixture f1 = cp.getFixtureA(); - Fixture f2 = cp.getFixtureB(); - // Get both bodies - Body b1 = f1.getBody(); - Body b2 = f2.getBody(); - // Get our objects that reference these bodies - Object o1 = b1.getUserData(); - Object o2 = b2.getUserData(); - - // If object 1 is a Box, then object 2 must be a particle - // Note we are ignoring particle on particle collisions - if (o1.getClass() == Box.class) { - Particle p = (Particle) o2; - p.change(); - } - // If object 2 is a Box, then object 1 must be a particle - else if (o2.getClass() == Box.class) { - Particle p = (Particle) o1; - p.change(); - } - } - - void endContact(Contact contact) { - // TODO Auto-generated method stub - } - - void preSolve(Contact contact, Manifold oldManifold) { - // TODO Auto-generated method stub - } - - void postSolve(Contact contact, ContactImpulse impulse) { - // TODO Auto-generated method stub - } -} - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Particle.pde deleted file mode 100644 index d21b5f819..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Particle.pde +++ /dev/null @@ -1,92 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - color col; - - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x, y, r); - body.setUserData(this); - col = color(175); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Change color when hit - void change() { - col = color(255, 0, 0); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - rotate(a); - fill(col); - stroke(0); - strokeWeight(1); - ellipse(0, 0, r*2, r*2); - // Let's add a line so we can see the rotation - line(0, 0, r, 0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x, y); - bd.type = BodyType.DYNAMIC; - body = box2d.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - body.setAngularVelocity(random(-10, 10)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Spring.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Spring.pde deleted file mode 100644 index 62cac8a4b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/CollisionsAndControlInterface/Spring.pde +++ /dev/null @@ -1,82 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// Class to describe the spring joint (displayed as a line) - -class Spring { - - // This is the box2d object we need to create - MouseJoint mouseJoint; - - Spring() { - // At first it doesn't exist - mouseJoint = null; - } - - // If it exists we set its target to the mouse location - void update(float x, float y) { - if (mouseJoint != null) { - // Always convert to world coordinates! - Vec2 mouseWorld = box2d.coordPixelsToWorld(x,y); - mouseJoint.setTarget(mouseWorld); - } - } - - void display() { - if (mouseJoint != null) { - // We can get the two anchor points - Vec2 v1 = null; - mouseJoint.getAnchorA(v1); - Vec2 v2 = null; - mouseJoint.getAnchorB(v2); - // Convert them to screen coordinates - v1 = box2d.coordWorldToPixels(v1); - v2 = box2d.coordWorldToPixels(v2); - // And just draw a line - stroke(0); - strokeWeight(1); - line(v1.x,v1.y,v2.x,v2.y); - } - } - - - // This is the key function where - // we attach the spring to an x,y location - // and the Box object's location - void bind(float x, float y, Box box) { - // Define the joint - MouseJointDef md = new MouseJointDef(); - - // Body A is just a fake ground body for simplicity (there isn't anything at the mouse) - md.bodyA = box2d.getGroundBody(); - // Body 2 is the box's boxy - md.bodyB = box.body; - // Get the mouse location in world coordinates - Vec2 mp = box2d.coordPixelsToWorld(x,y); - // And that's the target - md.target.set(mp); - // Some stuff about how strong and bouncy the spring should be - md.maxForce = 1000.0f * box.body.m_mass; - md.frequencyHz = 5.0f; - md.dampingRatio = 0.9f; - - // Wake up body! - //box.body.wakeUp(); - - // Make the joint! - mouseJoint = (MouseJoint) box2d.world.createJoint(md); - } - - void destroy() { - // We can get rid of the joint when the mouse is released - if (mouseJoint != null) { - box2d.world.destroyJoint(mouseJoint); - mouseJoint = null; - } - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Boundary.pde deleted file mode 100644 index 7b5012517..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Boundary.pde +++ /dev/null @@ -1,54 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Box.pde deleted file mode 100644 index 9319bfbc7..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Box.pde +++ /dev/null @@ -1,97 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A rectangular box -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x, float y) { - w = random(8,16); - h = w; - // Add the box to the box2d world - makeBody(new Vec2(x,y),w,h); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+w*h) { - killBody(); - return true; - } - return false; - } - - void attract(float x,float y) { - // From BoxWrap2D example - Vec2 worldTarget = box2d.coordPixelsToWorld(x,y); - Vec2 bodyVec = body.getWorldCenter(); - // First find the vector going from this body to the specified point - worldTarget.subLocal(bodyVec); - // Then, scale the vector to the specified force - worldTarget.normalize(); - worldTarget.mulLocal((float) 50); - // Now apply it to the body's center of mass. - body.applyForce(worldTarget, bodyVec); - } - - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(CENTER); - pushMatrix(); - translate(pos.x,pos.y); - rotate(-a); - fill(175); - stroke(0); - rect(0,0,w,h); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - // Define the body and make it from the shape - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - - body = box2d.createBody(bd); - body.createFixture(fd); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Exercise_5_10_ApplyForceAttractMouse.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Exercise_5_10_ApplyForceAttractMouse.pde deleted file mode 100644 index fbc54c32e..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceAttractMouse/Exercise_5_10_ApplyForceAttractMouse.pde +++ /dev/null @@ -1,85 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of falling rectangles - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; -// A list for all of our rectangles -ArrayList boxes; - -void setup() { - size(640,360); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -20); - - // Create ArrayLists - boxes = new ArrayList(); - boundaries = new ArrayList(); - - // Add a bunch of fixed boundaries - boundaries.add(new Boundary(width/4,height-5,width/2-50,10)); - boundaries.add(new Boundary(3*width/4,height-5,width/2-50,10)); - boundaries.add(new Boundary(width-5,height/2,10,height)); - boundaries.add(new Boundary(5,height/2,10,height)); -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // When the mouse is clicked, add a new Box object - if (random(1) < 0.1) { - Box p = new Box(random(width),10); - boxes.add(p); - } - - if (mousePressed) { - for (Box b: boxes) { - b.attract(mouseX,mouseY); - } - } - - // Display all the boundaries - for (Boundary wall: boundaries) { - wall.display(); - } - - // Display all the boxes - for (Box b: boxes) { - b.display(); - } - - // Boxes that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = boxes.size()-1; i >= 0; i--) { - Box b = boxes.get(i); - if (b.done()) { - boxes.remove(i); - } - } - - fill(0); - text("Click mouse to attract boxes",20,20); -} - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Boundary.pde deleted file mode 100644 index 7b5012517..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Boundary.pde +++ /dev/null @@ -1,54 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Box.pde deleted file mode 100644 index 6dbb25b3f..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Box.pde +++ /dev/null @@ -1,88 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A rectangular box -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x, float y) { - w = random(8, 16); - h = w; - // Add the box to the box2d world - makeBody(new Vec2(x, y), w, h); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+w*h) { - killBody(); - return true; - } - return false; - } - - void applyForce(Vec2 force) { - Vec2 pos = body.getWorldCenter(); - body.applyForce(force, pos); - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - fill(175); - stroke(0); - rect(0, 0, w, h); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.2; - - // Define the body and make it from the shape - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - bd.angle = random(TWO_PI); - - body = box2d.createBody(bd); - body.createFixture(fd); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Exercise_5_10_ApplyForceSimpleWind.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Exercise_5_10_ApplyForceSimpleWind.pde deleted file mode 100644 index 95364b7da..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_ApplyForceSimpleWind/Exercise_5_10_ApplyForceSimpleWind.pde +++ /dev/null @@ -1,86 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of falling rectangles - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; -// A list for all of our rectangles -ArrayList boxes; - -void setup() { - size(640,360); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -20); - - // Create ArrayLists - boxes = new ArrayList(); - boundaries = new ArrayList(); - - // Add a bunch of fixed boundaries - boundaries.add(new Boundary(width/4,height-5,width/2-100,10)); - boundaries.add(new Boundary(3*width/4,height-5,width/2-100,10)); - boundaries.add(new Boundary(width-5,height/2,10,height)); - boundaries.add(new Boundary(5,height/2,10,height)); -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // When the mouse is clicked, add a new Box object - if (random(1) < 0.1) { - Box p = new Box(random(width),10); - boxes.add(p); - } - - if (mousePressed) { - for (Box b: boxes) { - Vec2 wind = new Vec2(20,0); - b.applyForce(wind); - } - } - - // Display all the boundaries - for (Boundary wall: boundaries) { - wall.display(); - } - - // Display all the boxes - for (Box b: boxes) { - b.display(); - } - - // Boxes that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = boxes.size()-1; i >= 0; i--) { - Box b = boxes.get(i); - if (b.done()) { - boxes.remove(i); - } - } - - fill(0); - text("Click mouse to apply a wind force.",20,20); -} - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Attractor.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Attractor.pde deleted file mode 100644 index 8de891160..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Attractor.pde +++ /dev/null @@ -1,70 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Showing how to use applyForce() with box2d - -// Fixed Attractor (this is redundant with Mover) - -class Attractor { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Attractor(float r_, float x, float y) { - r = r_; - // Define a body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - // Set its position - bd.position = box2d.coordPixelsToWorld(x,y); - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - body.createFixture(cs,1); - - } - - - // Formula for gravitational attraction - // We are computing this in "world" coordinates - // No need to convert to pixels and back - Vec2 attract(Mover m) { - float G = 100; // Strength of force - // clone() makes us a copy - Vec2 pos = body.getWorldCenter(); - Vec2 moverPos = m.body.getWorldCenter(); - // Vector pointing from mover to attractor - Vec2 force = pos.sub(moverPos); - float distance = force.length(); - // Keep force within bounds - distance = constrain(distance,1,5); - force.normalize(); - // Note the attractor's mass is 0 because it's fixed so can't use that - float strength = (G * 1 * m.body.m_mass) / (distance * distance); // Calculate gravitional force magnitude - force.mulLocal(strength); // Get force vector --> magnitude * direction - return force; - } - - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x,pos.y); - rotate(a); - fill(175); - stroke(0); - strokeWeight(2); - ellipse(0,0,r*2,r*2); - popMatrix(); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Exercise_5_10_AttractionApplyForce.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Exercise_5_10_AttractionApplyForce.pde deleted file mode 100644 index 7d7d49224..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Exercise_5_10_AttractionApplyForce.pde +++ /dev/null @@ -1,60 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Showing how to use applyForce() with box2d - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// Movers, jsut like before! -Mover[] movers = new Mover[25]; - -// Attractor, just like before! -Attractor a; - -void setup() { - size(800,200); - smooth(); - - box2d = new PBox2D(this); - box2d.createWorld(); - // No global gravity force - box2d.setGravity(0,0); - - for (int i = 0; i < movers.length; i++) { - movers[i] = new Mover(random(8,16),random(width),random(height)); - } - a = new Attractor(32,width/2,height/2); -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - a.display(); - - for (int i = 0; i < movers.length; i++) { - // Look, this is just like what we had before! - Vec2 force = a.attract(movers[i]); - movers[i].applyForce(force); - movers[i].display(); - } -} - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Mover.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Mover.pde deleted file mode 100644 index dcf446eeb..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_10_AttractionApplyForce/Mover.pde +++ /dev/null @@ -1,64 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Showing how to use applyForce() with box2d - -class Mover { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Mover(float r_, float x, float y) { - r = r_; - // Define a body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - - // Set its position - bd.position = box2d.coordPixelsToWorld(x,y); - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - - body.setLinearVelocity(new Vec2(random(-5,5),random(-5,-5))); - body.setAngularVelocity(random(-1,1)); - } - - void applyForce(Vec2 v) { - body.applyForce(v, body.getWorldCenter()); - } - - - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x,pos.y); - rotate(a); - fill(127); - stroke(0); - strokeWeight(2); - ellipse(0,0,r*2,r*2); - // Let's add a line so we can see the rotation - line(0,0,r,0); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Exercise_5_3_NoiseChain.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Exercise_5_3_NoiseChain.pde deleted file mode 100644 index 708e99688..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Exercise_5_3_NoiseChain.pde +++ /dev/null @@ -1,72 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// An uneven surface - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -// An object to store information about the uneven surface -Surface surface; - -void setup() { - size(383,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -20); - - // Create the empty list - particles = new ArrayList(); - // Create the surface - surface = new Surface(); -} - -void draw() { - // If the mouse is pressed, we make new particles - if (mousePressed) { - float sz = random(2,6); - particles.add(new Particle(mouseX,mouseY,sz)); - } - - // We must always step through time! - box2d.step(); - - background(255); - - // Draw the surface - surface.display(); - - // Draw all particles - for (Particle p: particles) { - p.display(); - } - - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - if (p.done()) { - particles.remove(i); - } - } -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Particle.pde deleted file mode 100644 index be9a07e7e..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Particle.pde +++ /dev/null @@ -1,90 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x,y,r); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x,pos.y); - rotate(-a); - fill(175); - stroke(0); - strokeWeight(1); - ellipse(0,0,r*2,r*2); - // Let's add a line so we can see the rotation - line(0,0,r,0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x,y); - bd.type = BodyType.DYNAMIC; - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - // Give it a random initial velocity (and angular velocity) - body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); - body.setAngularVelocity(random(-10,10)); - } - - - - - - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Surface.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Surface.pde deleted file mode 100644 index a8d72166a..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_NoiseChain/Surface.pde +++ /dev/null @@ -1,79 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// An uneven surface boundary - -class Surface { - // We'll keep track of all of the surface points - ArrayList surface; - - - Surface() { - surface = new ArrayList(); - - // This is what box2d uses to put the surface in its world - ChainShape chain = new ChainShape(); - - // Perlin noise argument - float xoff = 0.0; - - // This has to go backwards so that the objects bounce off the top of the surface - // This "edgechain" will only work in one direction! - for (float x = width+10; x > -10; x -= 5) { - - // Doing some stuff with perlin noise to calculate a surface that points down on one side - // and up on the other - float y; - if (x > width/2) { - y = 50 + (width - x)*1.1 + map(noise(xoff),0,1,-80,80); - } - else { - y = 50 + x*1.1 + map(noise(xoff),0,1,-40,40); - } - - // Store the vertex in screen coordinates - surface.add(new Vec2(x,y)); - - // Move through perlin noise - xoff += 0.1; - - } - - // Build an array of vertices in Box2D coordinates - // from the ArrayList we made - Vec2[] vertices = new Vec2[surface.size()]; - for (int i = 0; i < vertices.length; i++) { - Vec2 edge = box2d.coordPixelsToWorld(surface.get(i)); - vertices[i] = edge; - } - - // Create the chain! - chain.createChain(vertices,vertices.length); - - // The edge chain is now attached to a body via a fixture - BodyDef bd = new BodyDef(); - bd.position.set(0.0f,0.0f); - Body body = box2d.createBody(bd); - // Shortcut, we could define a fixture if we - // want to specify frictions, restitution, etc. - body.createFixture(chain,1); - - } - - // A simple function to just draw the edge chain as a series of vertex points - void display() { - strokeWeight(2); - stroke(0); - noFill(); - beginShape(); - for (Vec2 v: surface) { - vertex(v.x,v.y); - } - endShape(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Exercise_5_3_SineChain.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Exercise_5_3_SineChain.pde deleted file mode 100644 index f96ba5619..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Exercise_5_3_SineChain.pde +++ /dev/null @@ -1,72 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// An uneven surface - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -// An object to store information about the uneven surface -Surface surface; - -void setup() { - size(383,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -10); - - // Create the empty list - particles = new ArrayList(); - // Create the surface - surface = new Surface(); -} - -void draw() { - // If the mouse is pressed, we make new particles - if (random(1) < 0.5) { - float sz = random(2,6); - particles.add(new Particle(width/2,10,sz)); - } - - // We must always step through time! - box2d.step(); - - background(255); - - // Draw the surface - surface.display(); - - // Draw all particles - for (Particle p: particles) { - p.display(); - } - - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - if (p.done()) { - particles.remove(i); - } - } -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Particle.pde deleted file mode 100644 index 8ef8d65b2..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Particle.pde +++ /dev/null @@ -1,90 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x,y,r); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x,pos.y); - rotate(-a); - fill(175); - stroke(0); - strokeWeight(1); - ellipse(0,0,r*2,r*2); - // Let's add a line so we can see the rotation - line(0,0,r,0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x,y); - bd.type = BodyType.DYNAMIC; - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - // Give it a random initial velocity (and angular velocity) - body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); - body.setAngularVelocity(random(-10,10)); - } - - - - - - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Surface.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Surface.pde deleted file mode 100644 index ec00f3e54..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_3_SineChain/Surface.pde +++ /dev/null @@ -1,70 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// An uneven surface boundary - -class Surface { - // We'll keep track of all of the surface points - ArrayList surface; - - - Surface() { - surface = new ArrayList(); - - // This is what box2d uses to put the surface in its world - ChainShape chain = new ChainShape(); - - float theta = 0; - - // This has to go backwards so that the objects bounce off the top of the surface - // This "edgechain" will only work in one direction! - for (float x = width+10; x > -10; x -= 5) { - - // Doing some stuff with perlin noise to calculate a surface that points down on one side - // and up on the other - float y = map(cos(theta),-1,1,75,height-10); - theta += 0.15; - - // Store the vertex in screen coordinates - surface.add(new Vec2(x,y)); - - } - - // Build an array of vertices in Box2D coordinates - // from the ArrayList we made - Vec2[] vertices = new Vec2[surface.size()]; - for (int i = 0; i < vertices.length; i++) { - Vec2 edge = box2d.coordPixelsToWorld(surface.get(i)); - vertices[i] = edge; - } - - // Create the chain! - chain.createChain(vertices,vertices.length); - - // The edge chain is now attached to a body via a fixture - BodyDef bd = new BodyDef(); - bd.position.set(0.0f,0.0f); - Body body = box2d.createBody(bd); - // Shortcut, we could define a fixture if we - // want to specify frictions, restitution, etc. - body.createFixture(chain,1); - - } - - // A simple function to just draw the edge chain as a series of vertex points - void display() { - strokeWeight(2); - stroke(0); - noFill(); - beginShape(); - for (Vec2 v: surface) { - vertex(v.x,v.y); - } - endShape(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Box.pde deleted file mode 100644 index 19c610cb2..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Box.pde +++ /dev/null @@ -1,89 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A rectangular box -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x, float y) { - w = random(4, 16); - h = random(4, 16); - // Add the box to the box2d world - makeBody(new Vec2(x, y), w, h); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+w*h) { - killBody(); - return true; - } - return false; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - stroke(0); - fill(127); - strokeWeight(2); - rect(0, 0, w, h); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - // Define the body and make it from the shape - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - - body = box2d.createBody(bd); - body.createFixture(fd); - //body.setMassFromShapes(); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Bridge.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Bridge.pde deleted file mode 100644 index 4fcf14477..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Bridge.pde +++ /dev/null @@ -1,67 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Series of Particles connected with distance joints - -class Bridge { - - // Bridge properties - float totalLength; // How long - int numPoints; // How many points - - // Our chain is a list of particles - ArrayList particles; - - // Chain constructor - Bridge(float l, int n) { - - totalLength = l; - numPoints = n; - - particles = new ArrayList(); - - float len = totalLength / numPoints; - - // Here is the real work, go through and add particles to the chain itself - for(int i=0; i < numPoints+1; i++) { - // Make a new particle - Particle p = null; - - // First and last particles are made with density of zero - if (i == 0 || i == numPoints) p = new Particle(i*len,height/4,4,true); - else p = new Particle(i*len,height/4,4,false); - particles.add(p); - - // Connect the particles with a distance joint - if (i > 0) { - DistanceJointDef djd = new DistanceJointDef(); - Particle previous = particles.get(i-1); - // Connection between previous particle and this one - djd.bodyA = previous.body; - djd.bodyB = p.body; - // Equilibrium length - djd.length = box2d.scalarPixelsToWorld(len); - // These properties affect how springy the joint is - djd.frequencyHz = 0; - djd.dampingRatio = 0; - - // Make the joint. Note we aren't storing a reference to the joint ourselves anywhere! - // We might need to someday, but for now it's ok - DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd); - } - } - } - - // Draw the bridge - void display() { - for (Particle p: particles) { - p.display(); - } - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Exercise_5_6_Bridge.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Exercise_5_6_Bridge.pde deleted file mode 100644 index 4d341ed96..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Exercise_5_6_Bridge.pde +++ /dev/null @@ -1,85 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Example demonstrating distance joints -// A bridge is formed by connected a series of particles with joints - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// An object to describe a Bridget (a list of particles with joint connections) -Bridge bridge; - -// A list for all of our rectangles -ArrayList boxes; - -void setup() { - size(800, 200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - - // Make the bridge - bridge = new Bridge(width, width/10); - - // Create ArrayLists - boxes = new ArrayList(); -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - - // When the mouse is clicked, add a new Box object - if (mousePressed) { - Box p = new Box(mouseX, mouseY); - boxes.add(p); - } - - // Display all the boxes - for (Box b: boxes) { - b.display(); - } - - // Boxes that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = boxes.size()-1; i >= 0; i--) { - Box b = boxes.get(i); - if (b.done()) { - boxes.remove(i); - } - } - - // Draw the windmill - bridge.display(); - - - fill(0); - //text("Click mouse to add boxes.", 10, height-10); -} - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Particle.pde deleted file mode 100644 index e3c8b0097..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Exercise_5_6_Bridge/Particle.pde +++ /dev/null @@ -1,79 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Particle(float x, float y, float r_, boolean fixed) { - r = r_; - - // Define a body - BodyDef bd = new BodyDef(); - if (fixed) bd.type = BodyType.STATIC; - else bd.type = BodyType.DYNAMIC; - - // Set its position - bd.position = box2d.coordPixelsToWorld(x,y); - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x,pos.y); - rotate(a); - stroke(0); - fill(127); - strokeWeight(2); - ellipse(0,0,r*2,r*2); - // Let's add a line so we can see the rotation - line(0,0,r,0); - popMatrix(); - } - - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/Box.pde deleted file mode 100644 index 02d085bac..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/Box.pde +++ /dev/null @@ -1,103 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A rectangular box - -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - boolean dragged = false; - - // Constructor - Box(float x_, float y_) { - float x = x_; - float y = y_; - w = 24; - h = 24; - // Add the box to the box2d world - makeBody(new Vec2(x,y),w,h); - body.setUserData(this); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - boolean contains(float x, float y) { - Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); - Fixture f = body.getFixtureList(); - boolean inside = f.testPoint(worldPoint); - return inside; - } - - void setAngularVelocity(float a) { - body.setAngularVelocity(a); - } - void setVelocity(Vec2 v) { - body.setLinearVelocity(v); - } - - void setLocation(float x, float y) { - Vec2 pos = body.getWorldCenter(); - Vec2 target = box2d.coordPixelsToWorld(x,y); - Vec2 diff = new Vec2(target.x-pos.x,target.y-pos.y); - diff.mulLocal(50); - setVelocity(diff); - setAngularVelocity(0); - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(PConstants.CENTER); - pushMatrix(); - translate(pos.x,pos.y); - rotate(a); - fill(175); - stroke(0); - rect(0,0,w,h); - popMatrix(); - } - - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - // Define and create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.KINEMATIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - bd.fixedRotation = true; - body = box2d.createBody(bd); - - // Define a polygon (this is what we use for a rectangle) - PolygonShape ps = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - ps.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = ps; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/KinematicTest.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/KinematicTest.pde deleted file mode 100644 index 15f86aca3..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/KinematicTest.pde +++ /dev/null @@ -1,144 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of controlling an object with our own motion (by attaching a MouseJoint) -// Also demonstrates how to know which object was hit - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// Just a single box this time -Box box; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -// Perlin noise values -float xoff = 0; -float yoff = 1000; - - -void setup() { - size(640,360); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Turn on collision listening! - box2d.listenForCollisions(); - - // Make the box - box = new Box(width/2,height/2); - - // Create the empty list - particles = new ArrayList(); - - -} - -void draw() { - background(255); - - if (random(1) < 0.2) { - float sz = random(4,8); - particles.add(new Particle(width/2,-20,sz)); - } - - - // We must always step through time! - box2d.step(); - - // Make an x,y coordinate out of perlin noise - float x = noise(xoff)*width; - float y = noise(yoff)*height; - xoff += 0.01; - yoff += 0.01; - - // This is tempting but will not work! - // box.body.setXForm(box2d.screenToWorld(x,y),0); - - // Instead update the spring which pulls the mouse along - if (mousePressed) { - box.setLocation(mouseX,mouseY); - } else { - //box.setLocation(x,y); - } - - // Look at all particles - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - p.display(); - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - if (p.done()) { - particles.remove(i); - } - } - - // Draw the box - box.display(); - - // Draw the spring - // spring.display(); -} - - -// Collision event functions! -void beginContact(Contact cp) { - // Get both fixtures - Fixture f1 = cp.getFixtureA(); - Fixture f2 = cp.getFixtureB(); - // Get both bodies - Body b1 = f1.getBody(); - Body b2 = f2.getBody(); - // Get our objects that reference these bodies - Object o1 = b1.getUserData(); - Object o2 = b2.getUserData(); - - // If object 1 is a Box, then object 2 must be a particle - // Note we are ignoring particle on particle collisions - if (o1.getClass() == Box.class) { - Particle p = (Particle) o2; - p.change(); - } - // If object 2 is a Box, then object 1 must be a particle - else if (o2.getClass() == Box.class) { - Particle p = (Particle) o1; - p.change(); - } -} - - -// Objects stop touching each other -void endContact(Contact cp) { -} - - - - - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/Particle.pde deleted file mode 100644 index 12c50f772..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/KinematicTest/Particle.pde +++ /dev/null @@ -1,93 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - color col; - - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x, y, r); - body.setUserData(this); - col = color(175); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Change color when hit - void change() { - col = color(255, 0, 0); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - rotate(a); - fill(col); - stroke(0); - strokeWeight(1); - ellipse(0, 0, r*2, r*2); - // Let's add a line so we can see the rotation - line(0, 0, r, 0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x, y); - bd.type = BodyType.DYNAMIC; - - body = box2d.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - body.setAngularVelocity(random(-10, 10)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Boundary.pde deleted file mode 100644 index d3ff672da..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Boundary.pde +++ /dev/null @@ -1,62 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A fixed boundary class (now incorporates angle) - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_, float a) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.angle = a; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - noFill(); - stroke(0); - strokeWeight(1); - rectMode(CENTER); - - float a = b.getAngle(); - - pushMatrix(); - translate(x,y); - rotate(-a); - rect(0,0,w,h); - popMatrix(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Liquidy.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Liquidy.pde deleted file mode 100644 index 73e61f25c..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Liquidy.pde +++ /dev/null @@ -1,74 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Box2D particle system example - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - - - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; - -// A list for all particle systems -ArrayList systems; - -void setup() { - size(400,300); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // We are setting a custom gravity - box2d.setGravity(0, -20); - - // Create ArrayLists - systems = new ArrayList(); - boundaries = new ArrayList(); - - // Add a bunch of fixed boundaries - boundaries.add(new Boundary(50,100,300,5,-0.3)); - boundaries.add(new Boundary(250,175,300,5,0.5)); - -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // Run all the particle systems - for (ParticleSystem system: systems) { - system.run(); - - int n = (int) random(0,2); - system.addParticles(n); - } - - // Display all the boundaries - for (Boundary wall: boundaries) { - wall.display(); - } -} - - -void mousePressed() { - // Add a new Particle System whenever the mouse is clicked - systems.add(new ParticleSystem(0, new PVector(mouseX,mouseY))); -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Particle.pde deleted file mode 100644 index 145b22276..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/Particle.pde +++ /dev/null @@ -1,99 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A Particle - -class Particle { - - // We need to keep track of a Body - Body body; - - PVector[] trail; - - // Constructor - Particle(float x_, float y_) { - float x = x_; - float y = y_; - trail = new PVector[6]; - for (int i = 0; i < trail.length; i++) { - trail[i] = new PVector(x, y); - } - - // Add the box to the box2d world - // Here's a little trick, let's make a tiny tiny radius - // This way we have collisions, but they don't overwhelm the system - makeBody(new Vec2(x, y), 0.2f); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+20) { - killBody(); - return true; - } - return false; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - - // Keep track of a history of screen positions in an array - for (int i = 0; i < trail.length-1; i++) { - trail[i] = trail[i+1]; - } - trail[trail.length-1] = new PVector(pos.x, pos.y); - - // Draw particle as a trail - beginShape(); - noFill(); - strokeWeight(2); - stroke(0, 150); - for (int i = 0; i < trail.length; i++) { - vertex(trail[i].x, trail[i].y); - } - endShape(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float r) { - // Define and create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-1, 1), random(-1, 1))); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - - fd.density = 1; - fd.friction = 0; // Slippery when wet! - fd.restitution = 0.5; - - // We could use this if we want to turn collisions off - //cd.filter.groupIndex = -10; - - // Attach fixture to body - body.createFixture(fd); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/ParticleSystem.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/ParticleSystem.pde deleted file mode 100644 index 9c232f914..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/Liquidy/ParticleSystem.pde +++ /dev/null @@ -1,59 +0,0 @@ -// Box2D Particle System -// -// Spring 2010 - -// A class to describe a group 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 - - ParticleSystem(int num, PVector v) { - particles = new ArrayList(); // Initialize the ArrayList - origin = v.get(); // Store the origin point - - for (int i = 0; i < num; i++) { - particles.add(new Particle(origin.x,origin.y)); // Add "num" amount of particles to the ArrayList - } - } - - void run() { - // Display all the particles - for (Particle p: particles) { - p.display(); - } - - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - if (p.done()) { - particles.remove(i); - } - } - } - - void addParticles(int n) { - for (int i = 0; i < n; i++) { - particles.add(new Particle(origin.x,origin.y)); - } - } - - // A method to test if the particle system still has particles - boolean dead() { - if (particles.isEmpty()) { - return true; - } - else { - return false; - } - } - -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/Boundary.pde deleted file mode 100644 index d3ff672da..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/Boundary.pde +++ /dev/null @@ -1,62 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A fixed boundary class (now incorporates angle) - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_, float a) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.angle = a; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - noFill(); - stroke(0); - strokeWeight(1); - rectMode(CENTER); - - float a = b.getAngle(); - - pushMatrix(); - translate(x,y); - rotate(-a); - rect(0,0,w,h); - popMatrix(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/Box.pde deleted file mode 100644 index 27682e42b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/Box.pde +++ /dev/null @@ -1,106 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A rectangular box - -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - boolean dragged = false; - - // Constructor - Box(float x_, float y_) { - float x = x_; - float y = y_; - w = 24; - h = 24; - // Add the box to the box2d world - makeBody(new Vec2(x,y),w,h); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - boolean contains(float x, float y) { - Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); - Fixture f = body.getFixtureList(); - boolean inside = f.testPoint(worldPoint); - return inside; - } - - void setAngularVelocity(float a) { - body.setAngularVelocity(a); - } - void setVelocity(Vec2 v) { - body.setLinearVelocity(v); - } - - void setLocation(float x, float y) { - Vec2 pos = body.getWorldCenter(); - Vec2 target = box2d.coordPixelsToWorld(x,y); - Vec2 diff = target.sub(pos); - diff.mulLocal(50); - setVelocity(diff); - setAngularVelocity(0); - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(PConstants.CENTER); - pushMatrix(); - translate(pos.x,pos.y); - rotate(a); - fill(175); - stroke(0); - rect(0,0,w,h); - popMatrix(); - } - - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - // Define and create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.KINEMATIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - //body.setMassFromShapes(); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/MouseKinematic.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/MouseKinematic.pde deleted file mode 100644 index cad1269d7..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/MouseKinematic/MouseKinematic.pde +++ /dev/null @@ -1,78 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of controlling an object with the mouse (by attaching a spring) - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; - -// Just a single box this time -Box box; - -void setup() { - size(640,360); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Make the box - box = new Box(width/2,height/2); - - // Add a bunch of fixed boundaries - boundaries = new ArrayList(); - boundaries.add(new Boundary(width/2,height-5,width,10,0)); - boundaries.add(new Boundary(width/2,5,width,10,0)); - boundaries.add(new Boundary(width-5,height/2,10,height,0)); - boundaries.add(new Boundary(5,height/2,10,height,0)); -} - - -void draw() { - background(255); - - // We must always step through time! - - //if (box.dragged) { - box.setLocation(mouseX,mouseY); - //} - - box2d.step(); - - // Draw the boundaries - for (Boundary wall : boundaries) { - wall.display(); - } - - // Draw the box - box.display(); - - -} - -void mousePressed() { - if (box.contains(mouseX,mouseY)) { - box.dragged = true; - } -} - -void mouseReleased() { - box.dragged = false; -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise/Box.pde deleted file mode 100644 index 3ffd2de0f..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise/Box.pde +++ /dev/null @@ -1,23 +0,0 @@ -// A rectangular box -class Box { - - float x,y; - float w,h; - - // Constructor - Box(float x_, float y_) { - x = x_; - y = y_; - w = 16; - h = 16; - } - - // Drawing the box - void display() { - fill(127); - stroke(0); - strokeWeight(2); - rectMode(CENTER); - rect(x,y,w,h); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise/NOC_5_1_box2d_exercise.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise/NOC_5_1_box2d_exercise.pde deleted file mode 100644 index 2f5ddc73b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise/NOC_5_1_box2d_exercise.pde +++ /dev/null @@ -1,24 +0,0 @@ -// A list for all of our rectangles -ArrayList boxes; - -void setup() { - size(800,200); - smooth(); - // Create ArrayLists - boxes = new ArrayList(); -} - -void draw() { - background(255); - - // When the mouse is clicked, add a new Box object - if (mousePressed) { - Box p = new Box(mouseX,mouseY); - boxes.add(p); - } - - // Display all the boxes - for (Box b: boxes) { - b.display(); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise_solved/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise_solved/Box.pde deleted file mode 100644 index 8eff285ae..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise_solved/Box.pde +++ /dev/null @@ -1,55 +0,0 @@ -// A rectangular box -class Box { - // Instead of any of the usual variables, we will store a reference to a Box2D Body - Body body; - - float w,h; - - Box(float x, float y) { - w = 16; - h = 16; - - // Build Body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - body = box2d.createBody(bd); - - - // Define a polygon (this is what we use for a rectangle) - PolygonShape ps = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); // Box2D considers the width and height of a - ps.setAsBox(box2dW, box2dH); // rectangle to be the distance from the - // center to the edge (so half of what we - // normally think of as width or height.) - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = ps; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - // Attach Fixture to Body - body.createFixture(fd); - } - - void display() { - // We need the Body’s location and angle - Vec2 pos = box2d.getBodyPixelCoord(body); - float a = body.getAngle(); - - pushMatrix(); - translate(pos.x,pos.y); // Using the Vec2 position and float angle to - rotate(-a); // translate and rotate the rectangle - fill(127); - stroke(0); - strokeWeight(2); - rectMode(CENTER); - rect(0,0,w,h); - popMatrix(); - } - -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise_solved/NOC_5_1_box2d_exercise_solved.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise_solved/NOC_5_1_box2d_exercise_solved.pde deleted file mode 100644 index 34b4a8acd..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_1_box2d_exercise_solved/NOC_5_1_box2d_exercise_solved.pde +++ /dev/null @@ -1,37 +0,0 @@ -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A list for all of our rectangles -ArrayList boxes; - -PBox2D box2d; - -void setup() { - size(800, 200); - smooth(); - // Initialize and create the Box2D world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Create ArrayLists - boxes = new ArrayList(); -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // When the mouse is clicked, add a new Box object - Box p = new Box(mouseX, mouseY); - boxes.add(p); - - // Display all the boxes - for (Box b: boxes) { - b.display(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/Boundary.pde deleted file mode 100644 index d180aa86d..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/Boundary.pde +++ /dev/null @@ -1,54 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape ps = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - ps.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(ps,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/Box.pde deleted file mode 100644 index 5f76a87fa..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/Box.pde +++ /dev/null @@ -1,88 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A rectangular box -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x, float y) { - w = random(4, 16); - h = random(4, 16); - // Add the box to the box2d world - makeBody(new Vec2(x, y), w, h); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+w*h) { - killBody(); - return true; - } - return false; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - fill(127); - stroke(0); - strokeWeight(2); - rect(0, 0, w, h); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - // Define the body and make it from the shape - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - - body = box2d.createBody(bd); - body.createFixture(fd); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/NOC_5_2_Boxes.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/NOC_5_2_Boxes.pde deleted file mode 100644 index 7a80fac21..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_2_Boxes/NOC_5_2_Boxes.pde +++ /dev/null @@ -1,74 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of falling rectangles - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; -// A list for all of our rectangles -ArrayList boxes; - -void setup() { - size(800,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -10); - - // Create ArrayLists - boxes = new ArrayList(); - boundaries = new ArrayList(); - - // Add a bunch of fixed boundaries - boundaries.add(new Boundary(width/4,height-5,width/2-50,10)); - boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10)); -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // Boxes fall from the top every so often - if (random(1) < 0.2) { - Box p = new Box(width/2,30); - boxes.add(p); - } - - // Display all the boundaries - for (Boundary wall: boundaries) { - wall.display(); - } - - // Display all the boxes - for (Box b: boxes) { - b.display(); - } - - // Boxes that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = boxes.size()-1; i >= 0; i--) { - Box b = boxes.get(i); - if (b.done()) { - boxes.remove(i); - } - } -} - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/NOC_5_3_ChainShape_Simple.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/NOC_5_3_ChainShape_Simple.pde deleted file mode 100644 index 899555fab..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/NOC_5_3_ChainShape_Simple.pde +++ /dev/null @@ -1,72 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// An uneven surface - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -// An object to store information about the uneven surface -Surface surface; - -void setup() { - size(800,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -10); - - // Create the empty list - particles = new ArrayList(); - // Create the surface - surface = new Surface(); -} - -void draw() { - // If the mouse is pressed, we make new particles - if (random(1) < 0.5) { - float sz = random(4,8); - particles.add(new Particle(width/2,10,sz)); - } - - // We must always step through time! - box2d.step(); - - background(255); - - // Draw the surface - surface.display(); - - // Draw all particles - for (Particle p: particles) { - p.display(); - } - - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - if (p.done()) { - particles.remove(i); - } - } -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/Particle.pde deleted file mode 100644 index bfd3acdf5..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/Particle.pde +++ /dev/null @@ -1,90 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x,y,r); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x,pos.y); - rotate(-a); - fill(127); - stroke(0); - strokeWeight(2); - ellipse(0,0,r*2,r*2); - // Let's add a line so we can see the rotation - line(0,0,r,0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x,y); - bd.type = BodyType.DYNAMIC; - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - // Give it a random initial velocity (and angular velocity) - body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); - body.setAngularVelocity(random(-10,10)); - } - - - - - - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/Surface.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/Surface.pde deleted file mode 100644 index 13e19576b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_3_ChainShape_Simple/Surface.pde +++ /dev/null @@ -1,53 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// An uneven surface boundary - -class Surface { - // We'll keep track of all of the surface points - ArrayList surface; - - - Surface() { - surface = new ArrayList(); - // Here we keep track of the screen coordinates of the chain - surface.add(new Vec2(0, height/2)); - //surface.add(new Vec2(width/2, height/2+50)); - surface.add(new Vec2(width, height/2)); - - // This is what box2d uses to put the surface in its world - ChainShape chain = new ChainShape(); - - // We can add 3 vertices by making an array of 3 Vec2 objects - Vec2[] vertices = new Vec2[surface.size()]; - for (int i = 0; i < vertices.length; i++) { - vertices[i] = box2d.coordPixelsToWorld(surface.get(i)); - } - - chain.createChain(vertices, vertices.length); - - // The edge chain is now a body! - BodyDef bd = new BodyDef(); - Body body = box2d.world.createBody(bd); - // Shortcut, we could define a fixture if we - // want to specify frictions, restitution, etc. - body.createFixture(chain, 1); - } - - // A simple function to just draw the edge chain as a series of vertex points - void display() { - strokeWeight(1); - stroke(0); - fill(0); - beginShape(); - for (Vec2 v: surface) { - vertex(v.x, v.y); - } - vertex(width, height); - vertex(0, height); - endShape(CLOSE); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/Boundary.pde deleted file mode 100644 index 1a35aca9c..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/Boundary.pde +++ /dev/null @@ -1,60 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class (now incorporates angle) - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_, float a) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.angle = a; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, it doesn't move so we don't have to ask the Body for location - void display() { - fill(0); - stroke(0); - strokeWeight(1); - rectMode(CENTER); - float a = b.getAngle(); - pushMatrix(); - translate(x,y); - rotate(-a); - rect(0,0,w,h); - popMatrix(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/CustomShape.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/CustomShape.pde deleted file mode 100644 index fcd1cb5b0..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/CustomShape.pde +++ /dev/null @@ -1,91 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// A rectangular box -class CustomShape { - - // We need to keep track of a Body and a width and height - Body body; - - // Constructor - CustomShape(float x, float y) { - // Add the box to the box2d world - makeBody(new Vec2(x, y)); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height) { - killBody(); - return true; - } - return false; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - Fixture f = body.getFixtureList(); - PolygonShape ps = (PolygonShape) f.getShape(); - - - rectMode(CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - fill(127); - stroke(0); - strokeWeight(2); - beginShape(); - //println(vertices.length); - // For every vertex, convert to pixel vector - for (int i = 0; i < ps.getVertexCount(); i++) { - Vec2 v = box2d.vectorWorldToPixels(ps.getVertex(i)); - vertex(v.x, v.y); - } - endShape(CLOSE); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center) { - - Vec2[] vertices = new Vec2[4]; - vertices[0] = box2d.vectorPixelsToWorld(new Vec2(-15, 25)); - vertices[1] = box2d.vectorPixelsToWorld(new Vec2(15, 0)); - vertices[2] = box2d.vectorPixelsToWorld(new Vec2(20, -15)); - vertices[3] = box2d.vectorPixelsToWorld(new Vec2(-10, -10)); - - // Define a polygon (this is what we use for a rectangle) - PolygonShape ps = new PolygonShape(); - ps.set(vertices, vertices.length); - - // Define the body and make it from the shape - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - body.createFixture(ps, 1.0); - - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/NOC_5_4_Polygons.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/NOC_5_4_Polygons.pde deleted file mode 100644 index c3dc6d811..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_4_Polygons/NOC_5_4_Polygons.pde +++ /dev/null @@ -1,74 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of falling rectangles - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; -// A list for all of our rectangles -ArrayList polygons; - -void setup() { - size(800,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -20); - - // Create ArrayLists - polygons = new ArrayList(); - boundaries = new ArrayList(); - - // Add a bunch of fixed boundaries - boundaries.add(new Boundary(width/4,height-5,width/2-50,10,0)); - boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10,0)); - boundaries.add(new Boundary(width-5,height/2,10,height,0)); - boundaries.add(new Boundary(5,height/2,10,height,0)); -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // Display all the boundaries - for (Boundary wall: boundaries) { - wall.display(); - } - - // Display all the people - for (CustomShape cs: polygons) { - cs.display(); - } - - // people that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = polygons.size()-1; i >= 0; i--) { - CustomShape cs = polygons.get(i); - if (cs.done()) { - polygons.remove(i); - } - } -} - -void mousePressed() { - CustomShape cs = new CustomShape(mouseX,mouseY); - polygons.add(cs); -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/Boundary.pde deleted file mode 100644 index ea4eebb75..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/Boundary.pde +++ /dev/null @@ -1,62 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class (now incorporates angle) - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_, float a) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.angle = a; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - strokeWeight(1); - rectMode(CENTER); - - float a = b.getAngle(); - - pushMatrix(); - translate(x,y); - rotate(-a); - rect(0,0,w,h); - popMatrix(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/Lollipop.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/Lollipop.pde deleted file mode 100644 index 7d07373b5..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/Lollipop.pde +++ /dev/null @@ -1,89 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// A rectangular box -class Lollipop { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - float r; - - // Constructor - Lollipop(float x, float y) { - w = 8; - h = 24; - r = 8; - // Add the box to the box2d world - makeBody(new Vec2(x, y)); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+w*h) { - killBody(); - return true; - } - return false; - } - - // Drawing the lollipop - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(CENTER); - pushMatrix(); - translate(pos.x, pos.y); - rotate(-a); - fill(127); - stroke(0); - strokeWeight(2); - - rect(0,0,w,h); - ellipse(0, -h/2, r*2, r*2); - popMatrix(); - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center) { - - // Define the body and make it from the shape - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - CircleShape circle = new CircleShape(); - circle.m_radius = box2d.scalarPixelsToWorld(r); - Vec2 offset = new Vec2(0,-h/2); - offset = box2d.vectorPixelsToWorld(offset); - circle.m_p.set(offset.x,offset.y); - - PolygonShape ps = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - ps.setAsBox(box2dW, box2dH); - - body.createFixture(ps,1.0); - body.createFixture(circle, 1.0); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/NOC_5_5_MultiShapes.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/NOC_5_5_MultiShapes.pde deleted file mode 100644 index 4ebfedfd7..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_5_MultiShapes/NOC_5_5_MultiShapes.pde +++ /dev/null @@ -1,74 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of falling rectangles - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; -// A list for all of our rectangles -ArrayList pops; - -void setup() { - size(800,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this,20); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0, -20); - - // Create ArrayLists - pops = new ArrayList(); - boundaries = new ArrayList(); - - // Add a bunch of fixed boundaries - boundaries.add(new Boundary(width/4,height-5,width/2-50,10,0)); - boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10,0)); - boundaries.add(new Boundary(width-5,height/2,10,height,0)); - boundaries.add(new Boundary(5,height/2,10,height,0)); -} - -void draw() { - background(255); - - // We must always step through time! - if (mousePressed) box2d.step(); - - // Display all the boundaries - for (Boundary wall: boundaries) { - wall.display(); - } - - // Display all the people - for (Lollipop p: pops) { - p.display(); - } - - // people that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - for (int i = pops.size()-1; i >= 0; i--) { - Lollipop p = pops.get(i); - if (p.done()) { - pops.remove(i); - } - } -} - -void mousePressed() { - Lollipop p = new Lollipop(mouseX,mouseY); - pops.add(p); -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Boundary.pde deleted file mode 100644 index 7b5012517..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Boundary.pde +++ /dev/null @@ -1,54 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/NOC_5_6_DistanceJoint.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/NOC_5_6_DistanceJoint.pde deleted file mode 100644 index c68edd589..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/NOC_5_6_DistanceJoint.pde +++ /dev/null @@ -1,81 +0,0 @@ -// The Nature of Code -// -// Spring 201 -// PBox2D example - -// Example demonstrating distance joints -// A bridge is formed by connected a series of particles with joints - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; - - -// A list for all of our rectangles -ArrayList pairs; - -void setup() { - size(800,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Create ArrayLists - pairs = new ArrayList(); - - boundaries = new ArrayList(); - - // Add a bunch of fixed boundaries - boundaries.add(new Boundary(width/4,height-5,width/2-50,10)); - boundaries.add(new Boundary(3*width/4,height-50,width/2-50,10)); - -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // When the mouse is clicked, add a new Box object - - // Display all the boxes - for (Pair p: pairs) { - p.display(); - } - - // Display all the boundaries - for (Boundary wall: boundaries) { - wall.display(); - } - - fill(0); - text("Click mouse to add connected particles.",10,20); -} - -void mousePressed() { - Pair p = new Pair(mouseX,mouseY); - pairs.add(p); -} - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Pair.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Pair.pde deleted file mode 100644 index d250ad1ed..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Pair.pde +++ /dev/null @@ -1,48 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Series of Particles connected with distance joints - -class Pair { - - Particle p1; - Particle p2; - - float len; - // Chain constructor - Pair(float x, float y) { - len = 32; - - p1 = new Particle(x,y); - p2 = new Particle(x+random(-1,1),y+random(-1,1)); - - DistanceJointDef djd = new DistanceJointDef(); - // Connection between previous particle and this one - djd.bodyA = p1.body; - djd.bodyB = p2.body; - // Equilibrium length - djd.length = box2d.scalarPixelsToWorld(len); - - // These properties affect how springy the joint is - djd.frequencyHz = 3; // Try a value less than 5 (0 for no elasticity) - djd.dampingRatio = 0.1; // Ranges between 0 and 1 (1 for no springiness) - - // Make the joint. Note we aren't storing a reference to the joint ourselves anywhere! - // We might need to someday, but for now it's ok - DistanceJoint dj = (DistanceJoint) box2d.world.createJoint(djd); - } - - void display() { - Vec2 pos1 = box2d.getBodyPixelCoord(p1.body); - Vec2 pos2 = box2d.getBodyPixelCoord(p2.body); - stroke(0); - strokeWeight(2); - line(pos1.x,pos1.y,pos2.x,pos2.y); - - p1.display(); - p2.display(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Particle.pde deleted file mode 100644 index c66da98cd..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_6_DistanceJoint/Particle.pde +++ /dev/null @@ -1,77 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Particle(float x, float y) { - r = 8; - - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x,y); - bd.type = BodyType.DYNAMIC; - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x,pos.y); - rotate(a); - fill(127); - stroke(0); - strokeWeight(2); - ellipse(0,0,r*2,r*2); - // Let's add a line so we can see the rotation - line(0,0,r,0); - popMatrix(); - } - - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Box.pde deleted file mode 100644 index 06410e87a..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Box.pde +++ /dev/null @@ -1,73 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// A rectangular box - -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x, float y, float w_, float h_, boolean lock) { - w = w_; - h = h_; - - // Define and create the body - BodyDef bd = new BodyDef(); - bd.position.set(box2d.coordPixelsToWorld(new Vec2(x,y))); - if (lock) bd.type = BodyType.STATIC; - else bd.type = BodyType.DYNAMIC; - - body = box2d.createBody(bd); - - // Define the shape -- a (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5,5),random(2,5))); - body.setAngularVelocity(random(-5,5)); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(PConstants.CENTER); - pushMatrix(); - translate(pos.x,pos.y); - rotate(-a); - fill(127); - stroke(0); - strokeWeight(2); - rect(0,0,w,h); - popMatrix(); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/NOC_5_7_RevoluteJoint.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/NOC_5_7_RevoluteJoint.pde deleted file mode 100644 index be3681432..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/NOC_5_7_RevoluteJoint.pde +++ /dev/null @@ -1,90 +0,0 @@ -// The Nature of Code -// -// Spring 201 -// PBox2D example - -// Example demonstrating revolute joint - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// An object to describe a Windmill (two bodies and one joint) -Windmill windmill; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -void setup() { - size(800,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Make the windmill at an x,y location - windmill = new Windmill(width/2,175); - - // Create the empty list - particles = new ArrayList(); - -} - -// Click the mouse to turn on or off the motor -void mousePressed() { - windmill.toggleMotor(); -} - -void draw() { - background(255); - - if (random(1) < 0.1) { - float sz = random(4,8); - particles.add(new Particle(random(width/2-100,width/2+100),-20,sz)); - } - - - // We must always step through time! - box2d.step(); - - // Look at all particles - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - p.display(); - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - if (p.done()) { - particles.remove(i); - } - } - - // Draw the windmill - windmill.display(); - - String status = "OFF"; - if (windmill.motorOn()) status = "ON"; - - fill(0); - text("Click mouse to toggle motor.\nMotor: " + status,10,height-30); - - -} - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Particle.pde deleted file mode 100644 index 0f64a5204..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Particle.pde +++ /dev/null @@ -1,85 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x, y, r); - body.setUserData(this); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - - rotate(-a); - fill(127); - stroke(0); - strokeWeight(2); - ellipse(0, 0, r*2, r*2); - // Let's add a line so we can see the rotation - line(0, 0, r, 0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x, y); - bd.type = BodyType.DYNAMIC; - - body = box2d.world.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - - fd.density = 2.0; - fd.friction = 0.01; - fd.restitution = 0.3; // Restitution is bounciness - - body.createFixture(fd); - - // Give it a random initial velocity (and angular velocity) - //body.setLinearVelocity(new Vec2(random(-10f,10f),random(5f,10f))); - body.setAngularVelocity(random(-10, 10)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Windmill.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Windmill.pde deleted file mode 100644 index a25423e9b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_7_RevoluteJoint/Windmill.pde +++ /dev/null @@ -1,62 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// Class to describe a fixed spinning object - -class Windmill { - - // Our object is two boxes and one joint - // Consider making the fixed box much smaller and not drawing it - RevoluteJoint joint; - Box box1; - Box box2; - - Windmill(float x, float y) { - - // Initialize locations of two boxes - box1 = new Box(x, y-20, 120, 10, false); - box2 = new Box(x, y, 10, 40, true); - - // Define joint as between two bodies - RevoluteJointDef rjd = new RevoluteJointDef(); - - rjd.initialize(box1.body, box2.body, box1.body.getWorldCenter()); - - // Turning on a motor (optional) - rjd.motorSpeed = PI*2; // how fast? - rjd.maxMotorTorque = 1000.0; // how powerful? - rjd.enableMotor = false; // is it on? - - // There are many other properties you can set for a Revolute joint - // For example, you can limit its angle between a minimum and a maximum - // See box2d manual for more - - - // Create the joint - joint = (RevoluteJoint) box2d.world.createJoint(rjd); - } - - // Turn the motor on or off - void toggleMotor() { - joint.enableMotor(!joint.isMotorEnabled()); - } - - boolean motorOn() { - return joint.isMotorEnabled(); - } - - - void display() { - box2.display(); - box1.display(); - - // Draw anchor just for debug - Vec2 anchor = box2d.coordWorldToPixels(box1.body.getWorldCenter()); - fill(0); - noStroke(); - ellipse(anchor.x, anchor.y, 8, 8); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Boundary.pde deleted file mode 100644 index a05edbf8b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Boundary.pde +++ /dev/null @@ -1,63 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A fixed boundary class (now incorporates angle) - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_, float a) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.angle = a; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - noFill(); - stroke(127); - fill(127); - strokeWeight(1); - rectMode(CENTER); - - float a = b.getAngle(); - - pushMatrix(); - translate(x,y); - rotate(-a); - rect(0,0,w,h); - popMatrix(); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Box.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Box.pde deleted file mode 100644 index f3a872879..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Box.pde +++ /dev/null @@ -1,89 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A rectangular box - -class Box { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - // Constructor - Box(float x_, float y_) { - float x = x_; - float y = y_; - w = 24; - h = 24; - // Add the box to the box2d world - makeBody(new Vec2(x,y),w,h); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - boolean contains(float x, float y) { - Vec2 worldPoint = box2d.coordPixelsToWorld(x, y); - Fixture f = body.getFixtureList(); - boolean inside = f.testPoint(worldPoint); - return inside; - } - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(PConstants.CENTER); - pushMatrix(); - translate(pos.x,pos.y); - rotate(a); - fill(127); - stroke(0); - strokeWeight(2); - rect(0,0,w,h); - popMatrix(); - } - - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_) { - // Define and create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - body = box2d.createBody(bd); - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - body.createFixture(fd); - //body.setMassFromShapes(); - - // Give it some initial random velocity - body.setLinearVelocity(new Vec2(random(-5, 5), random(2, 5))); - body.setAngularVelocity(random(-5, 5)); - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/NOC_5_8_MouseJoint.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/NOC_5_8_MouseJoint.pde deleted file mode 100644 index 564f716cd..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/NOC_5_8_MouseJoint.pde +++ /dev/null @@ -1,86 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of controlling an object with the mouse (by attaching a spring) - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -// A list we'll use to track fixed objects -ArrayList boundaries; - -// Just a single box this time -Box box; - -// The Spring that will attach to the box from the mouse -Spring spring; - -void setup() { - size(800,200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Make the box - box = new Box(width/2,height/2); - - // Make the spring (it doesn't really get initialized until the mouse is clicked) - spring = new Spring(); - - // Add a bunch of fixed boundaries - boundaries = new ArrayList(); - boundaries.add(new Boundary(width/2,height-5,width,10,0)); - boundaries.add(new Boundary(width/2,5,width,10,0)); - boundaries.add(new Boundary(width-5,height/2,10,height,0)); - boundaries.add(new Boundary(5,height/2,10,height,0)); -} - -// When the mouse is released we're done with the spring -void mouseReleased() { - spring.destroy(); -} - -// When the mouse is pressed we. . . -void mousePressed() { - // Check to see if the mouse was clicked on the box - if (box.contains(mouseX, mouseY)) { - // And if so, bind the mouse location to the box with a spring - spring.bind(mouseX,mouseY,box); - } -} - -void draw() { - background(255); - - // We must always step through time! - box2d.step(); - - // Always alert the spring to the new mouse location - spring.update(mouseX,mouseY); - - // Draw the boundaries - for (int i = 0; i < boundaries.size(); i++) { - Boundary wall = (Boundary) boundaries.get(i); - wall.display(); - } - - // Draw the box - box.display(); - // Draw the spring (it only appears when active) - spring.display(); -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Spring.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Spring.pde deleted file mode 100644 index 76c445d50..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_8_MouseJoint/Spring.pde +++ /dev/null @@ -1,78 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// Class to describe the spring joint (displayed as a line) - -class Spring { - - // This is the box2d object we need to create - MouseJoint mouseJoint; - - Spring() { - // At first it doesn't exist - mouseJoint = null; - } - - // If it exists we set its target to the mouse location - void update(float x, float y) { - if (mouseJoint != null) { - // Always convert to world coordinates! - Vec2 mouseWorld = box2d.coordPixelsToWorld(x,y); - mouseJoint.setTarget(mouseWorld); - } - } - - void display() { - if (mouseJoint != null) { - // We can get the two anchor points - Vec2 v1 = new Vec2(0,0); - mouseJoint.getAnchorA(v1); - Vec2 v2 = new Vec2(0,0); - mouseJoint.getAnchorB(v2); - // Convert them to screen coordinates - v1 = box2d.coordWorldToPixels(v1); - v2 = box2d.coordWorldToPixels(v2); - // And just draw a line - stroke(0); - strokeWeight(1); - line(v1.x,v1.y,v2.x,v2.y); - } - } - - - // This is the key function where - // we attach the spring to an x,y location - // and the Box object's location - void bind(float x, float y, Box box) { - // Define the joint - MouseJointDef md = new MouseJointDef(); - // Body A is just a fake ground body for simplicity (there isn't anything at the mouse) - md.bodyA = box2d.getGroundBody(); - // Body 2 is the box's boxy - md.bodyB = box.body; - // Get the mouse location in world coordinates - Vec2 mp = box2d.coordPixelsToWorld(x,y); - // And that's the target - md.target.set(mp); - // Some stuff about how strong and bouncy the spring should be - md.maxForce = 1000.0 * box.body.m_mass; - md.frequencyHz = 5.0; - md.dampingRatio = 0.9; - - // Make the joint! - mouseJoint = (MouseJoint) box2d.world.createJoint(md); - } - - void destroy() { - // We can get rid of the joint when the mouse is released - if (mouseJoint != null) { - box2d.world.destroyJoint(mouseJoint); - mouseJoint = null; - } - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/Boundary.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/Boundary.pde deleted file mode 100644 index 06c5bf80d..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/Boundary.pde +++ /dev/null @@ -1,56 +0,0 @@ -// The Nature of Code -// -// Spring 2012 -// PBox2D example - -// A fixed boundary class - -class Boundary { - - // A boundary is a simple rectangle with x,y,width,and height - float x; - float y; - float w; - float h; - - // But we also have to make a body for box2d to know about it - Body b; - - Boundary(float x_,float y_, float w_, float h_) { - x = x_; - y = y_; - w = w_; - h = h_; - - // Define the polygon - PolygonShape sd = new PolygonShape(); - // Figure out the box2d coordinates - float box2dW = box2d.scalarPixelsToWorld(w/2); - float box2dH = box2d.scalarPixelsToWorld(h/2); - // We're just a box - sd.setAsBox(box2dW, box2dH); - - - // Create the body - BodyDef bd = new BodyDef(); - bd.type = BodyType.STATIC; - bd.position.set(box2d.coordPixelsToWorld(x,y)); - b = box2d.createBody(bd); - - // Attached the shape to the body using a Fixture - b.createFixture(sd,1); - - b.setUserData(this); - } - - // Draw the boundary, if it were at an angle we'd have to do something fancier - void display() { - fill(0); - stroke(0); - rectMode(CENTER); - rect(x,y,w,h); - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/NOC_5_9_CollisionListening.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/NOC_5_9_CollisionListening.pde deleted file mode 100644 index 2d5f843e3..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/NOC_5_9_CollisionListening.pde +++ /dev/null @@ -1,111 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Basic example of controlling an object with our own motion (by attaching a MouseJoint) -// Also demonstrates how to know which object was hit - -import pbox2d.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.joints.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.collision.shapes.Shape; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; -import org.jbox2d.dynamics.contacts.*; - -// A reference to our box2d world -PBox2D box2d; - -// An ArrayList of particles that will fall on the surface -ArrayList particles; - -Boundary wall; - -void setup() { - size(800, 200); - smooth(); - - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - - // Turn on collision listening! - box2d.listenForCollisions(); - - // Create the empty list - particles = new ArrayList(); - - wall = new Boundary(width/2, height-5, width, 10); -} - -void draw() { - background(255); - - if (random(1) < 0.1) { - float sz = random(4, 8); - particles.add(new Particle(random(width), 20, sz)); - } - - - // We must always step through time! - box2d.step(); - - // Look at all particles - for (int i = particles.size()-1; i >= 0; i--) { - Particle p = particles.get(i); - p.display(); - // Particles that leave the screen, we delete them - // (note they have to be deleted from both the box2d world and our list - if (p.done()) { - particles.remove(i); - } - } - - wall.display(); -} - - -// Collision event functions! -void beginContact(Contact cp) { - // Get both fixtures - Fixture f1 = cp.getFixtureA(); - Fixture f2 = cp.getFixtureB(); - // Get both bodies - Body b1 = f1.getBody(); - Body b2 = f2.getBody(); - - // Get our objects that reference these bodies - Object o1 = b1.getUserData(); - Object o2 = b2.getUserData(); - - if (o1.getClass() == Particle.class && o2.getClass() == Particle.class) { - Particle p1 = (Particle) o1; - p1.change(); - Particle p2 = (Particle) o2; - p2.change(); - } - -} - -// Objects stop touching each other -void endContact(Contact cp) { -} - - - - - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/Particle.pde deleted file mode 100644 index 7d92ef9f6..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/NOC_5_9_CollisionListening/Particle.pde +++ /dev/null @@ -1,91 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// PBox2D example - -// A circular particle - -class Particle { - - // We need to keep track of a Body and a radius - Body body; - float r; - - color col; - - Particle(float x, float y, float r_) { - r = r_; - // This function puts the particle in the Box2d world - makeBody(x, y, r); - body.setUserData(this); - col = color(127); - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - // Change color when hit - void change() { - col = color(255, 0, 0); - } - - // Is the particle ready for deletion? - boolean done() { - // Let's find the screen position of the particle - Vec2 pos = box2d.getBodyPixelCoord(body); - // Is it off the bottom of the screen? - if (pos.y > height+r*2) { - killBody(); - return true; - } - return false; - } - - - // - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - // Get its angle of rotation - float a = body.getAngle(); - pushMatrix(); - translate(pos.x, pos.y); - rotate(a); - fill(col); - stroke(0); - strokeWeight(2); - ellipse(0, 0, r*2, r*2); - // Let's add a line so we can see the rotation - line(0, 0, r, 0); - popMatrix(); - } - - // Here's our function that adds the particle to the Box2D world - void makeBody(float x, float y, float r) { - // Define a body - BodyDef bd = new BodyDef(); - // Set its position - bd.position = box2d.coordPixelsToWorld(x, y); - bd.type = BodyType.DYNAMIC; - body = box2d.createBody(bd); - - // Make the body's shape a circle - CircleShape cs = new CircleShape(); - cs.m_radius = box2d.scalarPixelsToWorld(r); - - FixtureDef fd = new FixtureDef(); - fd.shape = cs; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.01; - fd.restitution = 0.3; - - // Attach fixture to body - body.createFixture(fd); - - body.setAngularVelocity(random(-10, 10)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/VectorStuff/VectorStuff.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/VectorStuff/VectorStuff.pde deleted file mode 100644 index a89fd6eb5..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/box2d/VectorStuff/VectorStuff.pde +++ /dev/null @@ -1,57 +0,0 @@ -// The Nature of Code -// -// Spring 2011 -// PBox2D example - -// Just demo-ing the basics of Vec2 vs. PVector - -import org.jbox2d.common.*; - -void setup() { - size(400,300); -// PVector a = new PVector(1,-1); -// PVector b = new PVector(3,4); -// a.add(b); -// -// PVector a = new PVector(1,-1); -// PVector b = new PVector(3,4); -// PVector c = PVector.add(a,b); -// -// Vec2 a = new Vec2(1,-1); -// Vec2 b = new Vec2(3,4); -// a.addLocal(b); -// -// Vec2 a = new Vec2(1,-1); -// Vec2 b = new Vec2(3,4); -// Vec2 c = a.add(b); -// -// PVector a = new PVector(1,-1); -// float n = 5; -// a.mult(n); -// -// PVector a = new PVector(1,-1); -// float n = 5; -// PVector c = PVector.mult(a,n); -// -// Vec2 a = new Vec2(1,-1); -// float n = 5; -// a.mulLocal(n); -// -// Vec2 a = new Vec2(1,-1); -// float n = 5; -// Vec2 c = a.mul(n); -// -// PVector a = new PVector(1,-1); -// float m = a.mag(); -// a.normalize(); - - Vec2 a = new Vec2(1,-1); - float m = a.length(); - a.normalize(); - println(a.x + "," + a.y); -} - -void draw() { - noLoop(); -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/readme.txt b/java/examples/Books/Nature of Code/chp5_physicslibraries/readme.txt deleted file mode 100644 index 99d3a27ad..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/readme.txt +++ /dev/null @@ -1,3 +0,0 @@ -For the box2d examples you will need PBox2D! - -https://github.com/shiffman/PBox2D \ No newline at end of file diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Blanket.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Blanket.pde deleted file mode 100644 index 70a45340b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Blanket.pde +++ /dev/null @@ -1,51 +0,0 @@ -class Blanket { - ArrayList particles; - ArrayList springs; - - Blanket() { - particles = new ArrayList(); - springs = new ArrayList(); - - int w = 20; - int h = 20; - - float len = 10; - float strength = 0.125; - - for(int y=0; y< h; y++) { - for(int x=0; x < w; x++) { - - Particle p = new Particle(new Vec2D(width/2+x*len-w*len/2,y*len)); - physics.addParticle(p); - particles.add(p); - - if (x > 0) { - Particle previous = particles.get(particles.size()-2); - Connection c = new Connection(p,previous,len,strength); - physics.addSpring(c); - springs.add(c); - } - - if (y > 0) { - Particle above = particles.get(particles.size()-w-1); - Connection c=new Connection(p,above,len,strength); - physics.addSpring(c); - springs.add(c); - } - } - } - - Particle topleft= particles.get(0); - topleft.lock(); - - Particle topright = particles.get(w-1); - topright.lock(); - } - - void display() { - for (Connection c : springs) { - c.display(); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Connection.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Connection.pde deleted file mode 100644 index 692d3f9ab..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Connection.pde +++ /dev/null @@ -1,11 +0,0 @@ -class Connection extends VerletSpring2D { - Connection(Particle p1, Particle p2, float len, float strength) { - super(p1,p2,len,strength); - } - - void display() { - stroke(0); - line(a.x,a.y,b.x,b.y); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Exercise_5_13_SoftBodySquareAdapted.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Exercise_5_13_SoftBodySquareAdapted.pde deleted file mode 100644 index 78aa8c09b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Exercise_5_13_SoftBodySquareAdapted.pde +++ /dev/null @@ -1,63 +0,0 @@ -/** - * This example is adapted from Karsten Schmidt's SoftBodySquare example - * Daniel Shiffman, 2011 - * The Nature of Code book - */ - -/*

Softbody square demo is showing how to create a 2D square mesh out of - * verlet particles and make it stable enough to avoid total structural - * deformation by including an inner skeleton.

- * - *

Usage: move mouse to drag/deform the square

- */ - -/* - * Copyright (c) 2008-2009 Karsten Schmidt - * - * This demo & library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * http://creativecommons.org/licenses/LGPL/2.1/ - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - - -import toxi.physics2d.behaviors.*; -import toxi.physics2d.*; - -import toxi.geom.*; -import toxi.math.*; - -VerletPhysics2D physics; - -Blanket b; - - -void setup() { - size(800,240); - smooth(); - physics=new VerletPhysics2D(); - physics.addBehavior(new GravityBehavior(new Vec2D(0,0.1))); - - b = new Blanket(); -} - -void draw() { - - background(255); - - physics.update(); - - b.display(); -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Particle.pde deleted file mode 100644 index 41a73d4c2..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_13_SoftBodySquareAdapted/Particle.pde +++ /dev/null @@ -1,17 +0,0 @@ -// Notice how we are using inheritance here! -// We could have just stored a reference to a VerletParticle object -// inside the Particle class, but inheritance is a nice alternative - -class Particle extends VerletParticle2D { - - Particle(Vec2D loc) { - super(loc); - } - - // All we're doing really is adding a display() function to a VerletParticle - void display() { - fill(175); - stroke(0); - ellipse(x,y,16,16); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Cluster.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Cluster.pde deleted file mode 100644 index e2629412e..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Cluster.pde +++ /dev/null @@ -1,99 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// Toxiclibs example: http://toxiclibs.org/ - -// Force directed graph -// Heavily based on: http://code.google.com/p/fidgen/ - -class Cluster { - - // A cluster is a grouping of nodes - ArrayList nodes; - - float diameter; - - // We initialize a Cluster with a number of nodes, a diameter, and centerpoint - Cluster(int n, float d, Vec2D center) { - - // Initialize the ArrayList - nodes = new ArrayList(); - - // Set the diameter - diameter = d; - - // Create the nodes - for (int i = 0; i < n; i++) { - // We can't put them right on top of each other - nodes.add(new Node(center.add(Vec2D.randomVector()))); - } - - // Connect all the nodes with a Spring - for (int i = 1; i < nodes.size(); i++) { - VerletParticle2D pi = (VerletParticle2D) nodes.get(i); - for (int j = 0; j < i; j++) { - VerletParticle2D pj = (VerletParticle2D) nodes.get(j); - // A Spring needs two particles, a resting length, and a strength - physics.addSpring(new VerletSpring2D(pi,pj,diameter,0.01)); - } - } - } - - void display() { - // Show all the nodes - for (int i = 0; i < nodes.size(); i++) { - Node n = (Node) nodes.get(i); - n.display(); - } - } - - // This functons connects one cluster to another - // Each point of one cluster connects to each point of the other cluster - // The connection is a "VerletMinDistanceSpring" - // A VerletMinDistanceSpring is a string which only enforces its rest length if the - // current distance is less than its rest length. This is handy if you just want to - // ensure objects are at least a certain distance from each other, but don't - // care if it's bigger than the enforced minimum. - void connect(Cluster other) { - ArrayList otherNodes = other.getNodes(); - for (int i = 0; i < nodes.size(); i++) { - VerletParticle2D pi = (VerletParticle2D) nodes.get(i); - for (int j = 0; j < otherNodes.size(); j++) { - VerletParticle2D pj = (VerletParticle2D) otherNodes.get(j); - // Create the spring - physics.addSpring(new VerletMinDistanceSpring2D(pi,pj,(diameter+other.diameter)*0.5,0.05)); - } - } - } - - - // Draw all the internal connections - void showConnections() { - stroke(0,150); - for (int i = 0; i < nodes.size(); i++) { - VerletParticle2D pi = (VerletParticle2D) nodes.get(i); - for (int j = i+1; j < nodes.size(); j++) { - VerletParticle2D pj = (VerletParticle2D) nodes.get(j); - line(pi.x,pi.y,pj.x,pj.y); - } - } - } - - // Draw all the connections between this Cluster and another Cluster - void showConnections(Cluster other) { - stroke(0,50); - strokeWeight(2); - ArrayList otherNodes = other.getNodes(); - for (int i = 0; i < nodes.size(); i++) { - VerletParticle2D pi = (VerletParticle2D) nodes.get(i); - for (int j = 0; j < otherNodes.size(); j++) { - VerletParticle2D pj = (VerletParticle2D) otherNodes.get(j); - line(pi.x,pi.y,pj.x,pj.y); - } - } - } - - ArrayList getNodes() { - return nodes; - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Exercise_5_15_ForceDirectedGraph.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Exercise_5_15_ForceDirectedGraph.pde deleted file mode 100644 index b6e4982d1..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Exercise_5_15_ForceDirectedGraph.pde +++ /dev/null @@ -1,134 +0,0 @@ -/** - *

Force directed graph, - * heavily based on: fid.gen
- * The Nature of Code
- * Spring 2010

- */ - -/* - * Copyright (c) 2010 Daniel Schiffmann - * - * This demo & library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * http://creativecommons.org/licenses/LGPL/2.1/ - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -import toxi.geom.*; -import toxi.physics2d.*; -import toxi.physics2d.behaviors.*; - -// Reference to physics world -VerletPhysics2D physics; - -// A list of cluster objects -ArrayList clusters; - -// Boolean that indicates whether we draw connections or not -boolean showPhysics = true; -boolean showParticles = true; - -// Font -PFont f; - -void setup() { - size(800,300); - smooth(); - f = createFont("Georgia",12,true); - - // Initialize the physics - physics=new VerletPhysics2D(); - physics.setWorldBounds(new Rect(10,10,width-20,height-20)); - - // Spawn a new random graph - newGraph(); - -} - -// Spawn a new random graph -void newGraph() { - - // Clear physics - physics.clear(); - - // Create new ArrayList (clears old one) - clusters = new ArrayList(); - - // Create 8 random clusters - for (int i = 0; i < 8; i++) { - Vec2D center = new Vec2D(width/2,height/2); - clusters.add(new Cluster((int) random(3,8),random(20,100),center)); - } - - // All clusters connect to all clusters - for (int i = 0; i < clusters.size(); i++) { - for (int j = i+1; j < clusters.size(); j++) { - Cluster ci = (Cluster) clusters.get(i); - Cluster cj = (Cluster) clusters.get(j); - ci.connect(cj); - } - } - -} - -void draw() { - - // Update the physics world - physics.update(); - - background(255); - - // Display all points - if (showParticles) { - for (int i = 0; i < clusters.size(); i++) { - Cluster c = (Cluster) clusters.get(i); - c.display(); - } - } - - // If we want to see the physics - if (showPhysics) { - for (int i = 0; i < clusters.size(); i++) { - // Cluster internal connections - Cluster ci = (Cluster) clusters.get(i); - ci.showConnections(); - - // Cluster connections to other clusters - for (int j = i+1; j < clusters.size(); j++) { - Cluster cj = (Cluster) clusters.get(j); - ci.showConnections(cj); - } - } - } - - // Instructions - fill(0); - textFont(f); - text("'p' to display or hide particles\n'c' to display or hide connections\n'n' for new graph",10,20); -} - -// Key press commands -void keyPressed() { - if (key == 'c') { - showPhysics = !showPhysics; - if (!showPhysics) showParticles = true; - } - else if (key == 'p') { - showParticles = !showParticles; - if (!showParticles) showPhysics = true; - } - else if (key == 'n') { - newGraph(); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Node.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Node.pde deleted file mode 100644 index 6fe3b5144..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/Exercise_5_15_ForceDirectedGraph/Node.pde +++ /dev/null @@ -1,27 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// Toxiclibs example: http://toxiclibs.org/ - -// Force directed graph -// Heavily based on: http://code.google.com/p/fidgen/ - -// Notice how we are using inheritance here! -// We could have just stored a reference to a VerletParticle object -// inside the Node class, but inheritance is a nice alternative - -class Node extends VerletParticle2D { - - Node(Vec2D pos) { - super(pos); - } - - // All we're doing really is adding a display() function to a VerletParticle - void display() { - fill(0,150); - stroke(0); - strokeWeight(2); - ellipse(x,y,16,16); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_10_SimpleSpring/NOC_5_10_SimpleSpring.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_10_SimpleSpring/NOC_5_10_SimpleSpring.pde deleted file mode 100644 index 95b877cf7..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_10_SimpleSpring/NOC_5_10_SimpleSpring.pde +++ /dev/null @@ -1,70 +0,0 @@ -// The Nature of Code -// -// PBox2D example - -// Simple Toxiclibs Spring - -import toxi.physics2d.*; -import toxi.physics2d.behaviors.*; -import toxi.geom.*; - -// Reference to physics world -VerletPhysics2D physics; - -Particle p1; -Particle p2; - -void setup() { - size(800,200); - smooth(); - frameRate(30); - - // Initialize the physics - physics=new VerletPhysics2D(); - physics.addBehavior(new GravityBehavior(new Vec2D(0,0.5))); - - // Set the world's bounding box - physics.setWorldBounds(new Rect(0,0,width,height)); - - // Make two particles - p1 = new Particle(new Vec2D(width/2,20)); - p2 = new Particle(new Vec2D(width,180)); - // Lock one in place - p1.lock(); - - // Make a spring connecting both Particles - VerletSpring2D spring=new VerletSpring2D(p1,p2,80,0.01); - - // Anything we make, we have to add into the physics world - physics.addParticle(p1); - physics.addParticle(p2); - physics.addSpring(spring); -} - -void draw() { - - // Update the physics world - physics.update(); - - background(255); - - // Draw a line between the particles - stroke(0); - strokeWeight(2); - line(p1.x,p1.y,p2.x,p2.y); - - // Display the particles - p1.display(); - p2.display(); - - // Move the second one according to the mouse - if (mousePressed) { - p2.lock(); - p2.x = mouseX; - p2.y = mouseY; - p2.unlock(); - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_10_SimpleSpring/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_10_SimpleSpring/Particle.pde deleted file mode 100644 index 5d15d4d44..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_10_SimpleSpring/Particle.pde +++ /dev/null @@ -1,18 +0,0 @@ -// Notice how we are using inheritance here! -// We could have just stored a reference to a VerletParticle object -// inside the Particle class, but inheritance is a nice alternative - -class Particle extends VerletParticle2D { - - Particle(Vec2D loc) { - super(loc); - } - - // All we're doing really is adding a display() function to a VerletParticle - void display() { - fill(127); - stroke(0); - strokeWeight(2); - ellipse(x,y,32,32); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/Chain.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/Chain.pde deleted file mode 100644 index e9703ed21..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/Chain.pde +++ /dev/null @@ -1,104 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// Toxiclibs example - -// A soft pendulum (series of connected springs) - -class Chain { - - // Chain properties - float totalLength; // How long - int numPoints; // How many points - float strength; // Strength of springs - float radius; // Radius of ball at tail - - // This list is redundant since we can ask for physics.particles, but in case we have many of these - // it's a convenient to keep track of our own list - ArrayList particles; - - // Let's keep an extra reference to the tail particle - // This is just the last particle in the ArrayList - Particle tail; - - // Some variables for mouse dragging - PVector offset = new PVector(); - boolean dragged = false; - - // Chain constructor - Chain(float l, int n, float r, float s) { - particles = new ArrayList(); - - totalLength = l; - numPoints = n; - radius = r; - strength = s; - - float len = totalLength / numPoints; - - // Here is the real work, go through and add particles to the chain itself - for(int i=0; i < numPoints; i++) { - // Make a new particle with an initial starting location - Particle particle=new Particle(width/2,i*len); - - // Redundancy, we put the particles both in physics and in our own ArrayList - physics.addParticle(particle); - particles.add(particle); - - // Connect the particles with a Spring (except for the head) - if (i != 0) { - Particle previous = particles.get(i-1); - VerletSpring2D spring = new VerletSpring2D(particle,previous,len,strength); - // Add the spring to the physics world - physics.addSpring(spring); - } - } - - // Keep the top fixed - Particle head=particles.get(0); - head.lock(); - - // Store reference to the tail - tail = particles.get(numPoints-1); - tail.radius = radius; - } - - // Check if a point is within the ball at the end of the chain - // If so, set dragged = true; - void contains(int x, int y) { - float d = dist(x,y,tail.x,tail.y); - if (d < radius) { - offset.x = tail.x - x; - offset.y = tail.y - y; - tail.lock(); - dragged = true; - } - } - - // Release the ball - void release() { - tail.unlock(); - dragged = false; - } - - // Update tail location if being dragged - void updateTail(int x, int y) { - if (dragged) { - tail.set(x+offset.x,y+offset.y); - } - } - - // Draw the chain - void display() { - // Draw line connecting all points - beginShape(); - stroke(0); - strokeWeight(2); - noFill(); - for (Particle p : particles) { - vertex(p.x,p.y); - } - endShape(); - tail.display(); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/NOC_5_11_SoftStringPendulum.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/NOC_5_11_SoftStringPendulum.pde deleted file mode 100644 index c79572ca2..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/NOC_5_11_SoftStringPendulum.pde +++ /dev/null @@ -1,70 +0,0 @@ -/** - *

A soft pendulum (series of connected springs)
- * The Nature of Code
- * Spring 2010

- */ - -/* - * Copyright (c) 2010 Daniel Shiffmann - * - * This demo & library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * http://creativecommons.org/licenses/LGPL/2.1/ - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -import toxi.physics2d.*; -import toxi.physics2d.behaviors.*; -import toxi.geom.*; - -// Reference to physics "world" (2D) -VerletPhysics2D physics; - -// Our "Chain" object -Chain chain; - -void setup() { - size(800, 200); - smooth(); - - // Initialize the physics world - physics=new VerletPhysics2D(); - physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.1))); - physics.setWorldBounds(new Rect(0, 0, width, height)); - - // Initialize the chain - chain = new Chain(180, 20, 16, 0.2); -} - -void draw() { - background(255); - - // Update physics - physics.update(); - // Update chain's tail according to mouse location - chain.updateTail(mouseX, mouseY); - // Display chain - chain.display(); -} - -void mousePressed() { - // Check to see if we're grabbing the chain - chain.contains(mouseX, mouseY); -} - -void mouseReleased() { - // Release the chain - chain.release(); -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/Particle.pde deleted file mode 100644 index da2de8b4f..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_11_SoftStringPendulum/Particle.pde +++ /dev/null @@ -1,20 +0,0 @@ -// Notice how we are using inheritance here! -// We could have just stored a reference to a VerletParticle object -// inside the Particle class, but inheritance is a nice alternative - -class Particle extends VerletParticle2D { - - float radius = 4; // Adding a radius for each particle - - Particle(float x, float y) { - super(x,y); - } - - // All we're doing really is adding a display() function to a VerletParticle - void display() { - fill(127); - stroke(0); - strokeWeight(2); - ellipse(x,y,radius*2,radius*2); - } -} diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/Cluster.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/Cluster.pde deleted file mode 100644 index 241520f2b..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/Cluster.pde +++ /dev/null @@ -1,64 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// Toxiclibs example: http://toxiclibs.org/ - -// Force directed graph -// Heavily based on: http://code.google.com/p/fidgen/ - -class Cluster { - - // A cluster is a grouping of nodes - ArrayList nodes; - - float diameter; - - // We initialize a Cluster with a number of nodes, a diameter, and centerpoint - Cluster(int n, float d, Vec2D center) { - - // Initialize the ArrayList - nodes = new ArrayList(); - - // Set the diameter - diameter = d; - - // Create the nodes - for (int i = 0; i < n; i++) { - // We can't put them right on top of each other - nodes.add(new Node(center.add(Vec2D.randomVector()))); - } - - // Connect all the nodes with a Spring - for (int i = 0; i < nodes.size()-1; i++) { - VerletParticle2D ni = nodes.get(i); - for (int j = i+1; j < nodes.size(); j++) { - VerletParticle2D nj = nodes.get(j); - // A Spring needs two particles, a resting length, and a strength - physics.addSpring(new VerletSpring2D(ni, nj, diameter, 0.01)); - } - } - } - - void display() { - // Show all the nodes - for (Node n : nodes) { - n.display(); - } - } - - - // Draw all the internal connections - void showConnections() { - stroke(0, 150); - strokeWeight(2); - for (int i = 0; i < nodes.size()-1; i++) { - VerletParticle2D pi = (VerletParticle2D) nodes.get(i); - for (int j = i+1; j < nodes.size(); j++) { - VerletParticle2D pj = (VerletParticle2D) nodes.get(j); - - line(pi.x, pi.y, pj.x, pj.y); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/NOC_5_12_SimpleCluster.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/NOC_5_12_SimpleCluster.pde deleted file mode 100644 index 0191d5201..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/NOC_5_12_SimpleCluster.pde +++ /dev/null @@ -1,76 +0,0 @@ -/** - *

Force directed graph, - * heavily based on: fid.gen
- * The Nature of Code
- * Spring 2010

- */ - -import toxi.geom.*; -import toxi.physics2d.*; - -// Reference to physics world -VerletPhysics2D physics; - -// A list of cluster objects -Cluster cluster; - -// Boolean that indicates whether we draw connections or not -boolean showPhysics = true; -boolean showParticles = true; - -// Font -PFont f; - -void setup() { - size(800, 200); - smooth(); - frameRate(30); - f = createFont("Georgia", 12, true); - - // Initialize the physics - physics=new VerletPhysics2D(); - physics.setWorldBounds(new Rect(10, 10, width-20, height-20)); - - // Spawn a new random graph - cluster = new Cluster(8, 100, new Vec2D(width/2, height/2)); -} - -void draw() { - - // Update the physics world - physics.update(); - - background(255); - - // Display all points - if (showParticles) { - cluster.display(); - } - - // If we want to see the physics - if (showPhysics) { - cluster.showConnections(); - } - - // Instructions - fill(0); - textFont(f); - text("'p' to display or hide particles\n'c' to display or hide connections\n'n' for new graph",10,20); -} - -// Key press commands -void keyPressed() { - if (key == 'c') { - showPhysics = !showPhysics; - if (!showPhysics) showParticles = true; - } - else if (key == 'p') { - showParticles = !showParticles; - if (!showParticles) showPhysics = true; - } - else if (key == 'n') { - physics.clear(); - cluster = new Cluster(int(random(2, 20)), random(10, width/2), new Vec2D(width/2, height/2)); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/Node.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/Node.pde deleted file mode 100644 index 6fe3b5144..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_12_SimpleCluster/Node.pde +++ /dev/null @@ -1,27 +0,0 @@ -// The Nature of Code -// -// Spring 2010 -// Toxiclibs example: http://toxiclibs.org/ - -// Force directed graph -// Heavily based on: http://code.google.com/p/fidgen/ - -// Notice how we are using inheritance here! -// We could have just stored a reference to a VerletParticle object -// inside the Node class, but inheritance is a nice alternative - -class Node extends VerletParticle2D { - - Node(Vec2D pos) { - super(pos); - } - - // All we're doing really is adding a display() function to a VerletParticle - void display() { - fill(0,150); - stroke(0); - strokeWeight(2); - ellipse(x,y,16,16); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/Attractor.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/Attractor.pde deleted file mode 100644 index e82810ee1..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/Attractor.pde +++ /dev/null @@ -1,17 +0,0 @@ -class Attractor extends VerletParticle2D { - - float r; - - Attractor (Vec2D loc) { - super (loc); - r = 24; - physics.addParticle(this); - physics.addBehavior(new AttractionBehavior(this, width, 0.1)); - } - - void display () { - fill(0); - ellipse (x, y, r*2, r*2); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/NOC_5_13_AttractRepel.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/NOC_5_13_AttractRepel.pde deleted file mode 100644 index 7d4c3362f..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/NOC_5_13_AttractRepel.pde +++ /dev/null @@ -1,41 +0,0 @@ -import toxi.geom.*; -import toxi.physics2d.*; -import toxi.physics2d.behaviors.*; - -ArrayList particles; -Attractor attractor; - -VerletPhysics2D physics; - -void setup () { - size (800, 200); - smooth(); - physics = new VerletPhysics2D (); - physics.setDrag (0.01); - - particles = new ArrayList(); - for (int i = 0; i < 50; i++) { - particles.add(new Particle(new Vec2D(random(width),random(height)))); - } - - attractor = new Attractor(new Vec2D(width/2,height/2)); -} - - -void draw () { - background (255); - physics.update (); - - attractor.display(); - for (Particle p: particles) { - p.display(); - } - - if (mousePressed) { - attractor.lock(); - attractor.set(mouseX,mouseY); - } else { - attractor.unlock(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/Particle.pde b/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/Particle.pde deleted file mode 100644 index a33163d71..000000000 --- a/java/examples/Books/Nature of Code/chp5_physicslibraries/toxiclibs/NOC_5_13_AttractRepel/Particle.pde +++ /dev/null @@ -1,20 +0,0 @@ -// class Spore extends the class "VerletParticle2D" -class Particle extends VerletParticle2D { - - float r; - - Particle (Vec2D loc) { - super(loc); - r = 8; - physics.addParticle(this); - physics.addBehavior(new AttractionBehavior(this, r*4, -1)); - } - - void display () { - fill (127); - stroke (0); - strokeWeight(2); - ellipse (x, y, r*2, r*2); - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/Cohesion/Cohesion.pde b/java/examples/Books/Nature of Code/chp6_agents/Cohesion/Cohesion.pde deleted file mode 100644 index 976fb2308..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Cohesion/Cohesion.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Separation -// Daniel Shiffman -// The Nature of Code, 2011 - -// Via Reynolds: http://www.red3d.com/cwr/steer/ - -// A list of vehicles -ArrayList vehicles; - -void setup() { - size(640,360); - smooth(); - - // We are now making random vehicles and storing them in an ArrayList - vehicles = new ArrayList(); - for (int i = 0; i < 100; i++) { - vehicles.add(new Vehicle(random(width),random(height))); - } -} - -void draw() { - background(255); - - for (Vehicle v : vehicles) { - // Path following and separation are worked on in this function - v.cohesion(vehicles); - // Call the generic run method (update, borders, display, etc.) - v.update(); - v.borders(); - v.display(); - } - - // Instructions - fill(0); - text("Drag the mouse to generate new vehicles.",10,height-16); -} - - -void mouseDragged() { - vehicles.add(new Vehicle(mouseX,mouseY)); - - if (vehicles.size() > 200) { - vehicles.remove(0); - } - -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/Cohesion/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/Cohesion/Vehicle.pde deleted file mode 100644 index bbd8719a9..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Cohesion/Vehicle.pde +++ /dev/null @@ -1,99 +0,0 @@ -// Separation -// Daniel Shiffman -// The Nature of Code, 2011 - -// Vehicle class - -class Vehicle { - - // All the usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - // Constructor initialize all values - Vehicle(float x, float y) { - location = new PVector(x, y); - r = 12; - maxspeed = 3; - maxforce = 0.2; - acceleration = new PVector(0, 0); - velocity = new PVector(0, 0); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // Separation - // Method checks for nearby vehicles and steers away - void cohesion (ArrayList vehicles) { - float desiredseparation = r*2; - PVector sum = new PVector(); - int count = 0; - // For every boid in the system, check if it's too close - for (Vehicle other : vehicles) { - float d = PVector.dist(location, other.location); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if (d > desiredseparation) { - // Calculate vector pointing away from neighbor - PVector diff = PVector.sub(location, other.location); - diff.normalize(); - diff.mult(-d); // Weight by distance - sum.add(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - sum.div(count); - // Our desired vector is the average scaled to maximum speed - sum.normalize(); - sum.mult(maxspeed); - // Implement Reynolds: Steering = Desired - Velocity - PVector steer = PVector.sub(sum, velocity); - steer.limit(maxforce); - applyForce(steer); - } - } - - - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void display() { - fill(175); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - ellipse(0, 0, r, r); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - if (location.y > height+r) location.y = -r; - } -} - - - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/CrowdPathFollowing.pde b/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/CrowdPathFollowing.pde deleted file mode 100644 index d8281dd00..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/CrowdPathFollowing.pde +++ /dev/null @@ -1,76 +0,0 @@ -// Crowd Path Following -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Via Reynolds: http://www.red3d.com/cwr/steer/CrowdPath.html - -// Using this variable to decide whether to draw all the stuff -boolean debug = false; - - -// A path object (series of connected points) -Path path; - -// Two vehicles -ArrayList vehicles; - -void setup() { - size(640,360); - smooth(); - // Call a function to generate new Path object - newPath(); - - // We are now making random vehicles and storing them in an ArrayList - vehicles = new ArrayList(); - for (int i = 0; i < 120; i++) { - newVehicle(random(width),random(height)); - } -} - -void draw() { - background(255); - // Display the path - path.display(); - - for (Vehicle v : vehicles) { - // Path following and separation are worked on in this function - v.applyBehaviors(vehicles,path); - // Call the generic run method (update, borders, display, etc.) - v.run(); - } - - // Instructions - fill(0); - text("Hit 'd' to toggle debugging lines. Click the mouse to generate new vehicles.",10,height-16); -} - -void newPath() { - // A path is a series of connected points - // A more sophisticated path might be a curve - path = new Path(); - float offset = 60; - path.addPoint(offset,offset); - path.addPoint(width-offset,offset); - path.addPoint(width-offset,height-offset); - path.addPoint(width/2,height-offset*3); - path.addPoint(offset,height-offset); -} - -void newVehicle(float x, float y) { - float maxspeed = random(2,4); - float maxforce = 0.3; - vehicles.add(new Vehicle(new PVector(x,y),maxspeed,maxforce)); -} - -void keyPressed() { - if (key == 'd') { - debug = !debug; - } -} - -void mousePressed() { - newVehicle(mouseX,mouseY); -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/Path.pde b/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/Path.pde deleted file mode 100644 index d6ea21a57..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/Path.pde +++ /dev/null @@ -1,50 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code - -class Path { - - // A Path is an arraylist of points (PVector objects) - ArrayList points; - // A path has a radius, i.e how far is it ok for the boid to wander off - float radius; - - Path() { - // Arbitrary radius of 20 - radius = 20; - points = new ArrayList(); - } - - // Add a point to the path - void addPoint(float x, float y) { - PVector point = new PVector(x, y); - points.add(point); - } - - // Draw the path - void display() { - strokeJoin(ROUND); - - // Draw thick line for radius - stroke(175); - strokeWeight(radius*2); - noFill(); - beginShape(); - for (PVector v : points) { - vertex(v.x, v.y); - } - endShape(CLOSE); - // Draw thin line for center of path - stroke(0); - strokeWeight(1); - noFill(); - beginShape(); - for (PVector v : points) { - vertex(v.x, v.y); - } - endShape(CLOSE); - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/Vehicle.pde deleted file mode 100644 index be7811b49..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/Vehicle.pde +++ /dev/null @@ -1,239 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Vehicle class - -class Vehicle { - - // All the usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - // Constructor initialize all values - Vehicle( PVector l, float ms, float mf) { - location = l.get(); - r = 12; - maxspeed = ms; - maxforce = mf; - acceleration = new PVector(0, 0); - velocity = new PVector(maxspeed, 0); - } - - // A function to deal with path following and separation - void applyBehaviors(ArrayList vehicles, Path path) { - // Follow path force - PVector f = follow(path); - // Separate from other boids force - PVector s = separate(vehicles); - // Arbitrary weighting - f.mult(3); - s.mult(1); - // Accumulate in acceleration - applyForce(f); - applyForce(s); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - - // Main "run" function - public void run() { - update(); - borders(); - render(); - } - - - // This function implements Craig Reynolds' path following algorithm - // http://www.red3d.com/cwr/steer/PathFollow.html - PVector follow(Path p) { - - // Predict location 25 (arbitrary choice) frames ahead - PVector predict = velocity.get(); - predict.normalize(); - predict.mult(25); - PVector predictLoc = PVector.add(location, predict); - - // Now we must find the normal to the path from the predicted location - // We look at the normal for each line segment and pick out the closest one - PVector normal = null; - PVector target = null; - float worldRecord = 1000000; // Start with a very high worldRecord distance that can easily be beaten - - // Loop through all points of the path - for (int i = 0; i < p.points.size(); i++) { - - // Look at a line segment - PVector a = p.points.get(i); - PVector b = p.points.get((i+1)%p.points.size()); // Note Path has to wraparound - - // Get the normal point to that line - PVector normalPoint = getNormalPoint(predictLoc, a, b); - - // Check if normal is on line segment - PVector dir = PVector.sub(b, a); - // If it's not within the line segment, consider the normal to just be the end of the line segment (point b) - //if (da + db > line.mag()+1) { - if (normalPoint.x < min(a.x,b.x) || normalPoint.x > max(a.x,b.x) || normalPoint.y < min(a.y,b.y) || normalPoint.y > max(a.y,b.y)) { - normalPoint = b.get(); - // If we're at the end we really want the next line segment for looking ahead - a = p.points.get((i+1)%p.points.size()); - b = p.points.get((i+2)%p.points.size()); // Path wraps around - dir = PVector.sub(b, a); - } - - // How far away are we from the path? - float d = PVector.dist(predictLoc, normalPoint); - // Did we beat the worldRecord and find the closest line segment? - if (d < worldRecord) { - worldRecord = d; - normal = normalPoint; - - // Look at the direction of the line segment so we can seek a little bit ahead of the normal - dir.normalize(); - // This is an oversimplification - // Should be based on distance to path & velocity - dir.mult(25); - target = normal.get(); - target.add(dir); - - } - } - - // Draw the debugging stuff - if (debug) { - // Draw predicted future location - stroke(0); - fill(0); - line(location.x, location.y, predictLoc.x, predictLoc.y); - ellipse(predictLoc.x, predictLoc.y, 4, 4); - - // Draw normal location - stroke(0); - fill(0); - ellipse(normal.x, normal.y, 4, 4); - // Draw actual target (red if steering towards it) - line(predictLoc.x, predictLoc.y, target.x, target.y); - if (worldRecord > p.radius) fill(255, 0, 0); - noStroke(); - ellipse(target.x, target.y, 8, 8); - } - - // Only if the distance is greater than the path's radius do we bother to steer - if (worldRecord > p.radius) { - return seek(target); - } - else { - return new PVector(0, 0); - } - } - - - // A function to get the normal point from a point (p) to a line segment (a-b) - // This function could be optimized to make fewer new Vector objects - PVector getNormalPoint(PVector p, PVector a, PVector b) { - // Vector from a to p - PVector ap = PVector.sub(p, a); - // Vector from a to b - PVector ab = PVector.sub(b, a); - ab.normalize(); // Normalize the line - // Project vector "diff" onto line by using the dot product - ab.mult(ap.dot(ab)); - PVector normalPoint = PVector.add(a, ab); - return normalPoint; - } - - // Separation - // Method checks for nearby boids and steers away - PVector separate (ArrayList boids) { - float desiredseparation = r*2; - PVector steer = new PVector(0, 0, 0); - int count = 0; - // For every boid in the system, check if it's too close - for (int i = 0 ; i < boids.size(); i++) { - Vehicle other = (Vehicle) boids.get(i); - float d = PVector.dist(location, other.location); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if ((d > 0) && (d < desiredseparation)) { - // Calculate vector pointing away from neighbor - PVector diff = PVector.sub(location, other.location); - diff.normalize(); - diff.div(d); // Weight by distance - steer.add(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - steer.div((float)count); - } - - // As long as the vector is greater than 0 - if (steer.mag() > 0) { - // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mult(maxspeed); - steer.sub(velocity); - steer.limit(maxforce); - } - return steer; - } - - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - PVector seek(PVector target) { - PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus Velocationity - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); // Limit to maximum steering force - - return steer; - } - - - void render() { - // Simpler boid is just a circle - fill(75); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - ellipse(0, 0, r, r); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - //if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - //if (location.y > height+r) location.y = -r; - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/sketch.properties b/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/CrowdPathFollowing/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_04_Wander/Exercise_6_04_Wander.pde b/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_04_Wander/Exercise_6_04_Wander.pde deleted file mode 100644 index 734b1db00..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_04_Wander/Exercise_6_04_Wander.pde +++ /dev/null @@ -1,30 +0,0 @@ -// Wander -// Daniel Shiffman -// The Nature of Code - -// Demonstration of Craig Reynolds' "Wandering" behavior -// See: http://www.red3d.com/cwr/ - -// Click mouse to turn on and off rendering of the wander circle - - -Vehicle wanderer; -boolean debug = true; - -void setup() { - size(740,200); - wanderer = new Vehicle(width/2,height/2); - smooth(); -} - -void draw() { - background(255); - wanderer.wander(); - wanderer.run(); -} - -void mousePressed() { - debug = !debug; -} - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_04_Wander/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_04_Wander/Vehicle.pde deleted file mode 100644 index 87a836746..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_04_Wander/Vehicle.pde +++ /dev/null @@ -1,123 +0,0 @@ -// Wander -// Daniel Shiffman -// The Nature of Code - -// The "Vehicle" class (for wandering) - -class Vehicle { - - PVector location; - PVector velocity; - PVector acceleration; - float r; - float wandertheta; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(float x, float y) { - acceleration = new PVector(0,0); - velocity = new PVector(0,0); - location = new PVector(x,y); - r = 6; - wandertheta = 0; - maxspeed = 2; - maxforce = 0.05; - } - - void run() { - update(); - borders(); - display(); - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void wander() { - float wanderR = 25; // Radius for our "wander circle" - float wanderD = 80; // Distance for our "wander circle" - float change = 0.3; - wandertheta += random(-change,change); // Randomly change wander theta - - // Now we have to calculate the new location to steer towards on the wander circle - PVector circleloc = velocity.get(); // Start with velocity - circleloc.normalize(); // Normalize to get heading - circleloc.mult(wanderD); // Multiply by distance - circleloc.add(location); // Make it relative to boid's location - - float h = velocity.heading2D(); // We need to know the heading to offset wandertheta - - PVector circleOffSet = new PVector(wanderR*cos(wandertheta+h),wanderR*sin(wandertheta+h)); - PVector target = PVector.add(circleloc,circleOffSet); - seek(target); - - // Render wandering circle, etc. - if (debug) drawWanderStuff(location,circleloc,target,wanderR); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - void seek(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus Velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - - applyForce(steer); - } - - void display() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(127); - stroke(0); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - if (location.y > height+r) location.y = -r; - } -} - - -// A method just to draw the circle associated with wandering -void drawWanderStuff(PVector location, PVector circle, PVector target, float rad) { - stroke(0); - noFill(); - ellipseMode(CENTER); - ellipse(circle.x,circle.y,rad*2,rad*2); - ellipse(target.x,target.y,4,4); - line(location.x,location.y,circle.x,circle.y); - line(circle.x,circle.y,target.x,target.y); -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_09_AngleBetween/Exercise_6_09_AngleBetween.pde b/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_09_AngleBetween/Exercise_6_09_AngleBetween.pde deleted file mode 100644 index fb9e3b9ce..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_09_AngleBetween/Exercise_6_09_AngleBetween.pde +++ /dev/null @@ -1,54 +0,0 @@ -// Angle Between Two Vectors -// Daniel Shiffman -// The Nature of Code - -// Using the dot product to compute the angle between two vectors - -void setup() { - size(383, 200); - smooth(); -} - -void draw() { - background(255); - - // A "vector" (really a point) to store the mouse location and screen center location - PVector mouseLoc = new PVector(mouseX, mouseY); - PVector centerLoc = new PVector(width/2, height/2); - - // Aha, a vector to store the displacement between the mouse and center - PVector v = PVector.sub(mouseLoc, centerLoc); - v.normalize(); - v.mult(75); - - PVector xaxis = new PVector(75, 0); - // Render the vector - drawVector(v, centerLoc, 1.0); - drawVector(xaxis, centerLoc, 1.0); - - - float theta = PVector.angleBetween(v, xaxis); - - fill(0); - text(int(degrees(theta)) + " degrees\n" + theta + " radians", 10, 160); -} - -// Renders a vector object 'v' as an arrow and a location 'loc' -void drawVector(PVector v, PVector loc, float scayl) { - pushMatrix(); - float arrowsize = 6; - // Translate to location to render vector - translate(loc.x, loc.y); - stroke(0); - strokeWeight(2); - // Call vector heading function to get direction (pointing up is a heading of 0) - rotate(v.heading2D()); - // 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 - line(0, 0, len, 0); - line(len, 0, len-arrowsize, +arrowsize/2); - line(len, 0, len-arrowsize, -arrowsize/2); - popMatrix(); -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Exercise_6_13_CrowdPathFollowing.pde b/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Exercise_6_13_CrowdPathFollowing.pde deleted file mode 100644 index 8815cbb25..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Exercise_6_13_CrowdPathFollowing.pde +++ /dev/null @@ -1,77 +0,0 @@ -// Crowd Path Following -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Via Reynolds: http://www.red3d.com/cwr/steer/CrowdPath.html - -// Using this variable to decide whether to draw all the stuff -boolean debug = false; - - -// A path object (series of connected points) -Path path; - -// Two vehicles -ArrayList vehicles; - -void setup() { - size(720,200); - smooth(); - // Call a function to generate new Path object - newPath(); - - // We are now making random vehicles and storing them in an ArrayList - vehicles = new ArrayList(); - for (int i = 0; i < 120; i++) { - newVehicle(random(width),random(height)); - } -} - -void draw() { - background(255); - // Display the path - path.display(); - - for (Vehicle v : vehicles) { - // Path following and separation are worked on in this function - v.applyBehaviors(vehicles,path); - // Call the generic run method (update, borders, display, etc.) - v.run(); - } - - // Instructions - fill(0); - textAlign(CENTER); - text("Hit 'd' to toggle debugging lines.\nClick the mouse to generate new vehicles.",width/2,height-20); -} - -void newPath() { - // A path is a series of connected points - // A more sophisticated path might be a curve - path = new Path(); - float offset = 30; - path.addPoint(offset,offset); - path.addPoint(width-offset,offset); - path.addPoint(width-offset,height-offset); - path.addPoint(width/2,height-offset*3); - path.addPoint(offset,height-offset); -} - -void newVehicle(float x, float y) { - float maxspeed = random(2,4); - float maxforce = 0.3; - vehicles.add(new Vehicle(new PVector(x,y),maxspeed,maxforce)); -} - -void keyPressed() { - if (key == 'd') { - debug = !debug; - } -} - -void mousePressed() { - newVehicle(mouseX,mouseY); -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Path.pde b/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Path.pde deleted file mode 100644 index d6ea21a57..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Path.pde +++ /dev/null @@ -1,50 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code - -class Path { - - // A Path is an arraylist of points (PVector objects) - ArrayList points; - // A path has a radius, i.e how far is it ok for the boid to wander off - float radius; - - Path() { - // Arbitrary radius of 20 - radius = 20; - points = new ArrayList(); - } - - // Add a point to the path - void addPoint(float x, float y) { - PVector point = new PVector(x, y); - points.add(point); - } - - // Draw the path - void display() { - strokeJoin(ROUND); - - // Draw thick line for radius - stroke(175); - strokeWeight(radius*2); - noFill(); - beginShape(); - for (PVector v : points) { - vertex(v.x, v.y); - } - endShape(CLOSE); - // Draw thin line for center of path - stroke(0); - strokeWeight(1); - noFill(); - beginShape(); - for (PVector v : points) { - vertex(v.x, v.y); - } - endShape(CLOSE); - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Vehicle.pde deleted file mode 100644 index be7811b49..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/Exercise_6_13_CrowdPathFollowing/Vehicle.pde +++ /dev/null @@ -1,239 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Vehicle class - -class Vehicle { - - // All the usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - // Constructor initialize all values - Vehicle( PVector l, float ms, float mf) { - location = l.get(); - r = 12; - maxspeed = ms; - maxforce = mf; - acceleration = new PVector(0, 0); - velocity = new PVector(maxspeed, 0); - } - - // A function to deal with path following and separation - void applyBehaviors(ArrayList vehicles, Path path) { - // Follow path force - PVector f = follow(path); - // Separate from other boids force - PVector s = separate(vehicles); - // Arbitrary weighting - f.mult(3); - s.mult(1); - // Accumulate in acceleration - applyForce(f); - applyForce(s); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - - // Main "run" function - public void run() { - update(); - borders(); - render(); - } - - - // This function implements Craig Reynolds' path following algorithm - // http://www.red3d.com/cwr/steer/PathFollow.html - PVector follow(Path p) { - - // Predict location 25 (arbitrary choice) frames ahead - PVector predict = velocity.get(); - predict.normalize(); - predict.mult(25); - PVector predictLoc = PVector.add(location, predict); - - // Now we must find the normal to the path from the predicted location - // We look at the normal for each line segment and pick out the closest one - PVector normal = null; - PVector target = null; - float worldRecord = 1000000; // Start with a very high worldRecord distance that can easily be beaten - - // Loop through all points of the path - for (int i = 0; i < p.points.size(); i++) { - - // Look at a line segment - PVector a = p.points.get(i); - PVector b = p.points.get((i+1)%p.points.size()); // Note Path has to wraparound - - // Get the normal point to that line - PVector normalPoint = getNormalPoint(predictLoc, a, b); - - // Check if normal is on line segment - PVector dir = PVector.sub(b, a); - // If it's not within the line segment, consider the normal to just be the end of the line segment (point b) - //if (da + db > line.mag()+1) { - if (normalPoint.x < min(a.x,b.x) || normalPoint.x > max(a.x,b.x) || normalPoint.y < min(a.y,b.y) || normalPoint.y > max(a.y,b.y)) { - normalPoint = b.get(); - // If we're at the end we really want the next line segment for looking ahead - a = p.points.get((i+1)%p.points.size()); - b = p.points.get((i+2)%p.points.size()); // Path wraps around - dir = PVector.sub(b, a); - } - - // How far away are we from the path? - float d = PVector.dist(predictLoc, normalPoint); - // Did we beat the worldRecord and find the closest line segment? - if (d < worldRecord) { - worldRecord = d; - normal = normalPoint; - - // Look at the direction of the line segment so we can seek a little bit ahead of the normal - dir.normalize(); - // This is an oversimplification - // Should be based on distance to path & velocity - dir.mult(25); - target = normal.get(); - target.add(dir); - - } - } - - // Draw the debugging stuff - if (debug) { - // Draw predicted future location - stroke(0); - fill(0); - line(location.x, location.y, predictLoc.x, predictLoc.y); - ellipse(predictLoc.x, predictLoc.y, 4, 4); - - // Draw normal location - stroke(0); - fill(0); - ellipse(normal.x, normal.y, 4, 4); - // Draw actual target (red if steering towards it) - line(predictLoc.x, predictLoc.y, target.x, target.y); - if (worldRecord > p.radius) fill(255, 0, 0); - noStroke(); - ellipse(target.x, target.y, 8, 8); - } - - // Only if the distance is greater than the path's radius do we bother to steer - if (worldRecord > p.radius) { - return seek(target); - } - else { - return new PVector(0, 0); - } - } - - - // A function to get the normal point from a point (p) to a line segment (a-b) - // This function could be optimized to make fewer new Vector objects - PVector getNormalPoint(PVector p, PVector a, PVector b) { - // Vector from a to p - PVector ap = PVector.sub(p, a); - // Vector from a to b - PVector ab = PVector.sub(b, a); - ab.normalize(); // Normalize the line - // Project vector "diff" onto line by using the dot product - ab.mult(ap.dot(ab)); - PVector normalPoint = PVector.add(a, ab); - return normalPoint; - } - - // Separation - // Method checks for nearby boids and steers away - PVector separate (ArrayList boids) { - float desiredseparation = r*2; - PVector steer = new PVector(0, 0, 0); - int count = 0; - // For every boid in the system, check if it's too close - for (int i = 0 ; i < boids.size(); i++) { - Vehicle other = (Vehicle) boids.get(i); - float d = PVector.dist(location, other.location); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if ((d > 0) && (d < desiredseparation)) { - // Calculate vector pointing away from neighbor - PVector diff = PVector.sub(location, other.location); - diff.normalize(); - diff.div(d); // Weight by distance - steer.add(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - steer.div((float)count); - } - - // As long as the vector is greater than 0 - if (steer.mag() > 0) { - // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mult(maxspeed); - steer.sub(velocity); - steer.limit(maxforce); - } - return steer; - } - - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - PVector seek(PVector target) { - PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus Velocationity - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); // Limit to maximum steering force - - return steer; - } - - - void render() { - // Simpler boid is just a circle - fill(75); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - ellipse(0, 0, r, r); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - //if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - //if (location.y > height+r) location.y = -r; - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek/NOC_6_01_Seek.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek/NOC_6_01_Seek.pde deleted file mode 100644 index 91534778c..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek/NOC_6_01_Seek.pde +++ /dev/null @@ -1,36 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman -// Nature of Code, Spring 2011 - -// Two "vehicles" follow the mouse position - -// Implements Craig Reynold's autonomous steering behaviors -// One vehicle "seeks" -// One vehicle "arrives" -// See: http://www.red3d.com/cwr/ - -Vehicle v; - -void setup() { - size(800, 200); - v = new Vehicle(width/2, height/2); - smooth(); -} - -void draw() { - background(255); - - PVector mouse = new PVector(mouseX, mouseY); - - // Draw an ellipse at the mouse location - fill(200); - stroke(0); - strokeWeight(2); - ellipse(mouse.x, mouse.y, 48, 48); - - // Call the appropriate steering behaviors for our agents - v.seek(mouse); - v.update(); - v.display(); -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek/Vehicle.pde deleted file mode 100644 index 55d5e83e9..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek/Vehicle.pde +++ /dev/null @@ -1,74 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman - -// The "Vehicle" class - -class Vehicle { - - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(float x, float y) { - acceleration = new PVector(0,0); - velocity = new PVector(0,-2); - location = new PVector(x,y); - r = 6; - maxspeed = 4; - maxforce = 0.1; - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelerationelertion to 0 each cycle - acceleration.mult(0); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // A method that calculates a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - void seek(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - - applyForce(steer); - } - - void display() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + PI/2; - fill(127); - stroke(0); - strokeWeight(1); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(CLOSE); - popMatrix(); - - - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek_trail/NOC_6_01_Seek_trail.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek_trail/NOC_6_01_Seek_trail.pde deleted file mode 100644 index 129a44bcc..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek_trail/NOC_6_01_Seek_trail.pde +++ /dev/null @@ -1,39 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman -// Nature of Code, Spring 2011 - -// Two "vehicles" follow the mouse position - -// Implements Craig Reynold's autonomous steering behaviors -// One vehicle "seeks" -// One vehicle "arrives" -// See: http://www.red3d.com/cwr/ - -Vehicle v; - -void setup() { - size(800, 200); - v = new Vehicle(width/2, height/2); - smooth(); -} - -void draw() { - if (mousePressed) { - - background(255); - - PVector mouse = new PVector(mouseX, mouseY); - - // Draw an ellipse at the mouse location - fill(200); - stroke(0); - strokeWeight(2); - ellipse(mouse.x, mouse.y, 48, 48); - - // Call the appropriate steering behaviors for our agents - v.seek(mouse); - v.update(); - v.display(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek_trail/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek_trail/Vehicle.pde deleted file mode 100644 index 0491c53ea..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_01_Seek_trail/Vehicle.pde +++ /dev/null @@ -1,90 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman - -// The "Vehicle" class - -class Vehicle { - ArrayList history = new ArrayList(); - - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(float x, float y) { - acceleration = new PVector(0,0); - velocity = new PVector(0,-2); - location = new PVector(x,y); - r = 6; - maxspeed = 4; - maxforce = 0.1; - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelerationelertion to 0 each cycle - acceleration.mult(0); - - history.add(location.get()); - if (history.size() > 100) { - history.remove(0); - } - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // A method that calculates a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - void seek(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - - applyForce(steer); - } - - void display() { - beginShape(); - stroke(0); - strokeWeight(1); - noFill(); - for(PVector v: history) { - vertex(v.x,v.y); - } - endShape(); - - - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + PI/2; - fill(127); - stroke(0); - strokeWeight(1); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(CLOSE); - popMatrix(); - - - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive/NOC_6_02_Arrive.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive/NOC_6_02_Arrive.pde deleted file mode 100644 index 0d6037343..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive/NOC_6_02_Arrive.pde +++ /dev/null @@ -1,35 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman -// Nature of Code, Spring 2011 - -// Two "vehicles" follow the mouse position - -// Implements Craig Reynold's autonomous steering behaviors -// One vehicle "seeks" -// One vehicle "arrives" -// See: http://www.red3d.com/cwr/ - -Vehicle v; - -void setup() { - size(800, 200); - v = new Vehicle(width/2, height/2); - smooth(); -} - -void draw() { - background(255); - - PVector mouse = new PVector(mouseX, mouseY); - - // Draw an ellipse at the mouse location - fill(200); - stroke(0); - strokeWeight(2); - ellipse(mouse.x, mouse.y, 48, 48); - - // Call the appropriate steering behaviors for our agents - v.arrive(mouse); - v.update(); - v.display(); -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive/Vehicle.pde deleted file mode 100644 index d16f0fc94..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive/Vehicle.pde +++ /dev/null @@ -1,80 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman - -// The "Vehicle" class - -class Vehicle { - - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(float x, float y) { - acceleration = new PVector(0,0); - velocity = new PVector(0,0); - location = new PVector(x,y); - r = 6; - maxspeed = 4; - maxforce = 0.1; - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelerationelertion to 0 each cycle - acceleration.mult(0); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // A method that calculates a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - void arrive(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - float d = desired.mag(); - // Normalize desired and scale with arbitrary damping within 100 pixels - desired.normalize(); - if (d < 100) { - float m = map(d,0,100,0,maxspeed); - desired.mult(m); - } else { - desired.mult(maxspeed); - } - - // Steering = Desired minus Velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - applyForce(steer); - } - - void display() { - - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + PI/2; - fill(127); - stroke(0); - strokeWeight(1); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(CLOSE); - popMatrix(); - - - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive_trail/NOC_6_02_Arrive_trail.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive_trail/NOC_6_02_Arrive_trail.pde deleted file mode 100644 index c0efbc4f5..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive_trail/NOC_6_02_Arrive_trail.pde +++ /dev/null @@ -1,38 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman -// Nature of Code, Spring 2011 - -// Two "vehicles" follow the mouse position - -// Implements Craig Reynold's autonomous steering behaviors -// One vehicle "seeks" -// One vehicle "arrives" -// See: http://www.red3d.com/cwr/ - -Vehicle v; - -void setup() { - size(800, 200); - v = new Vehicle(width/2, height/2); - smooth(); -} - -void draw() { - if (mousePressed) { - background(255); - - PVector mouse = new PVector(mouseX, mouseY); - - // Draw an ellipse at the mouse location - fill(200); - stroke(0); - strokeWeight(2); - ellipse(mouse.x, mouse.y, 48, 48); - - // Call the appropriate steering behaviors for our agents - v.arrive(mouse); - v.update(); - v.display(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive_trail/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive_trail/Vehicle.pde deleted file mode 100644 index b6c0f4649..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_02_Arrive_trail/Vehicle.pde +++ /dev/null @@ -1,96 +0,0 @@ -// Seek_Arrive -// Daniel Shiffman - -// The "Vehicle" class - -class Vehicle { - - ArrayList history = new ArrayList(); - - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(float x, float y) { - acceleration = new PVector(0,0); - velocity = new PVector(0,0); - location = new PVector(x,y); - r = 6; - maxspeed = 4; - maxforce = 0.1; - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelerationelertion to 0 each cycle - acceleration.mult(0); - - history.add(location.get()); - if (history.size() > 100) { - history.remove(0); - } - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // A method that calculates a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - void arrive(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - float d = desired.mag(); - // Normalize desired and scale with arbitrary damping within 100 pixels - desired.normalize(); - if (d < 100) { - float m = map(d,0,100,0,maxspeed); - desired.mult(m); - } else { - desired.mult(maxspeed); - } - - // Steering = Desired minus Velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - applyForce(steer); - } - - void display() { - - beginShape(); - stroke(0); - strokeWeight(1); - noFill(); - for(PVector v: history) { - vertex(v.x,v.y); - } - endShape(); - - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + PI/2; - fill(127); - stroke(0); - strokeWeight(1); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(CLOSE); - popMatrix(); - - - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls/NOC_6_03_StayWithinWalls.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls/NOC_6_03_StayWithinWalls.pde deleted file mode 100644 index 5f54281c5..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls/NOC_6_03_StayWithinWalls.pde +++ /dev/null @@ -1,37 +0,0 @@ -// Stay Within Walls -// Daniel Shiffman -// The Nature of Code - -// "Made-up" Steering behavior to stay within walls - - -Vehicle v; -boolean debug = true; - -float d = 25; - - -void setup() { - size(800, 200); - v = new Vehicle(width/2, height/2); - smooth(); -} - -void draw() { - background(255); - - if (debug) { - stroke(175); - noFill(); - rectMode(CENTER); - rect(width/2, height/2, width-d*2, height-d*2); - } - - v.boundaries(); - v.run(); -} - -void mousePressed() { - debug = !debug; -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls/Vehicle.pde deleted file mode 100644 index 7f557cdd2..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls/Vehicle.pde +++ /dev/null @@ -1,92 +0,0 @@ -// Wander -// Daniel Shiffman -// The Nature of Code - -// The "Vehicle" class - -class Vehicle { - - PVector location; - PVector velocity; - PVector acceleration; - float r; - - float maxspeed; - float maxforce; - - Vehicle(float x, float y) { - acceleration = new PVector(0, 0); - velocity = new PVector(3, -2); - velocity.mult(5); - location = new PVector(x, y); - r = 6; - maxspeed = 3; - maxforce = 0.15; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void boundaries() { - - PVector desired = null; - - if (location.x < d) { - desired = new PVector(maxspeed, velocity.y); - } - else if (location.x > width -d) { - desired = new PVector(-maxspeed, velocity.y); - } - - if (location.y < d) { - desired = new PVector(velocity.x, maxspeed); - } - else if (location.y > height-d) { - desired = new PVector(velocity.x, -maxspeed); - } - - if (desired != null) { - desired.normalize(); - desired.mult(maxspeed); - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); - applyForce(steer); - } - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - void display() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(127); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls_trail/NOC_6_03_StayWithinWalls_trail.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls_trail/NOC_6_03_StayWithinWalls_trail.pde deleted file mode 100644 index 5f54281c5..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls_trail/NOC_6_03_StayWithinWalls_trail.pde +++ /dev/null @@ -1,37 +0,0 @@ -// Stay Within Walls -// Daniel Shiffman -// The Nature of Code - -// "Made-up" Steering behavior to stay within walls - - -Vehicle v; -boolean debug = true; - -float d = 25; - - -void setup() { - size(800, 200); - v = new Vehicle(width/2, height/2); - smooth(); -} - -void draw() { - background(255); - - if (debug) { - stroke(175); - noFill(); - rectMode(CENTER); - rect(width/2, height/2, width-d*2, height-d*2); - } - - v.boundaries(); - v.run(); -} - -void mousePressed() { - debug = !debug; -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls_trail/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls_trail/Vehicle.pde deleted file mode 100644 index 0d94054a4..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_03_StayWithinWalls_trail/Vehicle.pde +++ /dev/null @@ -1,108 +0,0 @@ -// Wander -// Daniel Shiffman -// The Nature of Code - -// The "Vehicle" class - -class Vehicle { - ArrayList history = new ArrayList(); - - PVector location; - PVector velocity; - PVector acceleration; - float r; - - float maxspeed; - float maxforce; - - Vehicle(float x, float y) { - acceleration = new PVector(0, 0); - velocity = new PVector(3, -2); - velocity.mult(5); - location = new PVector(x, y); - r = 6; - maxspeed = 3; - maxforce = 0.15; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - - history.add(location.get()); - if (history.size() > 500) { - history.remove(0); - } - } - - void boundaries() { - - PVector desired = null; - - if (location.x < d) { - desired = new PVector(maxspeed, velocity.y); - } - else if (location.x > width -d) { - desired = new PVector(-maxspeed, velocity.y); - } - - if (location.y < d) { - desired = new PVector(velocity.x, maxspeed); - } - else if (location.y > height-d) { - desired = new PVector(velocity.x, -maxspeed); - } - - if (desired != null) { - desired.normalize(); - desired.mult(maxspeed); - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); - applyForce(steer); - } - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - void display() { - beginShape(); - stroke(0); - strokeWeight(1); - noFill(); - for(PVector v: history) { - vertex(v.x,v.y); - } - endShape(); - - - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(127); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/FlowField.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/FlowField.pde deleted file mode 100644 index b3c4c4a73..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/FlowField.pde +++ /dev/null @@ -1,92 +0,0 @@ -// Flow Field Following -// Daniel Shiffman -// The Nature of Code, Spring 2011 - -class FlowField { - - // A flow field is a two dimensional array of PVectors - PVector[][] field; - int cols, rows; // Columns and Rows - int resolution; // How large is each "cell" of the flow field - - FlowField(int r) { - resolution = r; - // Determine the number of columns and rows based on sketch's width and height - cols = width/resolution; - rows = height/resolution; - field = new PVector[cols][rows]; - init(); - } - - void init() { - // Reseed noise so we get a new flow field every time - noiseSeed((int)random(10000)); - float xoff = 0; - for (int i = 0; i < cols; i++) { - float yoff = 0; - for (int j = 0; j < rows; j++) { - //float theta = random(TWO_PI); - //float theta = map(noise(xoff,yoff),0,1,0,TWO_PI); - float x = i*resolution; - float y = j*resolution; - PVector v = new PVector(width/2-x,-y); - v.normalize(); - // Polar to cartesian coordinate transformation to get x and y components of the vector - field[i][j] = v;// new PVector(cos(theta),sin(theta)); - yoff += 0.1; - } - xoff += 0.1; - } - } - - // Draw every vector - void display() { - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - //drawVector(field[i][j],i*resolution,j*resolution,resolution-2); - pushMatrix(); - //translate(i*resolution+arrow.width/2,j*resolution+arrow.height/2); - translate(i*resolution,j*resolution); - rotate(field[i][j].heading2D()); - imageMode(CENTER); - //scale(0.2); - image(a,0,0); - //shape(arrow,-arrow.width/2,-arrow.height/2); - //ellipse(0,0,8,8); - popMatrix(); - } - } - - } - - // Renders a vector object 'v' as an arrow and a location 'x,y' - void drawVector(PVector v, float x, float y, float scayl) { - pushMatrix(); - float arrowsize = 4; - // Translate to location to render vector - translate(x,y); - stroke(0,100); - // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate - rotate(v.heading2D()); - // 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(); - } - - PVector lookup(PVector lookup) { - int column = int(constrain(lookup.x/resolution,0,cols-1)); - int row = int(constrain(lookup.y/resolution,0,rows-1)); - return field[column][row].get(); - } - - -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/NOC_6_04_Flow_Figures.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/NOC_6_04_Flow_Figures.pde deleted file mode 100644 index d688f62f6..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/NOC_6_04_Flow_Figures.pde +++ /dev/null @@ -1,34 +0,0 @@ -// Flow Field Following -// Daniel Shiffman -// The Nature of Code - -// Via Reynolds: http://www.red3d.com/cwr/steer/FlowFollow.html - -// Flowfield object -FlowField flowfield; -PShape arrow; -PImage a; - -void setup() { - size(1800, 60*9); - smooth(); - // Make a new flow field with "resolution" of 16 - flowfield = new FlowField(60); - arrow = loadShape("arrow.svg"); - a = loadImage("arrow60.png"); -} - -void draw() { - background(255); - // Display the flowfield in "debug" mode - translate(30,30); - flowfield.display(); - saveFrame("ch6_exc6.png"); - noLoop(); -} -// Make a new flowfield -void mousePressed() { - flowfield.init(); -} - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/Vehicle.pde deleted file mode 100644 index bd4149236..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/Vehicle.pde +++ /dev/null @@ -1,85 +0,0 @@ -// Flow Field Following -// Daniel Shiffman -// The Nature of Code, Spring 2011 - -class Vehicle { - - // The usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(PVector l, float ms, float mf) { - location = l.get(); - r = 3.0; - maxspeed = ms; - maxforce = mf; - acceleration = new PVector(0,0); - velocity = new PVector(0,0); - } - - public void run() { - update(); - borders(); - display(); - } - - - // Implementing Reynolds' flow field following algorithm - // http://www.red3d.com/cwr/steer/FlowFollow.html - void follow(FlowField flow) { - // What is the vector at that spot in the flow field? - PVector desired = flow.lookup(location); - // Scale it up by maxspeed - desired.mult(maxspeed); - // Steering is desired minus velocity - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); // Limit to maximum steering force - applyForce(steer); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void display() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(175); - stroke(0); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - if (location.y > height+r) location.y = -r; - } -} - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/data/arrow.svg b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/data/arrow.svg deleted file mode 100644 index 0f34115e7..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flow_Figures/data/arrow.svg +++ /dev/null @@ -1,5149 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/FlowField.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/FlowField.pde deleted file mode 100644 index 9ce11b4fe..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/FlowField.pde +++ /dev/null @@ -1,77 +0,0 @@ -// Flow Field Following -// Daniel Shiffman -// The Nature of Code, Spring 2011 - -class FlowField { - - // A flow field is a two dimensional array of PVectors - PVector[][] field; - int cols, rows; // Columns and Rows - int resolution; // How large is each "cell" of the flow field - - FlowField(int r) { - resolution = r; - // Determine the number of columns and rows based on sketch's width and height - cols = width/resolution; - rows = height/resolution; - field = new PVector[cols][rows]; - init(); - } - - void init() { - // Reseed noise so we get a new flow field every time - noiseSeed((int)random(10000)); - float xoff = 0; - for (int i = 0; i < cols; i++) { - float yoff = 0; - for (int j = 0; j < rows; j++) { - float theta = map(noise(xoff,yoff),0,1,0,TWO_PI); - // Polar to cartesian coordinate transformation to get x and y components of the vector - field[i][j] = new PVector(cos(theta),sin(theta)); - yoff += 0.1; - } - xoff += 0.1; - } - } - - // Draw every vector - void display() { - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - drawVector(field[i][j],i*resolution,j*resolution,resolution-2); - } - } - - } - - // Renders a vector object 'v' as an arrow and a location 'x,y' - void drawVector(PVector v, float x, float y, float scayl) { - pushMatrix(); - float arrowsize = 4; - // Translate to location to render vector - translate(x,y); - stroke(0,100); - // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate - rotate(v.heading2D()); - // 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(); - } - - PVector lookup(PVector lookup) { - int column = int(constrain(lookup.x/resolution,0,cols-1)); - int row = int(constrain(lookup.y/resolution,0,rows-1)); - return field[column][row].get(); - } - - -} - - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/NOC_6_04_Flowfield.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/NOC_6_04_Flowfield.pde deleted file mode 100644 index 805ea5962..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/NOC_6_04_Flowfield.pde +++ /dev/null @@ -1,55 +0,0 @@ -// Flow Field Following -// Daniel Shiffman -// The Nature of Code - -// Via Reynolds: http://www.red3d.com/cwr/steer/FlowFollow.html - -// Using this variable to decide whether to draw all the stuff -boolean debug = true; - -// Flowfield object -FlowField flowfield; -// An ArrayList of vehicles -ArrayList vehicles; - -void setup() { - size(800, 200); - smooth(); - // Make a new flow field with "resolution" of 16 - flowfield = new FlowField(20); - vehicles = new ArrayList(); - // Make a whole bunch of vehicles with random maxspeed and maxforce values - for (int i = 0; i < 120; i++) { - vehicles.add(new Vehicle(new PVector(random(width), random(height)), random(2, 5), random(0.1, 0.5))); - } -} - -void draw() { - background(255); - // Display the flowfield in "debug" mode - if (debug) flowfield.display(); - // Tell all the vehicles to follow the flow field - for (Vehicle v : vehicles) { - v.follow(flowfield); - v.run(); - } - - // Instructions - fill(0); - text("Hit space bar to toggle debugging lines.\nClick the mouse to generate a new flow field.",10,height-20); -} - - -void keyPressed() { - if (key == ' ') { - debug = !debug; - } -} - -// Make a new flowfield -void mousePressed() { - flowfield.init(); -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/Vehicle.pde deleted file mode 100644 index bd4149236..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/Vehicle.pde +++ /dev/null @@ -1,85 +0,0 @@ -// Flow Field Following -// Daniel Shiffman -// The Nature of Code, Spring 2011 - -class Vehicle { - - // The usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Vehicle(PVector l, float ms, float mf) { - location = l.get(); - r = 3.0; - maxspeed = ms; - maxforce = mf; - acceleration = new PVector(0,0); - velocity = new PVector(0,0); - } - - public void run() { - update(); - borders(); - display(); - } - - - // Implementing Reynolds' flow field following algorithm - // http://www.red3d.com/cwr/steer/FlowFollow.html - void follow(FlowField flow) { - // What is the vector at that spot in the flow field? - PVector desired = flow.lookup(location); - // Scale it up by maxspeed - desired.mult(maxspeed); - // Steering is desired minus velocity - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); // Limit to maximum steering force - applyForce(steer); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void display() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(175); - stroke(0); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - if (location.y > height+r) location.y = -r; - } -} - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/sketch.properties b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_04_Flowfield/sketch.properties deleted file mode 100644 index e69de29bb..000000000 diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/NOC_6_05_PathFollowingSimple.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/NOC_6_05_PathFollowingSimple.pde deleted file mode 100644 index 0fe9f7b0e..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/NOC_6_05_PathFollowingSimple.pde +++ /dev/null @@ -1,52 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code - -// Path is a just a straight line in this example -// Via Reynolds: // http://www.red3d.com/cwr/steer/PathFollow.html - -// Using this variable to decide whether to draw all the stuff -boolean debug = true; - -// A path object (series of connected points) -Path path; - -// Two vehicles -Vehicle car1; -Vehicle car2; - -void setup() { - size(800, 200); - smooth(); - - path = new Path(); - - // Each vehicle has different maxspeed and maxforce for demo purposes - car1 = new Vehicle(new PVector(0, height/2), 3, 0.05); - car2 = new Vehicle(new PVector(0, height/2), 5, 0.1); -} - -void draw() { - background(255); - // Display the path - path.display(); - // The boids follow the path - car1.follow(path); - car2.follow(path); - // Call the generic run method (update, borders, display, etc.) - car1.run(); - car2.run(); - - // Instructions - fill(0); - text("Hit space bar to toggle debugging lines.", 10, height-30); -} - -public void keyPressed() { - if (key == ' ') { - debug = !debug; - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/Path.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/Path.pde deleted file mode 100644 index 611babb5b..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/Path.pde +++ /dev/null @@ -1,46 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Path Following -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -class Path { - - // A Path is line between two points (PVector objects) - PVector start; - PVector end; - // A path has a radius, i.e how far is it ok for the boid to wander off - float radius; - - Path() { - // Arbitrary radius of 20 - radius = 20; - start = new PVector(0,height/3); - end = new PVector(width,2*height/3); - } - - // Draw the path - void display() { - - strokeWeight(radius*2); - stroke(0,100); - line(start.x,start.y,end.x,end.y); - - strokeWeight(1); - stroke(0); - line(start.x,start.y,end.x,end.y); - } -} - - - - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/Vehicle.pde deleted file mode 100644 index 109d7d658..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/Vehicle.pde +++ /dev/null @@ -1,161 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Vehicle class - -class Vehicle { - - // All the usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - // Constructor initialize all values - Vehicle( PVector l, float ms, float mf) { - location = l.get(); - r = 4.0; - maxspeed = ms; - maxforce = mf; - acceleration = new PVector(0, 0); - velocity = new PVector(maxspeed, 0); - } - - // Main "run" function - void run() { - update(); - borders(); - render(); - } - - - // This function implements Craig Reynolds' path following algorithm - // http://www.red3d.com/cwr/steer/PathFollow.html - void follow(Path p) { - - // Predict location 25 (arbitrary choice) frames ahead - PVector predict = velocity.get(); - predict.normalize(); - predict.mult(25); - PVector predictLoc = PVector.add(location, predict); - - // Look at the line segment - PVector a = p.start; - PVector b = p.end; - - // Get the normal point to that line - PVector normalPoint = getNormalPoint(predictLoc, a, b); - - // Find target point a little further ahead of normal - PVector dir = PVector.sub(b, a); - dir.normalize(); - dir.mult(10); // This could be based on velocity instead of just an arbitrary 10 pixels - PVector target = PVector.add(normalPoint, dir); - - // How far away are we from the path? - float distance = PVector.dist(predictLoc, normalPoint); - // Only if the distance is greater than the path's radius do we bother to steer - if (distance > p.radius) { - seek(target); - } - - - // Draw the debugging stuff - if (debug) { - fill(0); - stroke(0); - line(location.x, location.y, predictLoc.x, predictLoc.y); - ellipse(predictLoc.x, predictLoc.y, 4, 4); - - // Draw normal location - fill(0); - stroke(0); - line(predictLoc.x, predictLoc.y, normalPoint.x, normalPoint.y); - ellipse(normalPoint.x, normalPoint.y, 4, 4); - stroke(0); - if (distance > p.radius) fill(255, 0, 0); - noStroke(); - ellipse(target.x+dir.x, target.y+dir.y, 8, 8); - } - } - - - // A function to get the normal point from a point (p) to a line segment (a-b) - // This function could be optimized to make fewer new Vector objects - PVector getNormalPoint(PVector p, PVector a, PVector b) { - // Vector from a to p - PVector ap = PVector.sub(p, a); - // Vector from a to b - PVector ab = PVector.sub(b, a); - ab.normalize(); // Normalize the line - // Project vector "diff" onto line by using the dot product - ab.mult(ap.dot(ab)); - PVector normalPoint = PVector.add(a, ab); - return normalPoint; - } - - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - void seek(PVector target) { - PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target - - // If the magnitude of desired equals 0, skip out of here - // (We could optimize this to check if x and y are 0 to avoid mag() square root - if (desired.mag() == 0) return; - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus Velocity - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); // Limit to maximum steering force - - applyForce(steer); - } - - void render() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(175); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - rotate(theta); - beginShape(PConstants.TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - //if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - //if (location.y > height+r) location.y = -r; - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/sketch.properties b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/sketch.properties deleted file mode 100644 index 6d28cd598..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_05_PathFollowingSimple/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=JavaScript diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/NOC_6_06_PathFollowing.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/NOC_6_06_PathFollowing.pde deleted file mode 100644 index 09b097ddb..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/NOC_6_06_PathFollowing.pde +++ /dev/null @@ -1,65 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code - -// Via Reynolds: // http://www.red3d.com/cwr/steer/PathFollow.html - -// Using this variable to decide whether to draw all the stuff -boolean debug = true; - -// A path object (series of connected points) -Path path; - -// Two vehicles -Vehicle car1; -Vehicle car2; - -void setup() { - size(800, 200); - smooth(); - - // Call a function to generate new Path object - newPath(); - - // Each vehicle has different maxspeed and maxforce for demo purposes - car1 = new Vehicle(new PVector(0, height/2), 3, 0.1); - car2 = new Vehicle(new PVector(0, height/2), 5, 0.2); -} - -void draw() { - background(255); - // Display the path - path.display(); - // The boids follow the path - car1.follow(path); - car2.follow(path); - // Call the generic run method (update, borders, display, etc.) - car1.run(); - car2.run(); - - // Instructions - fill(0); - text("Hit space bar to toggle debugging lines.\nClick the mouse to generate a new path.", 10, height-30); -} - -void newPath() { - // A path is a series of connected points - // A more sophisticated path might be a curve - path = new Path(); - path.addPoint(0, height/2); - path.addPoint(random(0, width/2), random(0, height)); - path.addPoint(random(width/2, width), random(0, height)); - path.addPoint(width, height/2); -} - -public void keyPressed() { - if (key == ' ') { - debug = !debug; - } -} - -public void mousePressed() { - newPath(); -} - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/Path.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/Path.pde deleted file mode 100644 index d408cd460..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/Path.pde +++ /dev/null @@ -1,48 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code - -class Path { - - // A Path is an arraylist of points (PVector objects) - ArrayList points; - // A path has a radius, i.e how far is it ok for the boid to wander off - float radius; - - Path() { - // Arbitrary radius of 20 - radius = 20; - points = new ArrayList(); - } - - // Add a point to the path - void addPoint(float x, float y) { - PVector point = new PVector(x, y); - points.add(point); - } - - // Draw the path - void display() { - // Draw thick line for radius - stroke(175); - strokeWeight(radius*2); - noFill(); - beginShape(); - for (PVector v : points) { - vertex(v.x, v.y); - } - endShape(); - // Draw thin line for center of path - stroke(0); - strokeWeight(1); - noFill(); - beginShape(); - for (PVector v : points) { - vertex(v.x, v.y); - } - endShape(); - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/Vehicle.pde deleted file mode 100644 index 543e45c8e..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_06_PathFollowing/Vehicle.pde +++ /dev/null @@ -1,190 +0,0 @@ -// Path Following -// Daniel Shiffman -// The Nature of Code - -// Vehicle class - -class Vehicle { - - // All the usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - // Constructor initialize all values - Vehicle( PVector l, float ms, float mf) { - location = l.get(); - r = 4.0; - maxspeed = ms; - maxforce = mf; - acceleration = new PVector(0, 0); - velocity = new PVector(maxspeed, 0); - } - - // Main "run" function - public void run() { - update(); - borders(); - render(); - } - - - // This function implements Craig Reynolds' path following algorithm - // http://www.red3d.com/cwr/steer/PathFollow.html - void follow(Path p) { - - // Predict location 25 (arbitrary choice) frames ahead - PVector predict = velocity.get(); - predict.normalize(); - predict.mult(25); - PVector predictLoc = PVector.add(location, predict); - - // Now we must find the normal to the path from the predicted location - // We look at the normal for each line segment and pick out the closest one - - PVector normal = null; - PVector target = null; - float worldRecord = 1000000; // Start with a very high record distance that can easily be beaten - - // Loop through all points of the path - for (int i = 0; i < p.points.size()-1; i++) { - - // Look at a line segment - PVector a = p.points.get(i); - PVector b = p.points.get(i+1); - - // Get the normal point to that line - PVector normalPoint = getNormalPoint(predictLoc, a, b); - // This only works because we know our path goes from left to right - // We could have a more sophisticated test to tell if the point is in the line segment or not - if (normalPoint.x < a.x || normalPoint.x > b.x) { - // This is something of a hacky solution, but if it's not within the line segment - // consider the normal to just be the end of the line segment (point b) - normalPoint = b.get(); - } - - // How far away are we from the path? - float distance = PVector.dist(predictLoc, normalPoint); - // Did we beat the record and find the closest line segment? - if (distance < worldRecord) { - worldRecord = distance; - // If so the target we want to steer towards is the normal - normal = normalPoint; - - // Look at the direction of the line segment so we can seek a little bit ahead of the normal - PVector dir = PVector.sub(b, a); - dir.normalize(); - // This is an oversimplification - // Should be based on distance to path & velocity - dir.mult(10); - target = normalPoint.get(); - target.add(dir); - } - } - - // Only if the distance is greater than the path's radius do we bother to steer - if (worldRecord > p.radius) { - seek(target); - } - - - // Draw the debugging stuff - if (debug) { - // Draw predicted future location - stroke(0); - fill(0); - line(location.x, location.y, predictLoc.x, predictLoc.y); - ellipse(predictLoc.x, predictLoc.y, 4, 4); - - // Draw normal location - stroke(0); - fill(0); - ellipse(normal.x, normal.y, 4, 4); - // Draw actual target (red if steering towards it) - line(predictLoc.x, predictLoc.y, normal.x, normal.y); - if (worldRecord > p.radius) fill(255, 0, 0); - noStroke(); - ellipse(target.x, target.y, 8, 8); - } - } - - - // A function to get the normal point from a point (p) to a line segment (a-b) - // This function could be optimized to make fewer new Vector objects - PVector getNormalPoint(PVector p, PVector a, PVector b) { - // Vector from a to p - PVector ap = PVector.sub(p, a); - // Vector from a to b - PVector ab = PVector.sub(b, a); - ab.normalize(); // Normalize the line - // Project vector "diff" onto line by using the dot product - ab.mult(ap.dot(ab)); - PVector normalPoint = PVector.add(a, ab); - return normalPoint; - } - - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - void seek(PVector target) { - PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target - - // If the magnitude of desired equals 0, skip out of here - // (We could optimize this to check if x and y are 0 to avoid mag() square root - if (desired.mag() == 0) return; - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus Velocity - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); // Limit to maximum steering force - - applyForce(steer); - } - - void render() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(175); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - rotate(theta); - beginShape(PConstants.TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - //if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - //if (location.y > height+r) location.y = -r; - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_07_Separation/NOC_6_07_Separation.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_07_Separation/NOC_6_07_Separation.pde deleted file mode 100644 index 92d96bb0e..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_07_Separation/NOC_6_07_Separation.pde +++ /dev/null @@ -1,45 +0,0 @@ -// Separation -// Daniel Shiffman -// The Nature of Code, 2011 - -// Via Reynolds: http://www.red3d.com/cwr/steer/ - -// A list of vehicles -ArrayList vehicles; - -void setup() { - size(800,200); - smooth(); - - // We are now making random vehicles and storing them in an ArrayList - vehicles = new ArrayList(); - for (int i = 0; i < 100; i++) { - vehicles.add(new Vehicle(random(width),random(height))); - } -} - -void draw() { - background(255); - - for (Vehicle v : vehicles) { - // Path following and separation are worked on in this function - v.separate(vehicles); - // Call the generic run method (update, borders, display, etc.) - v.update(); - v.borders(); - v.display(); - } - - // Instructions - fill(0); - text("Drag the mouse to generate new vehicles.",10,height-16); -} - - -void mouseDragged() { - vehicles.add(new Vehicle(mouseX,mouseY)); -} - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_07_Separation/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_07_Separation/Vehicle.pde deleted file mode 100644 index e115f2394..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_07_Separation/Vehicle.pde +++ /dev/null @@ -1,99 +0,0 @@ -// Separation -// Daniel Shiffman -// The Nature of Code, 2011 - -// Vehicle class - -class Vehicle { - - // All the usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - // Constructor initialize all values - Vehicle(float x, float y) { - location = new PVector(x, y); - r = 12; - maxspeed = 3; - maxforce = 0.2; - acceleration = new PVector(0, 0); - velocity = new PVector(0, 0); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // Separation - // Method checks for nearby vehicles and steers away - void separate (ArrayList vehicles) { - float desiredseparation = r*2; - PVector sum = new PVector(); - int count = 0; - // For every boid in the system, check if it's too close - for (Vehicle other : vehicles) { - float d = PVector.dist(location, other.location); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if ((d > 0) && (d < desiredseparation)) { - // Calculate vector pointing away from neighbor - PVector diff = PVector.sub(location, other.location); - diff.normalize(); - diff.div(d); // Weight by distance - sum.add(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - sum.div(count); - // Our desired vector is the average scaled to maximum speed - sum.normalize(); - sum.mult(maxspeed); - // Implement Reynolds: Steering = Desired - Velocity - PVector steer = PVector.sub(sum, velocity); - steer.limit(maxforce); - applyForce(steer); - } - } - - - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void display() { - fill(175); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - ellipse(0, 0, r, r); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - if (location.y > height+r) location.y = -r; - } -} - - - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/NOC_6_08_SeparationAndSeek.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/NOC_6_08_SeparationAndSeek.pde deleted file mode 100644 index c1614c97f..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/NOC_6_08_SeparationAndSeek.pde +++ /dev/null @@ -1,43 +0,0 @@ -// Separation -// Daniel Shiffman -// The Nature of Code, 2011 - -// Via Reynolds: http://www.red3d.com/cwr/steer/ - -// A list of vehicles -ArrayList vehicles; - -void setup() { - size(800,200); - smooth(); - - // We are now making random vehicles and storing them in an ArrayList - vehicles = new ArrayList(); - for (int i = 0; i < 100; i++) { - vehicles.add(new Vehicle(random(width),random(height))); - } -} - -void draw() { - background(255); - - for (Vehicle v : vehicles) { - // Path following and separation are worked on in this function - v.applyBehaviors(vehicles); - // Call the generic run method (update, borders, display, etc.) - v.update(); - v.display(); - } - - // Instructions - fill(0); - text("Drag the mouse to generate new vehicles.",10,height-16); -} - - -void mouseDragged() { - vehicles.add(new Vehicle(mouseX,mouseY)); -} - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/Vehicle.pde deleted file mode 100644 index a78653ffe..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/Vehicle.pde +++ /dev/null @@ -1,115 +0,0 @@ -// Separation -// Daniel Shiffman -// The Nature of Code, 2011 - -// Vehicle class - -class Vehicle { - - // All the usual stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - // Constructor initialize all values - Vehicle(float x, float y) { - location = new PVector(x, y); - r = 12; - maxspeed = 3; - maxforce = 0.2; - acceleration = new PVector(0, 0); - velocity = new PVector(0, 0); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - void applyBehaviors(ArrayList vehicles) { - PVector separateForce = separate(vehicles); - PVector seekForce = seek(new PVector(mouseX,mouseY)); - separateForce.mult(2); - seekForce.mult(1); - applyForce(separateForce); - applyForce(seekForce); - } - - // A method that calculates a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - PVector seek(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - - return steer; - } - - // Separation - // Method checks for nearby vehicles and steers away - PVector separate (ArrayList vehicles) { - float desiredseparation = r*2; - PVector sum = new PVector(); - int count = 0; - // For every boid in the system, check if it's too close - for (Vehicle other : vehicles) { - float d = PVector.dist(location, other.location); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if ((d > 0) && (d < desiredseparation)) { - // Calculate vector pointing away from neighbor - PVector diff = PVector.sub(location, other.location); - diff.normalize(); - diff.div(d); // Weight by distance - sum.add(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - sum.div(count); - // Our desired vector is the average scaled to maximum speed - sum.normalize(); - sum.mult(maxspeed); - // Implement Reynolds: Steering = Desired - Velocity - sum.sub(velocity); - sum.limit(maxforce); - } - return sum; - } - - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void display() { - fill(175); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - ellipse(0, 0, r, r); - popMatrix(); - } - -} - - - - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/sketch.properties b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/sketch.properties deleted file mode 100644 index 6d28cd598..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_08_SeparationAndSeek/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=JavaScript diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/Boid.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/Boid.pde deleted file mode 100644 index 282e12f29..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/Boid.pde +++ /dev/null @@ -1,182 +0,0 @@ -// Flocking -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Boid class -// Methods for Separation, Cohesion, Alignment added - -class Boid { - - PVector location; - PVector velocity; - PVector acceleration; - float r; - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Boid(float x, float y) { - acceleration = new PVector(0,0); - velocity = new PVector(random(-1,1),random(-1,1)); - location = new PVector(x,y); - r = 3.0; - maxspeed = 3; - maxforce = 0.05; - } - - void run(ArrayList boids) { - flock(boids); - update(); - borders(); - render(); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - // We accumulate a new acceleration each time based on three rules - void flock(ArrayList boids) { - PVector sep = separate(boids); // Separation - PVector ali = align(boids); // Alignment - PVector coh = cohesion(boids); // Cohesion - // Arbitrarily weight these forces - sep.mult(1.5); - ali.mult(1.0); - coh.mult(1.0); - // Add the force vectors to acceleration - applyForce(sep); - applyForce(ali); - applyForce(coh); - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - PVector seek(PVector target) { - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus Velocity - PVector steer = PVector.sub(desired,velocity); - steer.limit(maxforce); // Limit to maximum steering force - return steer; - } - - void render() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(175); - stroke(0); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - if (location.y > height+r) location.y = -r; - } - - // Separation - // Method checks for nearby boids and steers away - PVector separate (ArrayList boids) { - float desiredseparation = 25.0f; - PVector steer = new PVector(0,0,0); - int count = 0; - // For every boid in the system, check if it's too close - for (Boid other : boids) { - float d = PVector.dist(location,other.location); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if ((d > 0) && (d < desiredseparation)) { - // Calculate vector pointing away from neighbor - PVector diff = PVector.sub(location,other.location); - diff.normalize(); - diff.div(d); // Weight by distance - steer.add(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - steer.div((float)count); - } - - // As long as the vector is greater than 0 - if (steer.mag() > 0) { - // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mult(maxspeed); - steer.sub(velocity); - steer.limit(maxforce); - } - return steer; - } - - // Alignment - // For every nearby boid in the system, calculate the average velocity - PVector align (ArrayList boids) { - float neighbordist = 50; - PVector sum = new PVector(0,0); - int count = 0; - for (Boid other : boids) { - float d = PVector.dist(location,other.location); - if ((d > 0) && (d < neighbordist)) { - sum.add(other.velocity); - count++; - } - } - if (count > 0) { - sum.div((float)count); - sum.normalize(); - sum.mult(maxspeed); - PVector steer = PVector.sub(sum,velocity); - steer.limit(maxforce); - return steer; - } else { - return new PVector(0,0); - } - } - - // Cohesion - // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location - PVector cohesion (ArrayList boids) { - float neighbordist = 50; - PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations - int count = 0; - for (Boid other : boids) { - float d = PVector.dist(location,other.location); - if ((d > 0) && (d < neighbordist)) { - sum.add(other.location); // Add location - count++; - } - } - if (count > 0) { - sum.div(count); - return seek(sum); // Steer towards the location - } else { - return new PVector(0,0); - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/Flock.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/Flock.pde deleted file mode 100644 index eb7458e5a..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/Flock.pde +++ /dev/null @@ -1,25 +0,0 @@ -// Flocking -// Daniel Shiffman -// The Nature of Code, Spring 2011 - -// Flock class -// Does very little, simply manages the ArrayList of all the boids - -class Flock { - ArrayList boids; // An ArrayList for all the boids - - Flock() { - boids = new ArrayList(); // Initialize the ArrayList - } - - void run() { - for (Boid b : boids) { - b.run(boids); // Passing the entire list of boids to each boid individually - } - } - - void addBoid(Boid b) { - boids.add(b); - } - -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/NOC_6_09_Flocking.pde b/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/NOC_6_09_Flocking.pde deleted file mode 100644 index c1c12b7f3..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/NOC_6_09_Flocking/NOC_6_09_Flocking.pde +++ /dev/null @@ -1,38 +0,0 @@ -// Flocking -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Demonstration of Craig Reynolds' "Flocking" behavior -// See: http://www.red3d.com/cwr/ -// Rules: Cohesion, Separation, Alignment - -// Click mouse to add boids into the system - -Flock flock; - -void setup() { - size(800,200); - flock = new Flock(); - // Add an initial set of boids into the system - for (int i = 0; i < 200; i++) { - Boid b = new Boid(width/2,height/2); - flock.addBoid(b); - } - smooth(); -} - -void draw() { - background(255); - flock.run(); - - // Instructions - fill(0); - //text("Drag the mouse to generate new boids.",10,height-16); -} - -// Add a new boid into the System -void mouseDragged() { - flock.addBoid(new Boid(mouseX,mouseY)); -} - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/StayWithinCircle/StayWithinCircle.pde b/java/examples/Books/Nature of Code/chp6_agents/StayWithinCircle/StayWithinCircle.pde deleted file mode 100644 index 0b42a37be..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/StayWithinCircle/StayWithinCircle.pde +++ /dev/null @@ -1,43 +0,0 @@ -// Stay Within Walls -// Daniel Shiffman -// The Nature of Code - -// "Made-up" Steering behavior to stay within walls - - -Vehicle v; -boolean debug = true; - - -PVector circleLocation; -float circleRadius; - - - -void setup() { - size(640, 360); - v = new Vehicle(width/2, height/4); - - circleLocation = new PVector(width/2,height/2); - circleRadius = height/2-25; - - smooth(); -} - -void draw() { - background(255); - - if (debug) { - stroke(175); - noFill(); - ellipse(circleLocation.x,circleLocation.y, circleRadius*2,circleRadius*2); - } - - v.boundaries(); - v.run(); -} - -void mousePressed() { - debug = !debug; -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/StayWithinCircle/Vehicle.pde b/java/examples/Books/Nature of Code/chp6_agents/StayWithinCircle/Vehicle.pde deleted file mode 100644 index fcd3e7a56..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/StayWithinCircle/Vehicle.pde +++ /dev/null @@ -1,95 +0,0 @@ -// Wander -// Daniel Shiffman -// The Nature of Code - -// The "Vehicle" class (for wandering) - -class Vehicle { - - PVector location; - PVector velocity; - PVector acceleration; - float r; - - float maxspeed; - float maxforce; - - Vehicle(float x, float y) { - acceleration = new PVector(0, 0); - velocity = new PVector(1,0); - velocity.mult(5); - location = new PVector(x, y); - r = 3; - maxspeed = 3; - maxforce = 0.15; - } - - void run() { - update(); - display(); - } - - // Method to update location - void update() { - // Update velocity - velocity.add(acceleration); - // Limit speed - velocity.limit(maxspeed); - location.add(velocity); - // Reset accelertion to 0 each cycle - acceleration.mult(0); - } - - void boundaries() { - - PVector desired = null; - - // Predict location 25 (arbitrary choice) frames ahead - PVector predict = velocity.get(); - predict.mult(25); - PVector futureLocation = PVector.add(location, predict); - float distance = PVector.dist(futureLocation,circleLocation); - - if (distance > circleRadius) { - PVector toCenter = PVector.sub(circleLocation,location); - toCenter.normalize(); - toCenter.mult(velocity.mag()); - desired = PVector.add(velocity,toCenter); - desired.normalize(); - desired.mult(maxspeed); - } - - if (desired != null) { - PVector steer = PVector.sub(desired, velocity); - steer.limit(maxforce); - applyForce(steer); - } - - fill(255,0,0); - ellipse(futureLocation.x,futureLocation.y,4,4); - - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acceleration.add(force); - } - - - void display() { - // Draw a triangle rotated in the direction of velocity - float theta = velocity.heading2D() + radians(90); - fill(175); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection/Thing.pde b/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection/Thing.pde deleted file mode 100644 index 5618479db..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection/Thing.pde +++ /dev/null @@ -1,32 +0,0 @@ -// Daniel Shiffman -// -// April 6, 2006 - -// Simple class describing an ellipse living on our screen - -class Thing { - - float x,y; - boolean highlight; - float r; - - Thing (float x_, float y_) { - x = x_; - y = y_; - highlight = false; - r = random(8) + 1; - } - - void move() { - x += random(-1,1); - y += random(-1,1); - } - - void render() { - noStroke(); - if (highlight) fill(255); - else fill(100); - ellipse(x,y,r,r); - } - -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection/intersection.pde b/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection/intersection.pde deleted file mode 100644 index 542c90d3b..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection/intersection.pde +++ /dev/null @@ -1,106 +0,0 @@ -// Daniel Shiffman -// - -// Bin-Lattice Spatial Subdivision -// http://www.red3d.com/cwr/papers/2000/pip.pdf - -// Example demonstrating optimized intersection test for large # of objects -// Each object registers its location in a virtual grid -// Only the objects in neighboring cells on the grid are tested against each other - -int totalThings = 2000; - -ArrayList a; // ArrayList for all "things" -ArrayList[][] grid; // Grid of ArrayLists for intersection test -int scl = 4; // Size of each grid cell -int cols, rows; // Total coluns and rows - -void setup() { - size(640,360); - smooth(); - - a = new ArrayList(); // Create the list - cols = width/scl; // Calculate cols & rows - rows = height/scl; - - // Initialize grid as 2D array of empty ArrayLists - grid = new ArrayList[cols][rows]; - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - grid[i][j] = new ArrayList(); - } - } - - // Put 2000 Things in the system - for (int i = 0; i < totalThings; i++) { - a.add(new Thing(random(width),random(height))); - } - -} - -void draw() { - background(0); - - // Every time through draw clear all the lists - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - grid[i][j].clear(); - } - } - - // Register every Thing object in the grid according to it's location - for (Thing t : a) { - t.highlight = false; - int x = int(t.x) / scl; - int y = int (t.y) /scl; - // It goes in 9 cells, i.e. every Thing is tested against other Things in its cell - // as well as its 8 neighbors - for (int n = -1; n <= 1; n++) { - for (int m = -1; m <= 1; m++) { - if (x+n >= 0 && x+n < cols && y+m >= 0 && y+m< rows) grid[x+n][y+m].add(t); - } - } - } - - // Run through the Grid - stroke(255); - for (int i = 0; i < cols; i++) { - //line(i*scl,0,i*scl,height); - for (int j = 0; j < rows; j++) { - //line(0,j*scl,width,j*scl); - - // For every list in the grid - ArrayList temp = grid[i][j]; - // Check every Thing - for (Thing t : temp) { - // Against every other Thing - for (Thing other : temp) { - // As long as its not the same one - if (other != t) { - // Check to see if they are touching - // (We could do many other things here besides just intersection tests, such - // as apply forces, etc.) - float d = dist(t.x,t.y,other.x,other.y); - if (d < t.r/2 + other.r/2) { - t.highlight = true; - } - } - } - } - } - } - - // Display and move all Things - for (Thing t : a) { - t.render(); - t.move(); - } - - fill(0); - rect(0,height-20,width,20); - fill(255); - text("Framerate: " + int(frameRate),10,height-6); - - - -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection_slow/Thing.pde b/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection_slow/Thing.pde deleted file mode 100644 index 5618479db..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection_slow/Thing.pde +++ /dev/null @@ -1,32 +0,0 @@ -// Daniel Shiffman -// -// April 6, 2006 - -// Simple class describing an ellipse living on our screen - -class Thing { - - float x,y; - boolean highlight; - float r; - - Thing (float x_, float y_) { - x = x_; - y = y_; - highlight = false; - r = random(8) + 1; - } - - void move() { - x += random(-1,1); - y += random(-1,1); - } - - void render() { - noStroke(); - if (highlight) fill(255); - else fill(100); - ellipse(x,y,r,r); - } - -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection_slow/intersection_slow.pde b/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection_slow/intersection_slow.pde deleted file mode 100644 index 5c3beae4e..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/binlatticespatialsubdivision/intersection/intersection_slow/intersection_slow.pde +++ /dev/null @@ -1,59 +0,0 @@ -// Daniel Shiffman -// - -// The old way to do intersection tests, look how slow!! - - -int totalThings = 2000; - -ArrayList a; // ArrayList for all "things" - -void setup() { - size(640,360); - a = new ArrayList(); // Create the list - - // Put 2000 Things in the system - for (int i = 0; i < totalThings; i++) { - a.add(new Thing(random(width),random(height))); - } - -} - -void draw() { - background(0); - fill(255); - noStroke(); - smooth(); - - // Run through the Grid - stroke(255); - for (Thing t : a) { - t.highlight = false; - for (Thing other : a) { - // As long as its not the same one - if (t != other) { - // Check to see if they are touching - // (We could do many other things here besides just intersection tests, such - // as apply forces, etc.) - float d = dist(t.x,t.y,other.x,other.y); - if (d < t.r/2 + other.r/2) { - t.highlight = true; - } - } - } - } - - // Display and move all Things - for (Thing t : a) { - t.render(); - t.move(); - } - - fill(0); - rect(0,height-20,width,20); - fill(255); - text("Framerate: " + int(frameRate),10,height-6); - - - -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Boid.pde b/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Boid.pde deleted file mode 100644 index c46398ee9..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Boid.pde +++ /dev/null @@ -1,255 +0,0 @@ -// Flocking -// Daniel Shiffman -// The Nature of Code - -// Boid class -// Methods for Separation, Cohesion, Alignment added - -class Boid { - - // We need to keep track of a Body and a width and height - Body body; - float w; - float h; - - float maxforce; // Maximum steering force - float maxspeed; // Maximum speed - - Boid(PVector loc) { - w = 12; - h = 12; - // Add the box to the box2d world - makeBody(new Vec2(loc.x,loc.y),w,h,new Vec2(0,0),0); - maxspeed = 20; - maxforce = 10; - } - - // This function removes the particle from the box2d world - void killBody() { - box2d.destroyBody(body); - } - - void run(ArrayList boids) { - flock(boids); - borders(); - display(); - } - - // We accumulate a new acceleration each time based on three rules - void flock(ArrayList boids) { - Vec2 sep = separate(boids); // Separation - Vec2 ali = align(boids); // Alignment - Vec2 coh = cohesion(boids); // Cohesion - // Arbitrarily weight these forces - sep.mulLocal(1.5); - ali.mulLocal(1); - coh.mulLocal(1); - // Add the force vectors to acceleration - Vec2 loc = body.getWorldCenter(); - body.applyForce(sep,loc); - body.applyForce(ali,loc); - body.applyForce(coh,loc); - } - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - Vec2 seek(Vec2 target) { - Vec2 loc = body.getWorldCenter(); - Vec2 desired = target.sub(loc); // A vector pointing from the location to the target - - // If the magnitude of desired equals 0, skip out of here - // (We could optimize this to check if x and y are 0 to avoid mag() square root - if (desired.length() == 0) return new Vec2(0,0); - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mulLocal(maxspeed); - // Steering = Desired minus Velocity - - Vec2 vel = body.getLinearVelocity(); - Vec2 steer = desired.sub(vel); - - float len = steer.length(); - if (len > maxforce) { - steer.normalize(); - steer.mulLocal(maxforce); - } - return steer; - } - - - - // Drawing the box - void display() { - // We look at each body and get its screen position - Vec2 pos = box2d.getBodyPixelCoord(body); - - // Get its angle of rotation - float a = body.getAngle(); - - rectMode(CENTER); - pushMatrix(); - translate(pos.x,pos.y); - rotate(-a); - fill(175); - strokeWeight(2); - stroke(0); - rect(0,0,w,h); - popMatrix(); - } - - // Wraparound - void borders() { - Vec2 loc = box2d.getBodyPixelCoord(body); - Vec2 vel = body.getLinearVelocity(); - float a = body.getAngularVelocity(); - if (loc.x < -w) { - killBody(); - makeBody(new Vec2(width+w,loc.y),w,h,vel,a); - } else if (loc.y < -w) { - killBody(); - makeBody(new Vec2(loc.x,height+w),w,h,vel,a); - } else if (loc.x > width+w) { - killBody(); - makeBody(new Vec2(-w,loc.y),w,h,vel,a); - } else if (loc.y > height+w) { - killBody(); - makeBody(new Vec2(loc.x,-w),w,h,vel,a); - } - } - - // Separation - // Method checks for nearby boids and steers away - Vec2 separate (ArrayList boids) { - float desiredseparation = box2d.scalarPixelsToWorld(30); - - Vec2 steer = new Vec2(0,0); - int count = 0; - // For every boid in the system, check if it's too close - Vec2 locA = body.getWorldCenter(); - for (Boid other : boids) { - Vec2 locB = other.body.getWorldCenter(); - float d = dist(locA.x,locA.y,locB.x,locB.y); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if ((d > 0) && (d < desiredseparation)) { - // Calculate vector pointing away from neighbor - Vec2 diff = locA.sub(locB); - diff.normalize(); - diff.mulLocal(1.0/d); // Weight by distance - steer.addLocal(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - steer.mulLocal(1.0/count); - } - - // As long as the vector is greater than 0 - if (steer.length() > 0) { - // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mulLocal(maxspeed); - Vec2 vel = body.getLinearVelocity(); - steer.subLocal(vel); - float len = steer.length(); - if (len > maxforce) { - steer.normalize(); - steer.mulLocal(maxforce); - } - } - return steer; - } - - // Alignment - // For every nearby boid in the system, calculate the average velocity - Vec2 align (ArrayList boids) { - float neighbordist = box2d.scalarPixelsToWorld(50); - Vec2 steer = new Vec2(0,0); - int count = 0; - Vec2 locA = body.getWorldCenter(); - for (Boid other : boids) { - Vec2 locB = other.body.getWorldCenter(); - float d = dist(locA.x,locA.y,locB.x,locB.y); - if ((d > 0) && (d < neighbordist)) { - Vec2 vel = other.body.getLinearVelocity(); - steer.addLocal(vel); - count++; - } - } - if (count > 0) { - steer.mulLocal(1.0/count); - } - - // As long as the vector is greater than 0 - if (steer.length() > 0) { - // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mulLocal(maxspeed); - Vec2 vel = body.getLinearVelocity(); - steer.subLocal(vel); - float len = steer.length(); - if (len > maxforce) { - steer.normalize(); - steer.mulLocal(maxforce); - } - } - return steer; - } - - // Cohesion - // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location - Vec2 cohesion (ArrayList boids) { - float neighbordist = box2d.scalarPixelsToWorld(50); - Vec2 sum = new Vec2(0,0); // Start with empty vector to accumulate all locations - int count = 0; - Vec2 locA = body.getWorldCenter(); - for (Boid other : boids) { - Vec2 locB = other.body.getWorldCenter(); - - float d = dist(locA.x,locA.y,locB.x,locB.y); - if ((d > 0) && (d < neighbordist)) { - sum.addLocal(locB); // Add location - count++; - } - } - if (count > 0) { - sum.mulLocal(1.0/count); - return seek(sum); // Steer towards the location - } - return sum; - } - - // This function adds the rectangle to the box2d world - void makeBody(Vec2 center, float w_, float h_, Vec2 vel, float avel) { - - // Define a polygon (this is what we use for a rectangle) - PolygonShape sd = new PolygonShape(); - float box2dW = box2d.scalarPixelsToWorld(w_/2); - float box2dH = box2d.scalarPixelsToWorld(h_/2); - sd.setAsBox(box2dW, box2dH); - - // Define a fixture - FixtureDef fd = new FixtureDef(); - fd.shape = sd; - // Parameters that affect physics - fd.density = 1; - fd.friction = 0.3; - fd.restitution = 0.5; - - // Define the body and make it from the shape - BodyDef bd = new BodyDef(); - bd.type = BodyType.DYNAMIC; - bd.position.set(box2d.coordPixelsToWorld(center)); - - body = box2d.createBody(bd); - body.createFixture(fd); - - body.setLinearVelocity(vel); - body.setAngularVelocity(avel); - - } - - -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Flock.pde b/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Flock.pde deleted file mode 100644 index b497c279c..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Flock.pde +++ /dev/null @@ -1,25 +0,0 @@ -// Flocking -// Daniel Shiffman -// The Nature of Code - -// Flock class -// Does very little, simply manages the ArrayList of all the boids - -class Flock { - ArrayList boids; // An arraylist for all the boids - - Flock() { - boids = new ArrayList(); // Initialize the arraylist - } - - void run() { - for (Boid b : boids) { - b.run(boids); // Passing the entire list of boids to each boid individually - } - } - - void addBoid(Boid b) { - boids.add(b); - } - -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Flocking_box2d.pde b/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Flocking_box2d.pde deleted file mode 100644 index 3a5934e66..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/box2d/Flocking_box2d/Flocking_box2d.pde +++ /dev/null @@ -1,56 +0,0 @@ -// Flocking -// Daniel Shiffman -// The Nature of Code - -// Demonstration of Craig Reynolds' "Flocking" behavior -// See: http://www.red3d.com/cwr/ -// Rules: Cohesion, Separation, Alignment - -// Click mouse to add boids into the system - -import pbox2d.*; -import org.jbox2d.collision.shapes.*; -import org.jbox2d.common.*; -import org.jbox2d.dynamics.*; - -// A reference to our box2d world -PBox2D box2d; - -Flock flock; - -void setup() { - size(640,360); - // Initialize box2d physics and create the world - box2d = new PBox2D(this); - box2d.createWorld(); - // We are setting a custom gravity - box2d.setGravity(0,0); - - flock = new Flock(); - // Add an initial set of boids into the system - for (int i = 0; i < 50; i++) { - flock.addBoid(new Boid(new PVector(random(width),random(height)))); - } - smooth(); -} - -void draw() { - - - // We must always step through time! - box2d.step(); - - background(255); - flock.run(); - -} - -void mousePressed() { - flock.addBoid(new Boid(new PVector(mouseX,mouseY))); -} - -void mouseDragged() { - flock.addBoid(new Boid(new PVector(mouseX,mouseY))); -} - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/Boid.pde b/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/Boid.pde deleted file mode 100644 index ee361fe01..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/Boid.pde +++ /dev/null @@ -1,181 +0,0 @@ -float swt = 25.0; //sep.mult(25.0f); -float awt = 4.0; //ali.mult(4.0f); -float cwt = 5.0; //coh.mult(5.0f); -float maxspeed = 1; -float maxforce = 0.025; - - -// Flocking -// Daniel Shiffman -// The Nature of Code, Spring 2009 - -// Boid class -// Methods for Separation, Cohesion, Alignment added - -class Boid { - - PVector loc; - PVector vel; - PVector acc; - float r; - - Boid(float x, float y) { - acc = new PVector(0,0); - vel = new PVector(random(-1,1),random(-1,1)); - loc = new PVector(x,y); - r = 2.0; - } - - void run(ArrayList boids) { - flock(boids); - update(); - borders(); - render(); - } - - void applyForce(PVector force) { - // We could add mass here if we want A = F / M - acc.add(force); - } - - // We accumulate a new acceleration each time based on three rules - void flock(ArrayList boids) { - PVector sep = separate(boids); // Separation - PVector ali = align(boids); // Alignment - PVector coh = cohesion(boids); // Cohesion - // Arbitrarily weight these forces - sep.mult(swt); - ali.mult(awt); - coh.mult(cwt); - // Add the force vectors to acceleration - applyForce(sep); - applyForce(ali); - applyForce(coh); - } - - // Method to update location - void update() { - // Update velocity - vel.add(acc); - // Limit speed - vel.limit(maxspeed); - loc.add(vel); - // Reset accelertion to 0 each cycle - acc.mult(0); - } - - // A method that calculates and applies a steering force towards a target - // STEER = DESIRED MINUS VELOCITY - PVector seek(PVector target) { - PVector desired = PVector.sub(target,loc); // A vector pointing from the location to the target - - // Normalize desired and scale to maximum speed - desired.normalize(); - desired.mult(maxspeed); - // Steering = Desired minus Velocity - PVector steer = PVector.sub(desired,vel); - steer.limit(maxforce); // Limit to maximum steering force - - return steer; - } - - void render() { - // Draw a triangle rotated in the direction of velocity - float theta = vel.heading2D() + radians(90); - fill(175); - stroke(0); - pushMatrix(); - translate(loc.x,loc.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - } - - // Wraparound - void borders() { - if (loc.x < -r) loc.x = width+r; - if (loc.y < -r) loc.y = height+r; - if (loc.x > width+r) loc.x = -r; - if (loc.y > height+r) loc.y = -r; - } - - // Separation - // Method checks for nearby boids and steers away - PVector separate (ArrayList boids) { - float desiredseparation = 25.0; - PVector steer = new PVector(0,0); - int count = 0; - // For every boid in the system, check if it's too close - for (Boid other : boids) { - float d = PVector.dist(loc,other.loc); - // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) - if ((d > 0) && (d < desiredseparation)) { - // Calculate vector pointing away from neighbor - PVector diff = PVector.sub(loc,other.loc); - diff.normalize(); - diff.div(d); // Weight by distance - steer.add(diff); - count++; // Keep track of how many - } - } - // Average -- divide by how many - if (count > 0) { - steer.div((float)count); - // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mult(maxspeed); - steer.sub(vel); - steer.limit(maxforce); - } - return steer; - } - - // Alignment - // For every nearby boid in the system, calculate the average velocity - PVector align (ArrayList boids) { - float neighbordist = 50.0; - PVector steer = new PVector(); - int count = 0; - for (Boid other : boids) { - float d = PVector.dist(loc,other.loc); - if ((d > 0) && (d < neighbordist)) { - steer.add(other.vel); - count++; - } - } - if (count > 0) { - steer.div((float)count); - // Implement Reynolds: Steering = Desired - Velocity - steer.normalize(); - steer.mult(maxspeed); - steer.sub(vel); - steer.limit(maxforce); - } - return steer; - } - - // Cohesion - // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location - PVector cohesion (ArrayList boids) { - float neighbordist = 50.0; - PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations - int count = 0; - for (Boid other : boids) { - float d = PVector.dist(loc,other.loc); - if ((d > 0) && (d < neighbordist)) { - sum.add(other.loc); // Add location - count++; - } - } - if (count > 0) { - sum.div((float)count); - return seek(sum); // Steer towards the location - } - return sum; - } -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/Flock.pde b/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/Flock.pde deleted file mode 100644 index eb7458e5a..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/Flock.pde +++ /dev/null @@ -1,25 +0,0 @@ -// Flocking -// Daniel Shiffman -// The Nature of Code, Spring 2011 - -// Flock class -// Does very little, simply manages the ArrayList of all the boids - -class Flock { - ArrayList boids; // An ArrayList for all the boids - - Flock() { - boids = new ArrayList(); // Initialize the ArrayList - } - - void run() { - for (Boid b : boids) { - b.run(boids); // Passing the entire list of boids to each boid individually - } - } - - void addBoid(Boid b) { - boids.add(b); - } - -} diff --git a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/flocking_sliders.pde b/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/flocking_sliders.pde deleted file mode 100644 index 16314225a..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/flocking_sliders.pde +++ /dev/null @@ -1,60 +0,0 @@ - -// Flocking -// Daniel Shiffman - -// Demonstration of Craig Reynolds' "Flocking" behavior -// See: http://www.red3d.com/cwr/ -// Rules: Cohesion, Separation, Alignment - -// Click mouse to add boids into the system - -import processing.opengl.*; - - -Flock flock; -PVector center; - -boolean showvalues = true; -boolean scrollbar = false; - - -void setup() { - size(1024,768,OPENGL); - setupScrollbars(); - center = new PVector(width/2,height/2); - colorMode(RGB,255,255,255,100); - flock = new Flock(); - // Add an initial set of boids into the system - for (int i = 0; i < 120; i++) { - flock.addBoid(new Boid(width/2,height/2)); - } - smooth(); -} - - -void draw() { - - background(255); - smooth(); - flock.run(); - drawScrollbars(); - - if (mousePressed && !scrollbar) { - flock.addBoid(new Boid(mouseX,mouseY)); - } - - - if (showvalues) { - fill(0); - textAlign(LEFT); - text("Total boids: " + flock.boids.size() + "\n" + "Framerate: " + round(frameRate) + "\nPress any key to show/hide sliders and text\nClick mouse to add more boids",5,100); - } -} - -void keyPressed() { - showvalues = !showvalues; -} - -void mousePressed() { -} - diff --git a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/keyPressed.pde b/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/keyPressed.pde deleted file mode 100644 index b28b04f64..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/keyPressed.pde +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/scrollbar.pde b/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/scrollbar.pde deleted file mode 100644 index ded631f29..000000000 --- a/java/examples/Books/Nature of Code/chp6_agents/flocking_sliders/scrollbar.pde +++ /dev/null @@ -1,133 +0,0 @@ -// Code based on "Scrollbar" by Casey Reas - -HScrollbar[] hs = new HScrollbar[5];// -String[] labels = {"separation", "alignment","cohesion","maxspeed","maxforce"}; - -int x = 5; -int y = 20; -int w = 50; -int h = 8; -int l = 2; -int spacing = 4; - -void setupScrollbars() { - for (int i = 0; i < hs.length; i++) { - hs[i] = new HScrollbar(x, y + i*(h+spacing), w, h, l); - } - - hs[0].setPos(0.5); - hs[1].setPos(0.5); - hs[2].setPos(0.5); - hs[3].setPos(0.5); - hs[4].setPos(0.05); - -} - -void drawScrollbars() { - //if (showvalues) { - swt = hs[0].getPos()*10.0f; //sep.mult(25.0f); - awt = hs[1].getPos()*2.0f; //sep.mult(25.0f); - cwt = hs[2].getPos()*2.0f; //sep.mult(25.0f); - maxspeed = hs[3].getPos()*10.0f; - maxforce = hs[4].getPos()*0.5; - - - if (showvalues) { - for (int i = 0; i < hs.length; i++) { - hs[i].update(); - hs[i].draw(); - fill(0); - textAlign(LEFT); - text(labels[i],x+w+spacing,y+i*(h+spacing)+spacing); - //text(hs[i].getPos(),x+w+spacing+75,y+i*(h+spacing)+spacing); - } - } -} - - -class HScrollbar -{ - int swidth, sheight; // width and height of bar - int xpos, ypos; // x and y position of bar - float spos, newspos; // x position of slider - int sposMin, sposMax; // max and min values of slider - int loose; // how loose/heavy - boolean over; // is the mouse over the slider? - boolean locked; - float ratio; - - HScrollbar (int xp, int yp, int sw, int sh, int l) { - swidth = sw; - sheight = sh; - int widthtoheight = sw - sh; - ratio = (float)sw / (float)widthtoheight; - xpos = xp; - ypos = yp-sheight/2; - spos = xpos; - newspos = spos; - sposMin = xpos; - sposMax = xpos + swidth - sheight; - loose = l; - } - - void update() { - if(over()) { - over = true; - } - else { - over = false; - } - if(mousePressed && over) { - scrollbar = true; - locked = true; - } - if(!mousePressed) { - locked = false; - scrollbar = false; - } - if(locked) { - newspos = constrain(mouseX-sheight/2, sposMin, sposMax); - } - if(abs(newspos - spos) > 0) { - spos = spos + (newspos-spos)/loose; - } - } - - int constrain(int val, int minv, int maxv) { - return min(max(val, minv), maxv); - } - - boolean over() { - if(mouseX > xpos && mouseX < xpos+swidth && - mouseY > ypos && mouseY < ypos+sheight) { - return true; - } - else { - return false; - } - } - - void draw() { - fill(255); - rectMode(CORNER); - rect(xpos, ypos, swidth, sheight); - if(over || locked) { - fill(153, 102, 0); - } - else { - fill(102, 102, 102); - } - rect(spos, ypos, sheight, sheight); - } - - void setPos(float s) { - spos = xpos + s*(sposMax-sposMin); - newspos = spos; - } - - float getPos() { - // convert spos to be values between - // 0 and the total width of the scrollbar - return ((spos-xpos))/(sposMax-sposMin);// * ratio; - } -} diff --git a/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/Cell.pde b/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/Cell.pde deleted file mode 100644 index 3f4a43fb5..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/Cell.pde +++ /dev/null @@ -1,38 +0,0 @@ -class Cell { - - float x, y; - float w; - float xoff; - float yoff; - - int state; - - Cell(float x_, float y_, float w_) { - x = x_; - y = y_; - w = w_; - xoff = w/2; - yoff = sin(radians(60))*w; - state = int(random(2)); - } - - - void display() { - - fill(state*255); - stroke(0); - pushMatrix(); - translate(x,y); - beginShape(); - vertex(0, yoff); - vertex(xoff, 0); - vertex(xoff+w, 0); - vertex(2*w, yoff); - vertex(xoff+w, 2*yoff); - vertex(xoff, 2*yoff); - vertex(0, yoff); - endShape(); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/Ex7_09_HexagonCells.pde b/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/Ex7_09_HexagonCells.pde deleted file mode 100644 index 072897b33..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/Ex7_09_HexagonCells.pde +++ /dev/null @@ -1,24 +0,0 @@ -// Daniel Shiffman, Nature of Code -// - -// Outline for game of life -// This is just a grid of hexagons right now - -GOL gol; - -void setup() { - size(800, 200); - smooth(); - gol = new GOL(); -} - -void draw() { - background(255); - gol.display(); -} - -// reset board when mouse is pressed -void mousePressed() { - gol.init(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/GOL.pde b/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/GOL.pde deleted file mode 100644 index c72710b1d..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Ex7_09_HexagonCells/GOL.pde +++ /dev/null @@ -1,40 +0,0 @@ -class GOL { - - float w = 20; - float h = sin(radians(60))*w; - int columns, rows; - - // Game of life board - Cell[][] board; - - - GOL() { - // Initialize rows, columns and set-up arrays - columns = width/int(w*3); - rows = height/int(h); - board = new Cell[columns][rows]; - init(); - } - - void init() { - float h = sin(radians(60))*w; - for (int i = 0; i < columns; i++) { - for (int j = 0; j < rows; j++) { - if (j % 2 == 0) board[i][j] = new Cell(i*w*3, j*h,w); - else board[i][j] = new Cell(i*w*3+w+h/2, j*h, w); - } - } - } - - - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - for ( int i = 0; i < columns;i++) { - for ( int j = 0; j < rows;j++) { - board[i][j].display(); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_01_WolframCA_randomizedrules/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_01_WolframCA_randomizedrules/CA.pde deleted file mode 100644 index c172ad832..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_01_WolframCA_randomizedrules/CA.pde +++ /dev/null @@ -1,95 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int[] cells; // An array of 0s and 1s - int generation; // How many generations? - - int[] ruleset; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - - int w = 5; - - CA(int[] r) { - ruleset = r; - cells = new int[width/w]; - restart(); - } - - // Make a random ruleset - void randomize() { - for (int i = 0; i < 8; i++) { - ruleset[i] = int(random(2)); - } - } - - // Reset to generation 0 - void restart() { - for (int i = 0; i < cells.length; i++) { - cells[i] = 0; - } - cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - - // The process of creating the new generation - void generate() { - // First we create an empty array for the new values - int[] nextgen = new int[cells.length]; - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 1; i < cells.length-1; i++) { - int left = cells[i-1]; // Left neighbor state - int me = cells[i]; // Current state - int right = cells[i+1]; // Right neighbor state - nextgen[i] = rules(left, me, right); // Compute next generation state based on ruleset - } - // The current generation is the new generation - cells = nextgen; - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - for (int i = 0; i < cells.length; i++) { - if (cells[i] == 1) fill(0); - else fill(255); - noStroke(); - rect(i*w, generation*w, w, w); - } - } - - // Implementing the Wolfram rules - // This is the concise conversion to binary way - /*int rules (int a, int b, int c) { - String s = "" + a + b + c; - int index = Integer.parseInt(s, 2); - return ruleset[index]; - }*/ - // For JavaScript Mode - int rules (int a, int b, int c) { - if (a == 1 && b == 1 && c == 1) return ruleset[0]; - if (a == 1 && b == 1 && c == 0) return ruleset[1]; - if (a == 1 && b == 0 && c == 1) return ruleset[2]; - if (a == 1 && b == 0 && c == 0) return ruleset[3]; - if (a == 0 && b == 1 && c == 1) return ruleset[4]; - if (a == 0 && b == 1 && c == 0) return ruleset[5]; - if (a == 0 && b == 0 && c == 1) return ruleset[6]; - if (a == 0 && b == 0 && c == 0) return ruleset[7]; - return 0; - } - - // The CA is done if it reaches the bottom of the screen - boolean finished() { - if (generation > height/w) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_01_WolframCA_randomizedrules/Exercise_7_01_WolframCA_randomizedrules.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_01_WolframCA_randomizedrules/Exercise_7_01_WolframCA_randomizedrules.pde deleted file mode 100644 index 95ebb0808..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_01_WolframCA_randomizedrules/Exercise_7_01_WolframCA_randomizedrules.pde +++ /dev/null @@ -1,42 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata -// When the system reaches bottom of the window, it restarts with a new ruleset -// Mouse click restarts as well - -CA ca; // An object to describe a Wolfram elementary Cellular Automata - -int delay = 0; - -void setup() { - size(800, 200); - background(255); - int[] ruleset = { - 0, 1, 0, 1, 1, 0, 1, 0 - }; // An initial rule system - ca = new CA(ruleset); // Initialize CA - frameRate(30); -} - -void draw() { - ca.display(); // Draw the CA - ca.generate(); - - if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart - delay++; - if (delay > 30) { - background(255); - ca.randomize(); - ca.restart(); - delay = 0; - } - } -} - -void mousePressed() { - background(255); - ca.randomize(); - ca.restart(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling/CA.pde deleted file mode 100644 index 545d0bb83..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling/CA.pde +++ /dev/null @@ -1,92 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int generation; // How many generations? - int[] ruleset; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - int w = 5; - int[][] matrix; // Store a history of generations in 2D array, not just one - - int cols; - int rows; - - - CA(int[] r) { - ruleset = r; - cols = width/w; - rows = height/w; - matrix = new int[cols][rows]; - restart(); - } - - // Make a random ruleset - void randomize() { - for (int i = 0; i < 8; i++) { - ruleset[i] = int(random(2)); - } - } - - // Reset to generation 0 - void restart() { - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - matrix[i][j] = 0; - } - } - matrix[cols/2][0] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - - // The process of creating the new generation - void generate() { - - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 0; i < cols; i++) { - int left = matrix[(i+cols-1)%cols][generation%rows]; // Left neighbor state - int me = matrix[i][generation%rows]; // Current state - int right = matrix[(i+1)%cols][generation%rows]; // Right neighbor state - matrix[i][(generation+1)%rows] = rules(left, me, right); // Compute next generation state based on ruleset - } - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - int offset = generation%rows; - - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - int y = j - offset; - if (y <= 0) y = rows + y; - if (matrix[i][j] == 1) fill(0); - else fill(255); - noStroke(); - rect(i*w, (y-1)*w, w, w); - } - } - } - - // Implementing the Wolfram rules - // This is the concise conversion to binary way - int rules (int a, int b, int c) { - String s = "" + a + b + c; - int index = Integer.parseInt(s, 2); - return ruleset[index]; - } - - // The CA is done if it reaches the bottom of the screen - boolean finished() { - if (generation > height/w) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling/Exercise_7_04_WolframCA_scrolling.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling/Exercise_7_04_WolframCA_scrolling.pde deleted file mode 100644 index 7c1c0c787..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling/Exercise_7_04_WolframCA_scrolling.pde +++ /dev/null @@ -1,34 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata -// with the system scrolling by -// Also implements wrap around - -CA ca; // An object to describe a Wolfram elementary Cellular Automata - - -void setup() { - size(800, 200); - frameRate(30); - background(255); - int[] ruleset = {0,1,1,1,1,0,1,1}; // Rule 222 - //int[] ruleset = {0,1,1,1,1,1,0,1}; // Rule 190 - //int[] ruleset = {0,1,1,1,1,0,0,0}; // Rule 30 - //int[] ruleset = {0,1,1,1,0,1,1,0}; // Rule 110 - - ca = new CA(ruleset); // Initialize CA -} - -void draw() { - ca.display(); // Draw the CA - ca.generate(); -} - -void mousePressed() { - saveFrame("222-####.png"); - //background(255); - //ca.randomize(); - //ca.restart(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_110/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_110/CA.pde deleted file mode 100644 index fdfd10d50..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_110/CA.pde +++ /dev/null @@ -1,104 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int generation; // How many generations? - int[] ruleset; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - int w = 5; - int[][] matrix; // Store a history of generations in 2D array, not just one - - int cols; - int rows; - - - CA(int[] r) { - ruleset = r; - cols = width/w; - rows = height/w; - matrix = new int[cols][rows]; - restart(); - } - - // Make a random ruleset - void randomize() { - for (int i = 0; i < 8; i++) { - ruleset[i] = int(random(2)); - } - } - - // Reset to generation 0 - void restart() { - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - matrix[i][j] = 0; - } - } - matrix[cols/2][0] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - - // The process of creating the new generation - void generate() { - - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 0; i < cols; i++) { - int left = matrix[(i+cols-1)%cols][generation%rows]; // Left neighbor state - int me = matrix[i][generation%rows]; // Current state - int right = matrix[(i+1)%cols][generation%rows]; // Right neighbor state - matrix[i][(generation+1)%rows] = rules(left, me, right); // Compute next generation state based on ruleset - } - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - int offset = generation%rows; - - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - int y = j - offset; - if (y <= 0) y = rows + y; - if (matrix[i][j] == 1) fill(0); - else fill(255); - noStroke(); - rect(i*w, (y-1)*w, w, w); - } - } - } - - // Implementing the Wolfram rules - // This is the concise conversion to binary way - /*int rules (int a, int b, int c) { - String s = "" + a + b + c; - int index = Integer.parseInt(s, 2); - return ruleset[index]; - }*/ - // For JavaScript Mode - int rules (int a, int b, int c) { - if (a == 1 && b == 1 && c == 1) return ruleset[7]; - if (a == 1 && b == 1 && c == 0) return ruleset[6]; - if (a == 1 && b == 0 && c == 1) return ruleset[5]; - if (a == 1 && b == 0 && c == 0) return ruleset[4]; - if (a == 0 && b == 1 && c == 1) return ruleset[3]; - if (a == 0 && b == 1 && c == 0) return ruleset[2]; - if (a == 0 && b == 0 && c == 1) return ruleset[1]; - if (a == 0 && b == 0 && c == 0) return ruleset[0]; - return 0; - } - - // The CA is done if it reaches the bottom of the screen - boolean finished() { - if (generation > height/w) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_110/Exercise_7_04_WolframCA_scrolling_110.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_110/Exercise_7_04_WolframCA_scrolling_110.pde deleted file mode 100644 index 034c8cc75..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_110/Exercise_7_04_WolframCA_scrolling_110.pde +++ /dev/null @@ -1,26 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata -// with the system scrolling by -// Also implements wrap around - -CA ca; // An object to describe a Wolfram elementary Cellular Automata - - -void setup() { - size(800, 100); - frameRate(30); - background(255); - //int[] ruleset = {0,1,1,1,1,0,1,1}; // Rule 222 - //int[] ruleset = {0,1,1,1,1,1,0,1}; // Rule 190 - //int[] ruleset = {0,1,1,1,1,0,0,0}; // Rule 30 - int[] ruleset = {0,1,1,1,0,1,1,0}; // Rule 110 - - ca = new CA(ruleset); // Initialize CA -} - -void draw() { - ca.display(); // Draw the CA - ca.generate(); -} diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_190/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_190/CA.pde deleted file mode 100644 index fdfd10d50..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_190/CA.pde +++ /dev/null @@ -1,104 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int generation; // How many generations? - int[] ruleset; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - int w = 5; - int[][] matrix; // Store a history of generations in 2D array, not just one - - int cols; - int rows; - - - CA(int[] r) { - ruleset = r; - cols = width/w; - rows = height/w; - matrix = new int[cols][rows]; - restart(); - } - - // Make a random ruleset - void randomize() { - for (int i = 0; i < 8; i++) { - ruleset[i] = int(random(2)); - } - } - - // Reset to generation 0 - void restart() { - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - matrix[i][j] = 0; - } - } - matrix[cols/2][0] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - - // The process of creating the new generation - void generate() { - - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 0; i < cols; i++) { - int left = matrix[(i+cols-1)%cols][generation%rows]; // Left neighbor state - int me = matrix[i][generation%rows]; // Current state - int right = matrix[(i+1)%cols][generation%rows]; // Right neighbor state - matrix[i][(generation+1)%rows] = rules(left, me, right); // Compute next generation state based on ruleset - } - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - int offset = generation%rows; - - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - int y = j - offset; - if (y <= 0) y = rows + y; - if (matrix[i][j] == 1) fill(0); - else fill(255); - noStroke(); - rect(i*w, (y-1)*w, w, w); - } - } - } - - // Implementing the Wolfram rules - // This is the concise conversion to binary way - /*int rules (int a, int b, int c) { - String s = "" + a + b + c; - int index = Integer.parseInt(s, 2); - return ruleset[index]; - }*/ - // For JavaScript Mode - int rules (int a, int b, int c) { - if (a == 1 && b == 1 && c == 1) return ruleset[7]; - if (a == 1 && b == 1 && c == 0) return ruleset[6]; - if (a == 1 && b == 0 && c == 1) return ruleset[5]; - if (a == 1 && b == 0 && c == 0) return ruleset[4]; - if (a == 0 && b == 1 && c == 1) return ruleset[3]; - if (a == 0 && b == 1 && c == 0) return ruleset[2]; - if (a == 0 && b == 0 && c == 1) return ruleset[1]; - if (a == 0 && b == 0 && c == 0) return ruleset[0]; - return 0; - } - - // The CA is done if it reaches the bottom of the screen - boolean finished() { - if (generation > height/w) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_190/Exercise_7_04_WolframCA_scrolling_190.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_190/Exercise_7_04_WolframCA_scrolling_190.pde deleted file mode 100644 index ea30685db..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_190/Exercise_7_04_WolframCA_scrolling_190.pde +++ /dev/null @@ -1,27 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata -// with the system scrolling by -// Also implements wrap around - -CA ca; // An object to describe a Wolfram elementary Cellular Automata - - -void setup() { - size(800, 100); - frameRate(30); - background(255); - //int[] ruleset = {0,1,1,1,1,0,1,1}; // Rule 222 - int[] ruleset = {0,1,1,1,1,1,0,1}; // Rule 190 - //int[] ruleset = {0,1,1,1,1,0,0,0}; // Rule 30 - //int[] ruleset = {0,1,1,1,0,1,1,0}; // Rule 110 - - ca = new CA(ruleset); // Initialize CA -} - -void draw() { - ca.display(); // Draw the CA - ca.generate(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_222/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_222/CA.pde deleted file mode 100644 index fdfd10d50..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_222/CA.pde +++ /dev/null @@ -1,104 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int generation; // How many generations? - int[] ruleset; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - int w = 5; - int[][] matrix; // Store a history of generations in 2D array, not just one - - int cols; - int rows; - - - CA(int[] r) { - ruleset = r; - cols = width/w; - rows = height/w; - matrix = new int[cols][rows]; - restart(); - } - - // Make a random ruleset - void randomize() { - for (int i = 0; i < 8; i++) { - ruleset[i] = int(random(2)); - } - } - - // Reset to generation 0 - void restart() { - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - matrix[i][j] = 0; - } - } - matrix[cols/2][0] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - - // The process of creating the new generation - void generate() { - - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 0; i < cols; i++) { - int left = matrix[(i+cols-1)%cols][generation%rows]; // Left neighbor state - int me = matrix[i][generation%rows]; // Current state - int right = matrix[(i+1)%cols][generation%rows]; // Right neighbor state - matrix[i][(generation+1)%rows] = rules(left, me, right); // Compute next generation state based on ruleset - } - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - int offset = generation%rows; - - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - int y = j - offset; - if (y <= 0) y = rows + y; - if (matrix[i][j] == 1) fill(0); - else fill(255); - noStroke(); - rect(i*w, (y-1)*w, w, w); - } - } - } - - // Implementing the Wolfram rules - // This is the concise conversion to binary way - /*int rules (int a, int b, int c) { - String s = "" + a + b + c; - int index = Integer.parseInt(s, 2); - return ruleset[index]; - }*/ - // For JavaScript Mode - int rules (int a, int b, int c) { - if (a == 1 && b == 1 && c == 1) return ruleset[7]; - if (a == 1 && b == 1 && c == 0) return ruleset[6]; - if (a == 1 && b == 0 && c == 1) return ruleset[5]; - if (a == 1 && b == 0 && c == 0) return ruleset[4]; - if (a == 0 && b == 1 && c == 1) return ruleset[3]; - if (a == 0 && b == 1 && c == 0) return ruleset[2]; - if (a == 0 && b == 0 && c == 1) return ruleset[1]; - if (a == 0 && b == 0 && c == 0) return ruleset[0]; - return 0; - } - - // The CA is done if it reaches the bottom of the screen - boolean finished() { - if (generation > height/w) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_222/Exercise_7_04_WolframCA_scrolling_222.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_222/Exercise_7_04_WolframCA_scrolling_222.pde deleted file mode 100644 index 69af02447..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_222/Exercise_7_04_WolframCA_scrolling_222.pde +++ /dev/null @@ -1,27 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata -// with the system scrolling by -// Also implements wrap around - -CA ca; // An object to describe a Wolfram elementary Cellular Automata - - -void setup() { - size(800, 100); - frameRate(30); - background(255); - int[] ruleset = {0,1,1,1,1,0,1,1}; // Rule 222 - //int[] ruleset = {0,1,1,1,1,1,0,1}; // Rule 190 - //int[] ruleset = {0,1,1,1,1,0,0,0}; // Rule 30 - //int[] ruleset = {0,1,1,1,0,1,1,0}; // Rule 110 - - ca = new CA(ruleset); // Initialize CA -} - -void draw() { - ca.display(); // Draw the CA - ca.generate(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_30/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_30/CA.pde deleted file mode 100644 index 0999f18b1..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_30/CA.pde +++ /dev/null @@ -1,103 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int generation; // How many generations? - int[] ruleset; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - int w = 5; - int[][] matrix; // Store a history of generations in 2D array, not just one - - int cols; - int rows; - - - CA(int[] r) { - ruleset = r; - cols = width/w; - rows = height/w; - matrix = new int[cols][rows]; - restart(); - } - - // Make a random ruleset - void randomize() { - for (int i = 0; i < 8; i++) { - ruleset[i] = int(random(2)); - } - } - - // Reset to generation 0 - void restart() { - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - matrix[i][j] = 0; - } - } - matrix[cols/2][0] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - - // The process of creating the new generation - void generate() { - - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 0; i < cols; i++) { - int left = matrix[(i+cols-1)%cols][generation%rows]; // Left neighbor state - int me = matrix[i][generation%rows]; // Current state - int right = matrix[(i+1)%cols][generation%rows]; // Right neighbor state - matrix[i][(generation+1)%rows] = rules(left, me, right); // Compute next generation state based on ruleset - } - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - int offset = generation%rows; - - for (int i = 0; i < cols; i++) { - for (int j = 0; j < rows; j++) { - int y = j - offset; - if (y <= 0) y = rows + y; - if (matrix[i][j] == 1) fill(0); - else fill(255); - noStroke(); - rect(i*w, (y-1)*w, w, w); - } - } - } - // Implementing the Wolfram rules - // This is the concise conversion to binary way - /*int rules (int a, int b, int c) { - String s = "" + a + b + c; - int index = Integer.parseInt(s, 2); - return ruleset[index]; - }*/ - // For JavaScript Mode - int rules (int a, int b, int c) { - if (a == 1 && b == 1 && c == 1) return ruleset[7]; - if (a == 1 && b == 1 && c == 0) return ruleset[6]; - if (a == 1 && b == 0 && c == 1) return ruleset[5]; - if (a == 1 && b == 0 && c == 0) return ruleset[4]; - if (a == 0 && b == 1 && c == 1) return ruleset[3]; - if (a == 0 && b == 1 && c == 0) return ruleset[2]; - if (a == 0 && b == 0 && c == 1) return ruleset[1]; - if (a == 0 && b == 0 && c == 0) return ruleset[0]; - return 0; - } - - // The CA is done if it reaches the bottom of the screen - boolean finished() { - if (generation > height/w) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_30/Exercise_7_04_WolframCA_scrolling_30.pde b/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_30/Exercise_7_04_WolframCA_scrolling_30.pde deleted file mode 100644 index 044451336..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Exercise_7_04_WolframCA_scrolling_30/Exercise_7_04_WolframCA_scrolling_30.pde +++ /dev/null @@ -1,27 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata -// with the system scrolling by -// Also implements wrap around - -CA ca; // An object to describe a Wolfram elementary Cellular Automata - - -void setup() { - size(800, 100); - frameRate(30); - background(255); - //int[] ruleset = {0,1,1,1,1,0,1,1}; // Rule 222 - //int[] ruleset = {0,1,1,1,1,1,0,1}; // Rule 190 - int[] ruleset = {0,1,1,1,1,0,0,0}; // Rule 30 - //int[] ruleset = {0,1,1,1,0,1,1,0}; // Rule 110 - - ca = new CA(ruleset); // Initialize CA -} - -void draw() { - ca.display(); // Draw the CA - ca.generate(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Figure_7_17_cells/Figure_7_17_cells.pde b/java/examples/Books/Nature of Code/chp7_CA/Figure_7_17_cells/Figure_7_17_cells.pde deleted file mode 100644 index 81ec31d42..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/Figure_7_17_cells/Figure_7_17_cells.pde +++ /dev/null @@ -1,28 +0,0 @@ - -size(1800,90); - -int w = 90; - -int total = width/w; - -int[] cells = {1,0,1,0,0,0,0,1,0,1,1,1,0,0,0,1,1,1,0,0}; - - -print("int[] cells = {"); -for (int i = 0; i < cells.length; i++) { - if (cells[i] == 0) fill(255); - else fill(64); - stroke(0); - rect(i*w,0,w-1,w-1); - print(cells[i] +","); -} - -saveFrame("cells.png"); - - - - - - - - diff --git a/java/examples/Books/Nature of Code/chp7_CA/Figure_7_17_cells/cells.tif b/java/examples/Books/Nature of Code/chp7_CA/Figure_7_17_cells/cells.tif deleted file mode 100644 index 38ef13501..000000000 Binary files a/java/examples/Books/Nature of Code/chp7_CA/Figure_7_17_cells/cells.tif and /dev/null differ diff --git a/java/examples/Books/Nature of Code/chp7_CA/GameOfLifeWrapAround/GOL.pde b/java/examples/Books/Nature of Code/chp7_CA/GameOfLifeWrapAround/GOL.pde deleted file mode 100644 index 827cf9843..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/GameOfLifeWrapAround/GOL.pde +++ /dev/null @@ -1,73 +0,0 @@ -class GOL { - - int w = 8; - int columns, rows; - - // Game of life board - int[][] board; - - - GOL() { - // Initialize rows, columns and set-up arrays - columns = width/w; - rows = height/w; - board = new int[columns][rows]; - //next = new int[columns][rows]; - // Call function to fill array with random values 0 or 1 - init(); - } - - void init() { - for (int i =1;i < columns-1;i++) { - for (int j =1;j < rows-1;j++) { - board[i][j] = int(random(2)); - } - } - } - - // The process of creating the new generation - void generate() { - - int[][] next = new int[columns][rows]; - - // Loop through every spot in our 2D array and check spots neighbors - for (int x = 0; x < columns; x++) { - for (int y = 0; y < rows; y++) { - - // Add up all the states in a 3x3 surrounding grid - int neighbors = 0; - for (int i = -1; i <= 1; i++) { - for (int j = -1; j <= 1; j++) { - neighbors += board[(x+i+columns)%columns][(y+j+rows)%rows]; - } - } - - // A little trick to subtract the current cell's state since - // we added it in the above loop - neighbors -= board[x][y]; - - // Rules of Life - if ((board[x][y] == 1) && (neighbors < 2)) next[x][y] = 0; // Loneliness - else if ((board[x][y] == 1) && (neighbors > 3)) next[x][y] = 0; // Overpopulation - else if ((board[x][y] == 0) && (neighbors == 3)) next[x][y] = 1; // Reproduction - else next[x][y] = board[x][y]; // Stasis - } - } - - // Next is now our board - board = next; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - for ( int i = 0; i < columns;i++) { - for ( int j = 0; j < rows;j++) { - if ((board[i][j] == 1)) fill(0); - else fill(255); - stroke(0); - rect(i*w, j*w, w, w); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/GameOfLifeWrapAround/GameOfLifeWrapAround.pde b/java/examples/Books/Nature of Code/chp7_CA/GameOfLifeWrapAround/GameOfLifeWrapAround.pde deleted file mode 100644 index 97d409a1c..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/GameOfLifeWrapAround/GameOfLifeWrapAround.pde +++ /dev/null @@ -1,31 +0,0 @@ -// Daniel Shiffman, Nature of Code -// - -// A basic implementation of John Conway's Game of Life CA -// how could this be improved to use object oriented programming? -// think of it as similar to our particle system, with a "cell" class -// to describe each individual cell and a "cellular automata" class -// to describe a collection of cells - -// Cells wrap around - -GOL gol; - -void setup() { - size(400, 400); - smooth(); - gol = new GOL(); -} - -void draw() { - background(255); - - gol.generate(); - gol.display(); -} - -// reset board when mouse is pressed -void mousePressed() { - gol.init(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/Cell.pde b/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/Cell.pde deleted file mode 100644 index 3f4a43fb5..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/Cell.pde +++ /dev/null @@ -1,38 +0,0 @@ -class Cell { - - float x, y; - float w; - float xoff; - float yoff; - - int state; - - Cell(float x_, float y_, float w_) { - x = x_; - y = y_; - w = w_; - xoff = w/2; - yoff = sin(radians(60))*w; - state = int(random(2)); - } - - - void display() { - - fill(state*255); - stroke(0); - pushMatrix(); - translate(x,y); - beginShape(); - vertex(0, yoff); - vertex(xoff, 0); - vertex(xoff+w, 0); - vertex(2*w, yoff); - vertex(xoff+w, 2*yoff); - vertex(xoff, 2*yoff); - vertex(0, yoff); - endShape(); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/GOL.pde b/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/GOL.pde deleted file mode 100644 index c72710b1d..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/GOL.pde +++ /dev/null @@ -1,40 +0,0 @@ -class GOL { - - float w = 20; - float h = sin(radians(60))*w; - int columns, rows; - - // Game of life board - Cell[][] board; - - - GOL() { - // Initialize rows, columns and set-up arrays - columns = width/int(w*3); - rows = height/int(h); - board = new Cell[columns][rows]; - init(); - } - - void init() { - float h = sin(radians(60))*w; - for (int i = 0; i < columns; i++) { - for (int j = 0; j < rows; j++) { - if (j % 2 == 0) board[i][j] = new Cell(i*w*3, j*h,w); - else board[i][j] = new Cell(i*w*3+w+h/2, j*h, w); - } - } - } - - - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - for ( int i = 0; i < columns;i++) { - for ( int j = 0; j < rows;j++) { - board[i][j].display(); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/HexagonCells.pde b/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/HexagonCells.pde deleted file mode 100644 index 2a42cdc5b..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/HexagonCells/HexagonCells.pde +++ /dev/null @@ -1,24 +0,0 @@ -// Daniel Shiffman, Nature of Code -// - -// Outline for game of life -// This is just a grid of hexagons right now - -GOL gol; - -void setup() { - size(600, 600); - smooth(); - gol = new GOL(); -} - -void draw() { - background(255); - gol.display(); -} - -// reset board when mouse is pressed -void mousePressed() { - gol.init(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_figures/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_figures/CA.pde deleted file mode 100644 index adb77ee3e..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_figures/CA.pde +++ /dev/null @@ -1,92 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int[] cells; // An array of 0s and 1s - int generation; // How many generations? - - int[] ruleset; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - - CA(int[] r) { - ruleset = r; - cells = new int[width/scl]; - restart(); - } - - CA() { - scl = 1; - cells = new int[width/scl]; - randomize(); - restart(); - } - - // Set the rules of the CA - void setRules(int[] r) { - ruleset = r; - } - - // Make a random ruleset - void randomize() { - for (int i = 0; i < 8; i++) { - ruleset[i] = int(random(2)); - } - } - - // Reset to generation 0 - void restart() { - for (int i = 0; i < cells.length; i++) { - cells[i] = 0; - } - cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - // The process of creating the new generation - void generate() { - // First we create an empty array for the new values - int[] nextgen = new int[cells.length]; - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 1; i < cells.length-1; i++) { - int left = cells[i-1]; // Left neighbor state - int me = cells[i]; // Current state - int right = cells[i+1]; // Right neighbor state - nextgen[i] = rules(left, me, right); // Compute next generation state based on ruleset - } - // The current generation is the new generation - cells = nextgen; - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void render() { - for (int i = 0; i < cells.length; i++) { - if (cells[i] == 1) fill(0); - else fill(255); - stroke(0); - rect(i*scl, generation*scl, scl, scl); - } - } - - // Implementing the Wolfram rules - // Could be improved and made more concise, but here we can explicitly see what is going on for each case - int rules (int a, int b, int c) { - String s = "" + a + b + c; - int index = Integer.parseInt(s,2); - return ruleset[index]; - } - - // The CA is done if it reaches the bottom of the screen - boolean finished() { - if (generation > height/scl) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_figures/NOC_7_01_WolframCA_figures.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_figures/NOC_7_01_WolframCA_figures.pde deleted file mode 100644 index a824998f2..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_figures/NOC_7_01_WolframCA_figures.pde +++ /dev/null @@ -1,35 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata -// When the system reaches bottom of the window, it restarts with a new ruleset -// Mouse click restarts as well - - -CA ca; // An instance object to describe the Wolfram basic Cellular Automata - -int scl = 20; - -void setup() { - size(1800,600); - background(255); - //int[] ruleset = {0,1,0,1,1,0,1,0}; // 90 - int[] ruleset = {0,1,1,1,1,0,1,1}; // An initial rule system - ca = new CA(ruleset); // Initialize CA -} - -void draw() { - ca.render(); // Draw the CA - ca.generate(); // Generate the next level - - if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart - saveFrame("rule222.png"); - noLoop(); - } -} - -void mousePressed() { - background(255); - ca.randomize(); - ca.restart(); -} diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_simple/CA.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_simple/CA.pde deleted file mode 100644 index 654b06645..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_simple/CA.pde +++ /dev/null @@ -1,65 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// A class to manage the CA - -class CA { - - int[] cells; // An array of 0s and 1s - int generation; // How many generations? - - int[] ruleset = {0, 1, 0, 1, 1, 0, 1, 0}; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1} - - int w = 10; - - CA() { - cells = new int[width/w]; - for (int i = 0; i < cells.length; i++) { - cells[i] = 0; - } - cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1" - generation = 0; - } - - // The process of creating the new generation - void generate() { - // First we create an empty array for the new values - int[] nextgen = new int[cells.length]; - // For every spot, determine new state by examing current state, and neighbor states - // Ignore edges that only have one neighor - for (int i = 1; i < cells.length-1; i++) { - int left = cells[i-1]; // Left neighbor state - int me = cells[i]; // Current state - int right = cells[i+1]; // Right neighbor state - nextgen[i] = rules(left, me, right); // Compute next generation state based on ruleset - } - // The current generation is the new generation - cells = nextgen; - generation++; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - for (int i = 0; i < cells.length; i++) { - if (cells[i] == 1) fill(0); - else fill(255); - noStroke(); - rect(i*w, generation*w, w, w); - } - } - - // Implementing the Wolfram rules - // Could be improved and made more concise, but here we can explicitly see what is going on for each case - int rules (int a, int b, int c) { - if (a == 1 && b == 1 && c == 1) return ruleset[0]; - if (a == 1 && b == 1 && c == 0) return ruleset[1]; - if (a == 1 && b == 0 && c == 1) return ruleset[2]; - if (a == 1 && b == 0 && c == 0) return ruleset[3]; - if (a == 0 && b == 1 && c == 1) return ruleset[4]; - if (a == 0 && b == 1 && c == 0) return ruleset[5]; - if (a == 0 && b == 0 && c == 1) return ruleset[6]; - if (a == 0 && b == 0 && c == 0) return ruleset[7]; - return 0; - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_simple/NOC_7_01_WolframCA_simple.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_simple/NOC_7_01_WolframCA_simple.pde deleted file mode 100644 index 61020ce67..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_01_WolframCA_simple/NOC_7_01_WolframCA_simple.pde +++ /dev/null @@ -1,21 +0,0 @@ -// Wolfram Cellular Automata -// Daniel Shiffman - -// Simple demonstration of a Wolfram 1-dimensional cellular automata - -CA ca; // An instance object to describe the Wolfram basic Cellular Automata - - -void setup() { - size(800, 400); - background(255); - ca = new CA(); // Initialize CA -} - -void draw() { - ca.display(); // Draw the CA - if (ca.generation < height/ca.w) { - ca.generate(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/Cell.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/Cell.pde deleted file mode 100644 index 86ba100b6..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/Cell.pde +++ /dev/null @@ -1,36 +0,0 @@ -class Cell { - - float x, y; - float w; - - int state; - int previous; - - Cell(float x_, float y_, float w_) { - x = x_; - y = y_; - w = w_; - - state = int(random(2)); - previous = state; - } - - void savePrevious() { - previous = state; - } - - void newState(int s) { - state = s; - } - - void display() { - if (previous == 0 && state == 1) fill(0,0,255); - else if (state == 1) fill(0); - else if (previous == 1 && state == 0) fill(255,0,0); - else fill(255); - stroke(0); - rect(x, y, w, w); - } -} - - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/GOL.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/GOL.pde deleted file mode 100644 index f7c13d486..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/GOL.pde +++ /dev/null @@ -1,69 +0,0 @@ -class GOL { - - int w = 8; - int columns, rows; - - // Game of life board - Cell[][] board; - - - GOL() { - // Initialize rows, columns and set-up arrays - columns = width/w; - rows = height/w; - board = new Cell[columns][rows]; - init(); - } - - void init() { - for (int i = 0; i < columns; i++) { - for (int j = 0; j < rows; j++) { - board[i][j] = new Cell(i*w, j*w, w); - } - } - } - - // The process of creating the new generation - void generate() { - for ( int i = 0; i < columns;i++) { - for ( int j = 0; j < rows;j++) { - board[i][j].savePrevious(); - } - } - - - // Loop through every spot in our 2D array and check spots neighbors - for (int x = 0; x < columns; x++) { - for (int y = 0; y < rows; y++) { - - // Add up all the states in a 3x3 surrounding grid - int neighbors = 0; - for (int i = -1; i <= 1; i++) { - for (int j = -1; j <= 1; j++) { - neighbors += board[(x+i+columns)%columns][(y+j+rows)%rows].previous; - } - } - - // A little trick to subtract the current cell's state since - // we added it in the above loop - neighbors -= board[x][y].previous; - - // Rules of Life - if ((board[x][y].state == 1) && (neighbors < 2)) board[x][y].newState(0); // Loneliness - else if ((board[x][y].state == 1) && (neighbors > 3)) board[x][y].newState(0); // Overpopulation - else if ((board[x][y].state == 0) && (neighbors == 3)) board[x][y].newState(1); // Reproduction - // else do nothing! - } - } - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - for ( int i = 0; i < columns;i++) { - for ( int j = 0; j < rows;j++) { - board[i][j].display(); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/NOC_7_02_GameOfLifeOOP.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/NOC_7_02_GameOfLifeOOP.pde deleted file mode 100644 index e10b32ef6..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_02_GameOfLifeOOP/NOC_7_02_GameOfLifeOOP.pde +++ /dev/null @@ -1,27 +0,0 @@ -// Daniel Shiffman, Nature of Code -// - -// A basic implementation of John Conway's Game of Life CA - -// Each cell is now an object! - -GOL gol; - -void setup() { - size(400, 400); - smooth(); - gol = new GOL(); -} - -void draw() { - background(255); - - gol.generate(); - gol.display(); -} - -// reset board when mouse is pressed -void mousePressed() { - gol.init(); -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_03_GameOfLifeSimple/GOL.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_03_GameOfLifeSimple/GOL.pde deleted file mode 100644 index 981919ae4..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_03_GameOfLifeSimple/GOL.pde +++ /dev/null @@ -1,73 +0,0 @@ -class GOL { - - int w = 8; - int columns, rows; - - // Game of life board - int[][] board; - - - GOL() { - // Initialize rows, columns and set-up arrays - columns = width/w; - rows = height/w; - board = new int[columns][rows]; - //next = new int[columns][rows]; - // Call function to fill array with random values 0 or 1 - init(); - } - - void init() { - for (int i =1;i < columns-1;i++) { - for (int j =1;j < rows-1;j++) { - board[i][j] = int(random(2)); - } - } - } - - // The process of creating the new generation - void generate() { - - int[][] next = new int[columns][rows]; - - // Loop through every spot in our 2D array and check spots neighbors - for (int x = 1; x < columns-1; x++) { - for (int y = 1; y < rows-1; y++) { - - // Add up all the states in a 3x3 surrounding grid - int neighbors = 0; - for (int i = -1; i <= 1; i++) { - for (int j = -1; j <= 1; j++) { - neighbors += board[x+i][y+j]; - } - } - - // A little trick to subtract the current cell's state since - // we added it in the above loop - neighbors -= board[x][y]; - - // Rules of Life - if ((board[x][y] == 1) && (neighbors < 2)) next[x][y] = 0; // Loneliness - else if ((board[x][y] == 1) && (neighbors > 3)) next[x][y] = 0; // Overpopulation - else if ((board[x][y] == 0) && (neighbors == 3)) next[x][y] = 1; // Reproduction - else next[x][y] = board[x][y]; // Stasis - } - } - - // Next is now our board - board = next; - } - - // This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0' - void display() { - for ( int i = 0; i < columns;i++) { - for ( int j = 0; j < rows;j++) { - if ((board[i][j] == 1)) fill(0); - else fill(255); - stroke(0); - rect(i*w, j*w, w, w); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_03_GameOfLifeSimple/NOC_7_03_GameOfLifeSimple.pde b/java/examples/Books/Nature of Code/chp7_CA/NOC_7_03_GameOfLifeSimple/NOC_7_03_GameOfLifeSimple.pde deleted file mode 100644 index 3ac6009ba..000000000 --- a/java/examples/Books/Nature of Code/chp7_CA/NOC_7_03_GameOfLifeSimple/NOC_7_03_GameOfLifeSimple.pde +++ /dev/null @@ -1,29 +0,0 @@ -// Daniel Shiffman, Nature of Code -// - -// A basic implementation of John Conway's Game of Life CA -// how could this be improved to use object oriented programming? -// think of it as similar to our particle system, with a "cell" class -// to describe each individual cell and a "cellular automata" class -// to describe a collection of cells - -GOL gol; - -void setup() { - size(400, 400); - smooth(); - gol = new GOL(); -} - -void draw() { - background(255); - - gol.generate(); - gol.display(); -} - -// reset board when mouse is pressed -void mousePressed() { - gol.init(); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/CantorSetArrayList/CantorSetArrayList.pde b/java/examples/Books/Nature of Code/chp8_fractals/CantorSetArrayList/CantorSetArrayList.pde deleted file mode 100644 index 48b2832de..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/CantorSetArrayList/CantorSetArrayList.pde +++ /dev/null @@ -1,47 +0,0 @@ -// Cantor Set -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple fractal, the Cantor Set -// Uses an ArrayList to store list of objects -// Generates when mouse is pressed - -float h = 30; - -// List of line objects -ArrayList cantor; - -void setup() { - size(729, 200); - - // Start with one line - cantor = new ArrayList(); - cantor.add(new CantorLine(0, 100, width)); -} - -// Click the mouse to advance the sequence -void mousePressed() { - generate(); -} - -void draw() { - background(255); - // Always show all the lines - for (CantorLine cl : cantor) { - cl.display(); - } - - fill(0); - text("Click mouse to generate",10,height-20); -} - -void generate() { - // Generate the next set of lines - ArrayList next = new ArrayList(); - for (CantorLine cl : cantor) { - next.add(new CantorLine(cl.x,cl.y,cl.len/3)); - next.add(new CantorLine(cl.x+cl.len*2/3,cl.y,cl.len/3)); - } - cantor = next; -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/CantorSetArrayList/Line.pde b/java/examples/Books/Nature of Code/chp8_fractals/CantorSetArrayList/Line.pde deleted file mode 100644 index 7e5c9dffa..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/CantorSetArrayList/Line.pde +++ /dev/null @@ -1,23 +0,0 @@ -// Cantor Set -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Cantor line is a simple horizontal line with a starting point -// and length - -class CantorLine { - float x,y; - float len; - - CantorLine(float x_, float y_, float len_) { - x = x_; - y = y_; - len = len_; - } - - void display() { - stroke(0); - line(x,y,x+len,y); - } - -} diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_01_RecursionLines/Exercise_8_01_RecursionLines.pde b/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_01_RecursionLines/Exercise_8_01_RecursionLines.pde deleted file mode 100644 index 0c5f2790f..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_01_RecursionLines/Exercise_8_01_RecursionLines.pde +++ /dev/null @@ -1,35 +0,0 @@ -// Simple Recursion -// Daniel Shiffman -// Nature of Code, Chapter 8 - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - drawLines(100,100,700,100); - noLoop(); -} - -void drawLines(float x1, float y1, float x2, float y2) { - - line(x1,y1,x2,y2); - - float dx = x2-x1; - float dy = y2-y1; - - //println(dx + " " + dy); - - if (dx == 0 && dy > 4) { - //println(dy); - drawLines(x1-dy/3,y1,x1+dy/3,y1); - drawLines(x1-dy/3,y2,x1+dy/3,y2); - } else if (dy == 0 && dx > 4) { - //println(dx); - drawLines(x1,y1-dx/3,x1,y1+dx/3); - drawLines(x2,y1-dx/3,x2,y1+dx/3); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_02_KochSnowFlake/Exercise_8_02_KochSnowFlake.pde b/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_02_KochSnowFlake/Exercise_8_02_KochSnowFlake.pde deleted file mode 100644 index 8ab63f9c7..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_02_KochSnowFlake/Exercise_8_02_KochSnowFlake.pde +++ /dev/null @@ -1,54 +0,0 @@ -// Koch Snowflake -// Daniel Shiffman - -// Renders a simple fractal, the Koch snowflake -// Each recursive level drawn in sequence - -ArrayList lines ; // A list to keep track of all the lines - -void setup() { - size(600, 692); - background(255); - lines = new ArrayList(); - PVector a = new PVector(0, 173); - PVector b = new PVector(width, 173); - PVector c = new PVector(width/2, 173+width*cos(radians(30))); - - // Starting with additional lines - lines.add(new KochLine(a, b)); - lines.add(new KochLine(b, c)); - lines.add(new KochLine(c, a)); - - for (int i = 0; i < 5; i++) { - generate(); - } - - smooth(); -} - -void draw() { - background(255); - for (KochLine l : lines) { - l.display(); - } -} - -void generate() { - ArrayList next = new ArrayList(); // Create emtpy list - for (KochLine l : lines) { - // Calculate 5 koch PVectors (done for us by the line object) - PVector a = l.kochA(); - PVector b = l.kochB(); - PVector c = l.kochC(); - PVector d = l.kochD(); - PVector e = l.kochE(); - // Make line segments between all the PVectors and add them - next.add(new KochLine(a, b)); - next.add(new KochLine(b, c)); - next.add(new KochLine(c, d)); - next.add(new KochLine(d, e)); - } - lines = next; -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_02_KochSnowFlake/KochLine.pde b/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_02_KochSnowFlake/KochLine.pde deleted file mode 100644 index a241afe1c..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_02_KochSnowFlake/KochLine.pde +++ /dev/null @@ -1,71 +0,0 @@ -// Koch Curve -// Daniel Shiffman - -// A class to describe one line segment in the fractal -// Includes methods to calculate midPVectors along the line according to the Koch algorithm - -class KochLine { - - // Two PVectors, - // a is the "left" PVector and - // b is the "right PVector - PVector start; - PVector end; - - KochLine(PVector a, PVector b) { - start = a.get(); - end = b.get(); - } - - void display() { - stroke(0); - line(start.x, start.y, end.x, end.y); - } - - PVector kochA() { - return start.get(); - } - - - // This is easy, just 1/3 of the way - PVector kochB() { - PVector v = PVector.sub(end, start); - v.div(3); - v.add(start); - return v; - } - - // More complicated, have to use a little trig to figure out where this PVector is! - PVector kochC() { - PVector a = start.get(); // Start at the beginning - - PVector v = PVector.sub(end, start); - v.div(3); - a.add(v); // Move to point B - - rotate(v, -radians(60)); // Rotate 60 degrees - a.add(v); // Move to point C - - return a; - } - - // Easy, just 2/3 of the way - PVector kochD() { - PVector v = PVector.sub(end, start); - v.mult(2/3.0); - v.add(start); - return v; - } - - PVector kochE() { - return end.get(); - } -} - -public void rotate(PVector v, float theta) { - float xTemp = v.x; - // Might need to check for rounding errors like with angleBetween function? - v.x = v.x*PApplet.cos(theta) - v.y*PApplet.sin(theta); - v.y = xTemp*PApplet.sin(theta) + v.y*PApplet.cos(theta); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_06_Tree/Exercise_8_06_Tree.pde b/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_06_Tree/Exercise_8_06_Tree.pde deleted file mode 100644 index 456441db3..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_06_Tree/Exercise_8_06_Tree.pde +++ /dev/null @@ -1,56 +0,0 @@ -// Recursive Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Branching angle calculated as a function of horizontal mouse location - -float theta; - -void setup() { - size(1800, 500); - smooth(); -} - -void draw() { - background(255); - // Let's pick an angle 0 to 90 degrees based on the mouse position - theta = PI/6;//map(mouseX,0,width,0,PI/2); - - // Start the tree from the bottom of the screen - translate(width/2, height); - stroke(0); - branch(200,0); - save("chapter08_exc06.png"); - noLoop(); -} - -void branch(float len, int level) { - // Each branch will be 2/3rds the size of the previous one - - //float sw = map(len,2,120,1,10); - //strokeWeight(sw); - strokeWeight(2); - - line(0, 0, 0, -len); - // Move to the end of that line - translate(0, -len); - - len *= 0.66; - level++; - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (level < 5) { - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(len,level); // Ok, now call myself to draw two new branches!! - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - - // Repeat the same thing, only branch off to the "left" this time! - pushMatrix(); - rotate(-theta); - branch(len,level); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_07_Tree/Exercise_8_07_Tree.pde b/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_07_Tree/Exercise_8_07_Tree.pde deleted file mode 100644 index 759570bf9..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Exercise_8_07_Tree/Exercise_8_07_Tree.pde +++ /dev/null @@ -1,51 +0,0 @@ -// Recursive Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Branching angle calculated as a function of horizontal mouse location - -float theta; - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - // Let's pick an angle 0 to 90 degrees based on the mouse position - theta = map(mouseX,0,width,0,PI/2); - - // Start the tree from the bottom of the screen - translate(width/2, height); - stroke(0); - branch(60); -} - -void branch(float len) { - // Each branch will be 2/3rds the size of the previous one - float sw = map(len,2,120,1,10); - strokeWeight(sw); - - line(0, 0, 0, -len); - // Move to the end of that line - translate(0, -len); - - len *= 0.66; - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (len > 2) { - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(len); // Ok, now call myself to draw two new branches!! - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - - // Repeat the same thing, only branch off to the "left" this time! - pushMatrix(); - rotate(-theta); - branch(len); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_02_Mandelbrot/Figure_8_02_Mandelbrot.pde b/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_02_Mandelbrot/Figure_8_02_Mandelbrot.pde deleted file mode 100644 index 5de2d26a6..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_02_Mandelbrot/Figure_8_02_Mandelbrot.pde +++ /dev/null @@ -1,79 +0,0 @@ -// The Mandelbrot Set -// Daniel Shiffman - -// Simple rendering of the Mandelbrot set -// c = a + bi -// Iterate z = z^2 + c, i.e. -// z(0) = 0 -// z(1) = 0*0 + c -// z(2) = c*c + c -// z(3) = (c*c + c) * (c*c + c) + c -// etc. - -// c*c = (a+bi) * (a+bi) = a^2 - b^2 + 2abi - -// Created 2 May 2005 - -// Establish a range of values on the complex plane -double xmin = -2.5; double ymin = -1; double w = 4; double h = 2; -// A different range will allow us to "zoom" in or out on the fractal -// double xmin = -1.5; double ymin = -.1; double wh = 0.15; - -void setup() { - size(863,863/2); -} - -void draw() { - - loadPixels(); - - // Maximum number of iterations for each point on the complex plane - int maxiterations = 200; - - // x goes from xmin to xmax - double xmax = xmin + w; - // y goes from ymin to ymax - double ymax = ymin + h; - - // Calculate amount we increment x,y for each pixel - double dx = (xmax - xmin) / (width); - double dy = (ymax - ymin) / (height); - - // Start y - double y = ymin; - for(int j = 0; j < height; j++) { - // Start x - double x = xmin; - for(int i = 0; i < width; i++) { - - // Now we test, as we iterate z = z^2 + cm does z tend towards infinity? - double a = x; - double b = y; - int n = 0; - while (n < maxiterations) { - double aa = a * a; - double bb = b * b; - double twoab = 2.0 * a * b; - a = aa - bb + x; - b = twoab + y; - // Infinty in our finite world is simple, let's just consider it 16 - if(aa + bb > 16.0f) { - break; // Bail - } - n++; - } - - // We color each pixel based on how long it takes to get to infinity - // If we never got there, let's pick the color black - if (n == maxiterations) pixels[i+j*width] = color(0); - else pixels[i+j*width] = color(n*16 % 255); // Gosh, we could make fancy colors here if we wanted - x += dx; - } - y += dy; - } - updatePixels(); - - save("chapter08_02.png"); - noLoop(); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_14_Koch/Figure_8_14_Koch.pde b/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_14_Koch/Figure_8_14_Koch.pde deleted file mode 100644 index ea0ba890f..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_14_Koch/Figure_8_14_Koch.pde +++ /dev/null @@ -1,65 +0,0 @@ -// Koch Curve -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple fractal, the Koch snowflake -// Each recursive level drawn in sequence - -ArrayList lines ; // A list to keep track of all the lines - -void setup() { - size(1820, 200); - - - smooth(); -} - - - -void draw() { - translate(10,0); - int spacing = 10; - int total = 5; - - background(255); - float w = (1800-spacing*(total-1))/5; - for (int n = 0; n < total; n++) { - lines = new ArrayList(); - PVector start = new PVector(0, height*2/3); - PVector end = new PVector(w, height*2/3); - lines.add(new KochLine(start, end)); - for (int i = 0; i < n; i++) { - generate(); - } - strokeWeight(2); - for (KochLine l : lines) { - l.display(); - } - noFill(); - strokeWeight(1); - stroke(127); - rect(0, 10, w,height-20); - translate(w+spacing, 0); - } - save("chapter08_14.png"); - noLoop(); -} - -void generate() { - ArrayList next = new ArrayList(); // Create emtpy list - for (KochLine l : lines) { - // Calculate 5 koch PVectors (done for us by the line object) - PVector a = l.kochA(); - PVector b = l.kochB(); - PVector c = l.kochC(); - PVector d = l.kochD(); - PVector e = l.kochE(); - // Make line segments between all the PVectors and add them - next.add(new KochLine(a, b)); - next.add(new KochLine(b, c)); - next.add(new KochLine(c, d)); - next.add(new KochLine(d, e)); - } - lines = next; -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_14_Koch/KochLine.pde b/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_14_Koch/KochLine.pde deleted file mode 100644 index a241afe1c..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_14_Koch/KochLine.pde +++ /dev/null @@ -1,71 +0,0 @@ -// Koch Curve -// Daniel Shiffman - -// A class to describe one line segment in the fractal -// Includes methods to calculate midPVectors along the line according to the Koch algorithm - -class KochLine { - - // Two PVectors, - // a is the "left" PVector and - // b is the "right PVector - PVector start; - PVector end; - - KochLine(PVector a, PVector b) { - start = a.get(); - end = b.get(); - } - - void display() { - stroke(0); - line(start.x, start.y, end.x, end.y); - } - - PVector kochA() { - return start.get(); - } - - - // This is easy, just 1/3 of the way - PVector kochB() { - PVector v = PVector.sub(end, start); - v.div(3); - v.add(start); - return v; - } - - // More complicated, have to use a little trig to figure out where this PVector is! - PVector kochC() { - PVector a = start.get(); // Start at the beginning - - PVector v = PVector.sub(end, start); - v.div(3); - a.add(v); // Move to point B - - rotate(v, -radians(60)); // Rotate 60 degrees - a.add(v); // Move to point C - - return a; - } - - // Easy, just 2/3 of the way - PVector kochD() { - PVector v = PVector.sub(end, start); - v.mult(2/3.0); - v.add(start); - return v; - } - - PVector kochE() { - return end.get(); - } -} - -public void rotate(PVector v, float theta) { - float xTemp = v.x; - // Might need to check for rounding errors like with angleBetween function? - v.x = v.x*PApplet.cos(theta) - v.y*PApplet.sin(theta); - v.y = xTemp*PApplet.sin(theta) + v.y*PApplet.cos(theta); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_20_Tree/Figure_8_20_Tree.pde b/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_20_Tree/Figure_8_20_Tree.pde deleted file mode 100644 index b64c609fc..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_20_Tree/Figure_8_20_Tree.pde +++ /dev/null @@ -1,53 +0,0 @@ -// Recursive Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Branching angle calculated as a function of horizontal mouse location - -float theta; - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - // Let's pick an angle 0 to 90 degrees based on the mouse position - theta = map(mouseX,0,width,0,PI/2); - - // Start the tree from the bottom of the screen - translate(width/2, height); - stroke(0); - branch(60); -} - -void branch(float len) { - // Each branch will be 2/3rds the size of the previous one - - //float sw = map(len,2,120,1,10); - //strokeWeight(sw); - strokeWeight(2); - - line(0, 0, 0, -len); - // Move to the end of that line - translate(0, -len); - - len *= 0.66; - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (len > 2) { - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(len); // Ok, now call myself to draw two new branches!! - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - - // Repeat the same thing, only branch off to the "left" this time! - pushMatrix(); - rotate(-theta); - branch(len); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_CantorLine/Figure_8_CantorLine.pde b/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_CantorLine/Figure_8_CantorLine.pde deleted file mode 100644 index c79f11cc4..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Figure_8_CantorLine/Figure_8_CantorLine.pde +++ /dev/null @@ -1,20 +0,0 @@ - -void setup() { - size(800, 60); - background(255); -} - -void cantor(float x, float y, float len) { - line(x, y, x+len, y); - - y += 20; - line(x,y,x+len/3,y); // [bold] - line(x+len*2/3,y,x+len,y); // [bold] -} - -void draw() { - cantor(10, 20, width-20); - save("chapter08_12.png"); - noLoop(); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_01_Recursion/NOC_8_01_Recursion.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_01_Recursion/NOC_8_01_Recursion.pde deleted file mode 100644 index 13b5ff2f2..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_01_Recursion/NOC_8_01_Recursion.pde +++ /dev/null @@ -1,26 +0,0 @@ -// Simple Recursion -// Daniel Shiffman -// Nature of Code, Chapter 8 - -void setup() { - size(800,200); - smooth(); -} - -void draw() { - background(255); - drawCircle(width/2,height/2,width); - noLoop(); -} - -// Very simple function that draws one circle -// and recursively calls itself -void drawCircle(int x, int y, float r) { - ellipse(x, y, r, r); - // Exit condition, stop when radius is too small - if(r > 2) { - r *= 0.75f; - // Call the function inside the function! (recursion!) - drawCircle(x, y, r); - } -} diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_02_Recursion/NOC_8_02_Recursion.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_02_Recursion/NOC_8_02_Recursion.pde deleted file mode 100644 index 1a460ecba..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_02_Recursion/NOC_8_02_Recursion.pde +++ /dev/null @@ -1,27 +0,0 @@ -// Simple Recursion -// Daniel Shiffman -// Nature of Code, Chapter 8 - -void setup() { - size(800,200); - smooth(); -} - -void draw() { - background(255); - drawCircle(width/2,height/2,400); - noLoop(); -} - -// Recursive function -void drawCircle(float x, float y, float r) { - stroke(0); - noFill(); - ellipse(x, y, r, r); - if(r > 2) { - // Now we draw two more circles, one to the left - // and one to the right - drawCircle(x + r/2, y, r/2); - drawCircle(x - r/2, y, r/2); - } -} diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_KochSimple/KochLine.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_KochSimple/KochLine.pde deleted file mode 100644 index a241afe1c..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_KochSimple/KochLine.pde +++ /dev/null @@ -1,71 +0,0 @@ -// Koch Curve -// Daniel Shiffman - -// A class to describe one line segment in the fractal -// Includes methods to calculate midPVectors along the line according to the Koch algorithm - -class KochLine { - - // Two PVectors, - // a is the "left" PVector and - // b is the "right PVector - PVector start; - PVector end; - - KochLine(PVector a, PVector b) { - start = a.get(); - end = b.get(); - } - - void display() { - stroke(0); - line(start.x, start.y, end.x, end.y); - } - - PVector kochA() { - return start.get(); - } - - - // This is easy, just 1/3 of the way - PVector kochB() { - PVector v = PVector.sub(end, start); - v.div(3); - v.add(start); - return v; - } - - // More complicated, have to use a little trig to figure out where this PVector is! - PVector kochC() { - PVector a = start.get(); // Start at the beginning - - PVector v = PVector.sub(end, start); - v.div(3); - a.add(v); // Move to point B - - rotate(v, -radians(60)); // Rotate 60 degrees - a.add(v); // Move to point C - - return a; - } - - // Easy, just 2/3 of the way - PVector kochD() { - PVector v = PVector.sub(end, start); - v.mult(2/3.0); - v.add(start); - return v; - } - - PVector kochE() { - return end.get(); - } -} - -public void rotate(PVector v, float theta) { - float xTemp = v.x; - // Might need to check for rounding errors like with angleBetween function? - v.x = v.x*PApplet.cos(theta) - v.y*PApplet.sin(theta); - v.y = xTemp*PApplet.sin(theta) + v.y*PApplet.cos(theta); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_KochSimple/NOC_8_03_KochSimple.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_KochSimple/NOC_8_03_KochSimple.pde deleted file mode 100644 index 0bfb84f6c..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_KochSimple/NOC_8_03_KochSimple.pde +++ /dev/null @@ -1,50 +0,0 @@ - // Koch Curve -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple fractal, the Koch snowflake -// Each recursive level drawn in sequence - -ArrayList lines ; // A list to keep track of all the lines - -void setup() { - size(600, 300); - background(255); - lines = new ArrayList(); - PVector start = new PVector(0, 200); - PVector end = new PVector(width, 200); - lines.add(new KochLine(start, end)); - - for (int i = 0; i < 5; i++) { - generate(); - } - - smooth(); -} - -void draw() { - background(255); - for (KochLine l : lines) { - l.display(); - } -} - -void generate() { - ArrayList next = new ArrayList(); // Create emtpy list - for (KochLine l : lines) { - // Calculate 5 koch PVectors (done for us by the line object) - PVector a = l.kochA(); - PVector b = l.kochB(); - PVector c = l.kochC(); - PVector d = l.kochD(); - PVector e = l.kochE(); - // Make line segments between all the PVectors and add them - next.add(new KochLine(a, b)); - next.add(new KochLine(b, c)); - next.add(new KochLine(c, d)); - next.add(new KochLine(d, e)); - } - lines = next; -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_Recursion/NOC_8_03_Recursion.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_Recursion/NOC_8_03_Recursion.pde deleted file mode 100644 index 55672e39f..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_03_Recursion/NOC_8_03_Recursion.pde +++ /dev/null @@ -1,28 +0,0 @@ -// Simple Recursion -// Daniel Shiffman -// Nature of Code, Chapter 8 - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - drawCircle(width/2, height/2, 400); - noLoop(); -} - -void drawCircle(float x, float y, float radius) { - noFill(); - stroke(0); - ellipse(x, y, radius, radius); - if (radius > 8) { - // Four circles! left right, up and down - drawCircle(x + radius/2, y, radius/2); - drawCircle(x - radius/2, y, radius/2); - drawCircle(x, y + radius/2, radius/2); - drawCircle(x, y - radius/2, radius/2); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_04_CantorSet/NOC_8_04_CantorSet.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_04_CantorSet/NOC_8_04_CantorSet.pde deleted file mode 100644 index 8bced0afb..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_04_CantorSet/NOC_8_04_CantorSet.pde +++ /dev/null @@ -1,38 +0,0 @@ -// Cantor Set -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple fractal, the Cantor Set - -void setup() { - size(800, 200); - background(255); - - // Call the recursive function - cantor(35, 0, 730); -} - -void draw() { - // No need to loop - noLoop(); -} - - -void cantor(float x, float y, float len) { - - float h = 30; - - // recursive exit condition - if (len >= 1) { - // Draw line (as rectangle to make it easier to see) - noStroke(); - fill(0); - rect(x, y, len, h/3); - // Go down to next y position - y += h; - // Draw 2 more lines 1/3rd the length (without the middle section) - cantor(x, y, len/3); - cantor(x+len*2/3, y, len/3); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_04_Tree/NOC_8_04_Tree.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_04_Tree/NOC_8_04_Tree.pde deleted file mode 100644 index 2cf4d38da..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_04_Tree/NOC_8_04_Tree.pde +++ /dev/null @@ -1,52 +0,0 @@ -// Recursive Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Branching angle calculated as a function of horizontal mouse location - -float theta; - -void setup() { - size(300, 200); - smooth(); -} - -void draw() { - background(255); - // Let's pick an angle 0 to 90 degrees based on the mouse position - theta = map(mouseX,0,width,0,PI/2); - - // Start the tree from the bottom of the screen - translate(width/2, height); - stroke(0); - branch(60); -} - -void branch(float len) { - // Each branch will be 2/3rds the size of the previous one - - float sw = map(len,2,120,1,10); - strokeWeight(sw); - - line(0, 0, 0, -len); - // Move to the end of that line - translate(0, -len); - - len *= 0.66; - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (len > 2) { - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(len); // Ok, now call myself to draw two new branches!! - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - - // Repeat the same thing, only branch off to the "left" this time! - pushMatrix(); - rotate(-theta); - branch(len); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/KochFractal.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/KochFractal.pde deleted file mode 100644 index c045cdd9d..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/KochFractal.pde +++ /dev/null @@ -1,70 +0,0 @@ -// Koch Curve -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// A class to manage the list of line segments in the snowflake pattern - -class KochFractal { - PVector start; // A PVector for the start - PVector end; // A PVector for the end - ArrayList lines; // A list to keep track of all the lines - int count; - - public KochFractal() { - start = new PVector(0,height-20); - end = new PVector(width,height-20); - lines = new ArrayList(); - restart(); - } - - void nextLevel() { - // For every line that is in the arraylist - // create 4 more lines in a new arraylist - lines = iterate(lines); - count++; - } - - void restart() { - count = 0; // Reset count - lines.clear(); // Empty the array list - lines.add(new KochLine(start,end)); // Add the initial line (from one end PVector to the other) - } - - int getCount() { - return count; - } - - // This is easy, just draw all the lines - void render() { - for(KochLine l : lines) { - l.display(); - } - } - - // This is where the **MAGIC** happens - // Step 1: Create an empty arraylist - // Step 2: For every line currently in the arraylist - // - calculate 4 line segments based on Koch algorithm - // - add all 4 line segments into the new arraylist - // Step 3: Return the new arraylist and it becomes the list of line segments for the structure - - // As we do this over and over again, each line gets broken into 4 lines, which gets broken into 4 lines, and so on. . . - ArrayList iterate(ArrayList before) { - ArrayList now = new ArrayList(); // Create emtpy list - for(KochLine l : before) { - // Calculate 5 koch PVectors (done for us by the line object) - PVector a = l.start(); - PVector b = l.kochleft(); - PVector c = l.kochmiddle(); - PVector d = l.kochright(); - PVector e = l.end(); - // Make line segments between all the PVectors and add them - now.add(new KochLine(a,b)); - now.add(new KochLine(b,c)); - now.add(new KochLine(c,d)); - now.add(new KochLine(d,e)); - } - return now; - } - -} diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/KochLine.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/KochLine.pde deleted file mode 100644 index 9f4f59871..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/KochLine.pde +++ /dev/null @@ -1,73 +0,0 @@ -// Koch Curve -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// A class to describe one line segment in the fractal -// Includes methods to calculate midPVectors along the line according to the Koch algorithm - -class KochLine { - - // Two PVectors, - // a is the "left" PVector and - // b is the "right PVector - PVector a; - PVector b; - - KochLine(PVector start, PVector end) { - a = start.get(); - b = end.get(); - } - - void display() { - stroke(0); - line(a.x, a.y, b.x, b.y); - } - - PVector start() { - return a.get(); - } - - PVector end() { - return b.get(); - } - - // This is easy, just 1/3 of the way - PVector kochleft() { - PVector v = PVector.sub(b, a); - v.div(3); - v.add(a); - return v; - } - - // More complicated, have to use a little trig to figure out where this PVector is! - PVector kochmiddle() { - PVector v = PVector.sub(b, a); - v.div(3); - - PVector p = a.get(); - p.add(v); - - rotate(v,-radians(60)); - p.add(v); - - return p; - } - - - // Easy, just 2/3 of the way - PVector kochright() { - PVector v = PVector.sub(a, b); - v.div(3); - v.add(b); - return v; - } -} - - public void rotate(PVector v, float theta) { - float xTemp = v.x; - // Might need to check for rounding errors like with angleBetween function? - v.x = v.x*cos(theta) - v.y*sin(theta); - v.y = xTemp*sin(theta) + v.y*cos(theta); - } - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/NOC_8_05_Koch.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/NOC_8_05_Koch.pde deleted file mode 100644 index 8d6fe3a35..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/NOC_8_05_Koch.pde +++ /dev/null @@ -1,29 +0,0 @@ -// Koch Curve -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple fractal, the Koch snowflake -// Each recursive level drawn in sequence - -KochFractal k; - -void setup() { - size(800,250); - background(255); - frameRate(1); // Animate slowly - k = new KochFractal(); - smooth(); -} - -void draw() { - background(255); - // Draws the snowflake! - k.render(); - // Iterate - k.nextLevel(); - // Let's not do it more than 5 times. . . - if (k.getCount() > 5) { - k.restart(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/sketch.properties b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/sketch.properties deleted file mode 100644 index 140966b6e..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_Koch/sketch.properties +++ /dev/null @@ -1,2 +0,0 @@ -mode.id=processing.mode.javascript.JavaScriptMode -mode=JavaScript diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_KochSimple/KochLine.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_KochSimple/KochLine.pde deleted file mode 100644 index a241afe1c..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_KochSimple/KochLine.pde +++ /dev/null @@ -1,71 +0,0 @@ -// Koch Curve -// Daniel Shiffman - -// A class to describe one line segment in the fractal -// Includes methods to calculate midPVectors along the line according to the Koch algorithm - -class KochLine { - - // Two PVectors, - // a is the "left" PVector and - // b is the "right PVector - PVector start; - PVector end; - - KochLine(PVector a, PVector b) { - start = a.get(); - end = b.get(); - } - - void display() { - stroke(0); - line(start.x, start.y, end.x, end.y); - } - - PVector kochA() { - return start.get(); - } - - - // This is easy, just 1/3 of the way - PVector kochB() { - PVector v = PVector.sub(end, start); - v.div(3); - v.add(start); - return v; - } - - // More complicated, have to use a little trig to figure out where this PVector is! - PVector kochC() { - PVector a = start.get(); // Start at the beginning - - PVector v = PVector.sub(end, start); - v.div(3); - a.add(v); // Move to point B - - rotate(v, -radians(60)); // Rotate 60 degrees - a.add(v); // Move to point C - - return a; - } - - // Easy, just 2/3 of the way - PVector kochD() { - PVector v = PVector.sub(end, start); - v.mult(2/3.0); - v.add(start); - return v; - } - - PVector kochE() { - return end.get(); - } -} - -public void rotate(PVector v, float theta) { - float xTemp = v.x; - // Might need to check for rounding errors like with angleBetween function? - v.x = v.x*PApplet.cos(theta) - v.y*PApplet.sin(theta); - v.y = xTemp*PApplet.sin(theta) + v.y*PApplet.cos(theta); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_KochSimple/NOC_8_05_KochSimple.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_KochSimple/NOC_8_05_KochSimple.pde deleted file mode 100644 index e387ed5c4..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_KochSimple/NOC_8_05_KochSimple.pde +++ /dev/null @@ -1,50 +0,0 @@ - // Koch Curve -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple fractal, the Koch snowflake -// Each recursive level drawn in sequence - -ArrayList lines ; // A list to keep track of all the lines - -void setup() { - size(383, 200); - background(255); - lines = new ArrayList(); - PVector start = new PVector(0, 150); - PVector end = new PVector(width, 150); - lines.add(new KochLine(start, end)); - - for (int i = 0; i < 5; i++) { - generate(); - } - - smooth(); -} - -void draw() { - background(255); - for (KochLine l : lines) { - l.display(); - } -} - -void generate() { - ArrayList next = new ArrayList(); // Create emtpy list - for (KochLine l : lines) { - // Calculate 5 koch PVectors (done for us by the line object) - PVector a = l.kochA(); - PVector b = l.kochB(); - PVector c = l.kochC(); - PVector d = l.kochD(); - PVector e = l.kochE(); - // Make line segments between all the PVectors and add them - next.add(new KochLine(a, b)); - next.add(new KochLine(b, c)); - next.add(new KochLine(c, d)); - next.add(new KochLine(d, e)); - } - lines = next; -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_TreeStochastic/NOC_8_05_TreeStochastic.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_TreeStochastic/NOC_8_05_TreeStochastic.pde deleted file mode 100644 index 17acd3e21..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_05_TreeStochastic/NOC_8_05_TreeStochastic.pde +++ /dev/null @@ -1,63 +0,0 @@ -// Stochastic Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Angles and number of branches are random - -void setup() { - size(600, 400); - smooth(); - newTree(); -} - -void draw() { - -} - -void mousePressed() { - newTree(); -} - -void newTree() { - background(255); - fill(0); - text("Click mouse to generate a new tree", 10, height-20); - - stroke(0); - // Start the tree from the bottom of the screen - translate(width/2, height); - // Start the recursive branching! - branch(120); -} - - - -void branch(float h) { - // thickness of the branch is mapped to its length - float sw = map(h, 2, 120, 1, 5); - strokeWeight(sw); - // Draw the actual branch - line(0, 0, 0, -h); - // Move along to end - translate(0, -h); - - // Each branch will be 2/3rds the size of the previous one - h *= 0.66f; - - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (h > 2) { - // A random number of branches - int n = int(random(1, 4)); - for (int i = 0; i < n; i++) { - // Picking a random angle - float theta = random(-PI/2, PI/2); - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(h); // Ok, now call myself to branch again - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_SimpleLSystem/NOC_8_06_SimpleLSystem.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_SimpleLSystem/NOC_8_06_SimpleLSystem.pde deleted file mode 100644 index 14e10da7e..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_SimpleLSystem/NOC_8_06_SimpleLSystem.pde +++ /dev/null @@ -1,47 +0,0 @@ -// L-System -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Just demonstrating working with L-System strings -// No drawing - -// Start with "A" -String current = "A"; -// Number of generations -int count = 0; - -void setup() { - size(200, 200); - println("Generation " + count + ": " + current); -} - -void draw() { - background(255); - fill(0); - text("Click mouse to generate", 10, height-20); - noLoop(); -} - -void mousePressed() { - // A new StringBuffer for the next generation - StringBuffer next = new StringBuffer(); - - // Look through the current String to replace according to L-System rules - for (int i = 0; i < current.length(); i++) { - char c = current.charAt(i); - if (c == 'A') { - // If we find A replace with AB - next.append("AB"); - } else if (c == 'B') { - // If we find B replace with A - next.append("A"); - } - } - // The current String is now the next one - current = next.toString(); - count++; - // Print to message console - println("Generation " + count + ": " + current); - println(count + " " + current.length()); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_Tree/NOC_8_06_Tree.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_Tree/NOC_8_06_Tree.pde deleted file mode 100644 index 4b415662d..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_Tree/NOC_8_06_Tree.pde +++ /dev/null @@ -1,53 +0,0 @@ -// Recursive Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Branching angle calculated as a function of horizontal mouse location - -float theta; - -void setup() { - size(250, 200); - smooth(); -} - -void draw() { - background(255); - // Let's pick an angle 0 to 90 degrees based on the mouse position - theta = map(mouseX,0,width,0,PI/2); - - // Start the tree from the bottom of the screen - translate(width/2, height); - stroke(0); - branch(60); -} - -void branch(float len) { - // Each branch will be 2/3rds the size of the previous one - - //float sw = map(len,2,120,1,10); - //strokeWeight(sw); - strokeWeight(2); - - line(0, 0, 0, -len); - // Move to the end of that line - translate(0, -len); - - len *= 0.66; - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (len > 2) { - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(len); // Ok, now call myself to draw two new branches!! - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - - // Repeat the same thing, only branch off to the "left" this time! - pushMatrix(); - rotate(-theta); - branch(len); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_Tree_static/NOC_8_06_Tree_static.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_Tree_static/NOC_8_06_Tree_static.pde deleted file mode 100644 index 92805fdb4..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_06_Tree_static/NOC_8_06_Tree_static.pde +++ /dev/null @@ -1,45 +0,0 @@ -// Recursive Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Branching angle calculated as a function of horizontal mouse location - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - // Start the tree from the bottom of the screen - translate(width/2, height); - stroke(0); - branch(60); - noLoop(); -} - -void branch(float len) { - strokeWeight(2); - - line(0, 0, 0, -len); - // Move to the end of that line - translate(0, -len); - - len *= 0.66; - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (len > 2) { - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(PI/5); // Rotate by theta - branch(len); // Ok, now call myself to draw two new branches!! - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - - // Repeat the same thing, only branch off to the "left" this time! - pushMatrix(); - rotate(-PI/5); - branch(len); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_07_TreeStochastic/NOC_8_07_TreeStochastic.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_07_TreeStochastic/NOC_8_07_TreeStochastic.pde deleted file mode 100644 index 9b6af0bc7..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_07_TreeStochastic/NOC_8_07_TreeStochastic.pde +++ /dev/null @@ -1,66 +0,0 @@ -// Stochastic Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Angles and number of branches are random - -void setup() { - size(800, 200); - smooth(); - newTree(); -} - -void draw() { - noLoop(); -} - -void mousePressed() { - newTree(); - redraw(); -} - -void newTree() { - background(255); - fill(0); - text("Click mouse to generate a new tree", 10, height-10); - - stroke(0); - pushMatrix(); - // Start the tree from the bottom of the screen - translate(width/2, height); - // Start the recursive branching! - branch(80); - popMatrix(); -} - - - -void branch(float h) { - // thickness of the branch is mapped to its length - float sw = map(h, 2, 120, 1, 5); - strokeWeight(sw); - // Draw the actual branch - line(0, 0, 0, -h); - // Move along to end - translate(0, -h); - - // Each branch will be 2/3rds the size of the previous one - h *= 0.66f; - - // All recursive functions must have an exit condition!!!! - // Here, ours is when the length of the branch is 2 pixels or less - if (h > 2) { - // A random number of branches - int n = int(random(1, 4)); - for (int i = 0; i < n; i++) { - // Picking a random angle - float theta = random(-PI/2, PI/2); - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(h); // Ok, now call myself to branch again - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_07_TreeStochastic_angleonly/NOC_8_07_TreeStochastic_angleonly.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_07_TreeStochastic_angleonly/NOC_8_07_TreeStochastic_angleonly.pde deleted file mode 100644 index ef4b2a41b..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_07_TreeStochastic_angleonly/NOC_8_07_TreeStochastic_angleonly.pde +++ /dev/null @@ -1,59 +0,0 @@ -// Stochastic Tree -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Renders a simple tree-like structure via recursion -// Angles and number of branches are random - -void setup() { - size(800, 200); - smooth(); - newTree(); -} - -void draw() { - noLoop(); -} - -void mousePressed() { - pushMatrix(); - newTree(); - popMatrix(); - redraw(); -} - -void newTree() { - background(255); - fill(0); - text("Click mouse to generate a new tree", 10, height-10); - - stroke(0); - // Start the tree from the bottom of the screen - translate(width/2, height); - // Start the recursive branching! - branch(60); -} - - - -void branch(float h) { - // thickness of the branch is mapped to its length - float sw = map(h, 2, 120, 1, 5); - strokeWeight(sw); - float theta = random(0,PI/3); - - line(0, 0, 0, -h); - translate(0, -h); - h *= 0.66; - if (h > 2) { - pushMatrix(); - rotate(theta); - branch(h); - popMatrix(); - pushMatrix(); - rotate(-theta); - branch(h); - popMatrix(); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_08_SimpleLSystem/NOC_8_08_SimpleLSystem.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_08_SimpleLSystem/NOC_8_08_SimpleLSystem.pde deleted file mode 100644 index da65cab34..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_08_SimpleLSystem/NOC_8_08_SimpleLSystem.pde +++ /dev/null @@ -1,47 +0,0 @@ -// L-System -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Just demonstrating working with L-System strings -// No drawing - -// Start with "A" -String current = "A"; -// Number of generations -int count = 0; - -void setup() { - size(800, 200); - println("Generation " + count + ": " + current); -} - -void draw() { - background(255); - fill(0); - text("Click mouse to generate", 10, height-20); - noLoop(); -} - -void mousePressed() { - // A new StringBuffer for the next generation - StringBuffer next = new StringBuffer(); - - // Look through the current String to replace according to L-System rules - for (int i = 0; i < current.length(); i++) { - char c = current.charAt(i); - if (c == 'A') { - // If we find A replace with AB - next.append("AB"); - } else if (c == 'B') { - // If we find B replace with A - next.append("A"); - } - } - // The current String is now the next one - current = next.toString(); - count++; - // Print to message console - println("Generation " + count + ": " + current); - //println(count + " " + current.length()); -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/LSystem.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/LSystem.pde deleted file mode 100644 index 3d381bd82..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/LSystem.pde +++ /dev/null @@ -1,67 +0,0 @@ -/* Daniel Shiffman */ -/* Programming from A to Z */ -/* Spring 2006 */ -/* http://www.shiffman.net */ -/* daniel.shiffman@nyu.edu */ - -/* LSystem Class */ - -// An LSystem has a starting sentence -// An a ruleset -// Each generation recursively replaces characteres in the sentence -// Based on the rulset - -class LSystem { - - String sentence; // The sentence (a String) - Rule[] ruleset; // The ruleset (an array of Rule objects) - int generation; // Keeping track of the generation # - - // Construct an LSystem with a startin sentence and a ruleset - LSystem(String axiom, Rule[] r) { - sentence = axiom; - ruleset = r; - generation = 0; - } - - // Generate the next generation - void generate() { - // An empty StringBuffer that we will fill - StringBuffer nextgen = new StringBuffer(); - // For every character in the sentence - for (int i = 0; i < sentence.length(); i++) { - // What is the character - char curr = sentence.charAt(i); - // We will replace it with itself unless it matches one of our rules - String replace = "" + curr; - // Check every rule - for (int j = 0; j < ruleset.length; j++) { - char a = ruleset[j].getA(); - // if we match the Rule, get the replacement String out of the Rule - if (a == curr) { - replace = ruleset[j].getB(); - break; - } - } - // Append replacement String - nextgen.append(replace); - } - // Replace sentence - sentence = nextgen.toString(); - // Increment generation - generation++; - } - - String getSentence() { - return sentence; - } - - int getGeneration() { - return generation; - } - - -} - - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/NOC_8_09_LSystem.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/NOC_8_09_LSystem.pde deleted file mode 100644 index 9d745a120..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/NOC_8_09_LSystem.pde +++ /dev/null @@ -1,63 +0,0 @@ -/* Daniel Shiffman */ -/* The Nature of Code */ -/* http://www.shiffman.net */ -/* daniel.shiffman@nyu.edu */ - -LSystem lsys; -Turtle turtle; - -void setup() { - size(800, 200); - /* - // Create an empty ruleset - Rule[] ruleset = new Rule[2]; - // Fill with two rules (These are rules for the Sierpinksi Gasket Triangle) - ruleset[0] = new Rule('F',"F--F--F--G"); - ruleset[1] = new Rule('G',"GG"); - // Create LSystem with axiom and ruleset - lsys = new LSystem("F--F--F",ruleset); - turtle = new Turtle(lsys.getSentence(),width*2,TWO_PI/3); - */ - - /*Rule[] ruleset = new Rule[1]; - //ruleset[0] = new Rule('F',"F[F]-F+F[--F]+F-F"); - ruleset[0] = new Rule['F',"FF+[+F-F-F]-[-F+F+F]"); - lsys = new LSystem("F-F-F-F",ruleset); - turtle = new Turtle(lsys.getSentence(),width-1,PI/2); - */ - - Rule[] ruleset = new Rule[1]; - ruleset[0] = new Rule('F', "FF+[+F-F-F]-[-F+F+F]"); - lsys = new LSystem("F", ruleset); - turtle = new Turtle(lsys.getSentence(), height/3, radians(25)); - - - - smooth(); -} - -void draw() { - background(255); - fill(0); - //text("Click mouse to generate", 10, height-10); - - translate(width/2, height); - rotate(-PI/2); - turtle.render(); - noLoop(); -} - -int counter = 0; - -void mousePressed() { - if (counter < 5) { - pushMatrix(); - lsys.generate(); - turtle.setToDo(lsys.getSentence()); - turtle.changeLen(0.5); - popMatrix(); - redraw(); - counter++; - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/Rule.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/Rule.pde deleted file mode 100644 index 43142063e..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/Rule.pde +++ /dev/null @@ -1,26 +0,0 @@ -/* Daniel Shiffman */ -/* http://www.shiffman.net */ - -/* A Class to describe an - LSystem Rule */ - -class Rule { - char a; - String b; - - Rule(char a_, String b_) { - a = a_; - b = b_; - } - - char getA() { - return a; - } - - String getB() { - return b; - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/Turtle.pde b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/Turtle.pde deleted file mode 100644 index 0193562e2..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/Turtle.pde +++ /dev/null @@ -1,53 +0,0 @@ -/* Daniel Shiffman */ -/* http://www.shiffman.net */ - -class Turtle { - - String todo; - float len; - float theta; - - Turtle(String s, float l, float t) { - todo = s; - len = l; - theta = t; - } - - void render() { - stroke(0,175); - for (int i = 0; i < todo.length(); i++) { - char c = todo.charAt(i); - if (c == 'F' || c == 'G') { - line(0,0,len,0); - translate(len,0); - } - else if (c == '+') { - rotate(theta); - } - else if (c == '-') { - rotate(-theta); - } - else if (c == '[') { - pushMatrix(); - } - else if (c == ']') { - popMatrix(); - } - } - } - - void setLen(float l) { - len = l; - } - - void changeLen(float percent) { - len *= percent; - } - - void setToDo(String s) { - todo = s; - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/sketch.properties b/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/sketch.properties deleted file mode 100644 index b3cbe600e..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/NOC_8_09_LSystem/sketch.properties +++ /dev/null @@ -1,2 +0,0 @@ -mode.id=processing.mode.java.JavaMode -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Tree2/Branch.pde b/java/examples/Books/Nature of Code/chp8_fractals/Tree2/Branch.pde deleted file mode 100644 index 67f073dcf..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Tree2/Branch.pde +++ /dev/null @@ -1,59 +0,0 @@ -// Recursive Tree (w/ ArrayList) -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// A class for one branch in the system - -class Branch { - // Each has a location, velocity, and timer - // We could implement this same idea with different data - PVector loc; - PVector vel; - float timer; - float timerstart; - - Branch(PVector l, PVector v, float n) { - loc = l.get(); - vel = v.get(); - timerstart = n; - timer = timerstart; - } - - // Move location - void update() { - loc.add(vel); - } - - // Draw a dot at location - void render() { - fill(0); - noStroke(); - ellipseMode(CENTER); - ellipse(loc.x,loc.y,2,2); - } - - // Did the timer run out? - boolean timeToBranch() { - timer--; - if (timer < 0) { - return true; - } else { - return false; - } - } - - // Create a new branch at the current location, but change direction by a given angle - Branch branch(float angle) { - // What is my current heading - float theta = vel.heading2D(); - // What is my current speed - float mag = vel.mag(); - // Turn me - theta += radians(angle); - // Look, polar coordinates to cartesian!! - PVector newvel = new PVector(mag*cos(theta),mag*sin(theta)); - // Return a new Branch - return new Branch(loc,newvel,timerstart*0.66f); - } - -} diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Tree2/Tree2.pde b/java/examples/Books/Nature of Code/chp8_fractals/Tree2/Tree2.pde deleted file mode 100644 index 3d558d0d9..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Tree2/Tree2.pde +++ /dev/null @@ -1,47 +0,0 @@ -// Recursive Tree (w/ ArrayList) -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Recursive branching "structure" without an explicitly recursive function -// Instead we have an ArrayList to hold onto N number of elements -// For every element in the ArrayList, we add 2 more elements, etc. (this is the recursion) - -// An arraylist that will keep track of all current branches -ArrayList tree; - -void setup() { - size(200,200); - background(255); - smooth(); - - // Setup the arraylist and add one branch to it - tree = new ArrayList(); - // A branch has a starting location, a starting "velocity", and a starting "timer" - Branch b = new Branch(new PVector(width/2,height),new PVector(0,-0.5),100); - // Add to arraylist - tree.add(b); -} - -void draw() { - // Try erasing the background to see how it works - // background(255); - - // Let's stop when the arraylist gets too big - if (tree.size() < 1024) { - // For every branch in the arraylist - for (int i = tree.size()-1; i >= 0; i--) { - // Get the branch, update and draw it - Branch b = tree.get(i); - b.update(); - b.render(); - // If it's ready to split - if (b.timeToBranch()) { - tree.remove(i); // Delete it - tree.add(b.branch( 30)); // Add one going right - tree.add(b.branch(-25)); // Add one going left - } - } - } -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Branch.pde b/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Branch.pde deleted file mode 100644 index a6921813b..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Branch.pde +++ /dev/null @@ -1,66 +0,0 @@ -// Recursive Tree (w/ ArrayList) -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// A class for one branch in the system - -class Branch { - // Each has a location, velocity, and timer - // We could implement this same idea with different data - PVector start; - PVector end; - PVector vel; - float timer; - float timerstart; - - boolean growing = true; - - Branch(PVector l, PVector v, float n) { - start = l.get(); - end = l.get(); - vel = v.get(); - timerstart = n; - timer = timerstart; - } - - // Move location - void update() { - if (growing) { - end.add(vel); - } - } - - // Draw a dot at location - void render() { - stroke(0); - line(start.x,start.y,end.x,end.y); - } - - // Did the timer run out? - boolean timeToBranch() { - timer--; - if (timer < 0 && growing) { - growing = false; - return true; - } - else { - return false; - } - } - - // Create a new branch at the current location, but change direction by a given angle - Branch branch(float angle) { - // What is my current heading - float theta = vel.heading2D(); - // What is my current speed - float mag = vel.mag(); - // Turn me - theta += radians(angle); - // Look, polar coordinates to cartesian!! - PVector newvel = new PVector(mag*cos(theta),mag*sin(theta)); - // Return a new Branch - return new Branch(end,newvel,timerstart*0.66f); - } - -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Leaf.pde b/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Leaf.pde deleted file mode 100644 index 145e0d1ce..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Leaf.pde +++ /dev/null @@ -1,21 +0,0 @@ -// Recursive Tree (w/ ArrayList) -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// A class for a leaf that gets placed at the end of -// the last branches - -class Leaf { - PVector loc; - - Leaf(PVector l) { - loc = l.get(); - } - - void display() { - noStroke(); - fill(50,100); - ellipse(loc.x,loc.y,4,4); - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Tree3.pde b/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Tree3.pde deleted file mode 100644 index 612650d72..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/Tree3/Tree3.pde +++ /dev/null @@ -1,58 +0,0 @@ -// Recursive Tree (w/ ArrayList) -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Recursive branching "structure" without an explicitly recursive function -// Instead we have an ArrayList to hold onto N number of elements -// For every element in the ArrayList, we add 2 more elements, etc. (this is the recursion) - -// An arraylist that will keep track of all current branches -ArrayList tree; -ArrayList leaves; - -void setup() { - size(200,200); - background(255); - smooth(); - - // Setup the arraylist and add one branch to it - tree = new ArrayList(); - leaves = new ArrayList(); - // A branch has a starting location, a starting "velocity", and a starting "timer" - Branch b = new Branch(new PVector(width/2,height),new PVector(0,-0.5),100); - // Add to arraylist - tree.add(b); -} - -void draw() { - background(255); - - // Let's stop when the arraylist gets too big - // For every branch in the arraylist - for (int i = tree.size()-1; i >= 0; i--) { - // Get the branch, update and draw it - Branch b = tree.get(i); - b.update(); - b.render(); - // If it's ready to split - if (b.timeToBranch()) { - if (tree.size() < 1024) { - //tree.remove(i); // Delete it - tree.add(b.branch( 30)); // Add one going right - tree.add(b.branch(-25)); // Add one going left - } - else { - leaves.add(new Leaf(b.end)); - } - } - } - - for (Leaf leaf : leaves) { - leaf.display(); - } - -} - - - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/TreeStochasticNoise/TreeStochasticNoise.pde b/java/examples/Books/Nature of Code/chp8_fractals/TreeStochasticNoise/TreeStochasticNoise.pde deleted file mode 100644 index 2a5544943..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/TreeStochasticNoise/TreeStochasticNoise.pde +++ /dev/null @@ -1,71 +0,0 @@ -// Stochastic Tree with angles fluctuating with Perlin noise -// Daniel Shiffman -// Nature of Code, Chapter 8 - -// Perlin noise offset -float yoff = 0; -// Random seed to control randomness while drawing the tree -int seed = 5; - - -void setup() { - size(800, 200); - smooth(); -} - -void draw() { - background(255); - fill(0); - //text("Click mouse to generate a new tree", 10, height-20); - - stroke(0); - // Start the tree from the bottom of the screen - translate(width/2, height); - // Move alogn through noise - yoff += 0.005; - randomSeed(seed); - // Start the recursive branching! - branch(60, 0); -} - - -void mousePressed() { - // New tree starts with new noise offset and new random seed - yoff = random(1000); - seed = millis(); -} - - -void branch(float h, float xoff) { - // thickness of the branch is mapped to its length - float sw = map(h, 2, 100, 1, 5); - strokeWeight(sw); - // Draw the branch - line(0, 0, 0, -h); - // Move along to end - translate(0, -h); - - // Each branch will be 2/3rds the size of the previous one - h *= 0.7f; - - // Move along through noise space - xoff += 0.1; - - if (h > 4) { - // Random number of branches - int n = int(random(0, 5)); - for (int i = 0; i < n; i++) { - - // Here the angle is controlled by perlin noise - // This is a totally arbitrary way to do it, try others! - float theta = map(noise(xoff+i, yoff), 0, 1, -PI/3, PI/3); - if (n%2==0) theta *= -1; - - pushMatrix(); // Save the current state of transformation (i.e. where are we now) - rotate(theta); // Rotate by theta - branch(h, xoff); // Ok, now call myself to branch again - popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/TreeStochasticNoise/sketch.properties b/java/examples/Books/Nature of Code/chp8_fractals/TreeStochasticNoise/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/TreeStochasticNoise/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp8_fractals/lsys/LSystem.pde b/java/examples/Books/Nature of Code/chp8_fractals/lsys/LSystem.pde deleted file mode 100644 index 3d381bd82..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/lsys/LSystem.pde +++ /dev/null @@ -1,67 +0,0 @@ -/* Daniel Shiffman */ -/* Programming from A to Z */ -/* Spring 2006 */ -/* http://www.shiffman.net */ -/* daniel.shiffman@nyu.edu */ - -/* LSystem Class */ - -// An LSystem has a starting sentence -// An a ruleset -// Each generation recursively replaces characteres in the sentence -// Based on the rulset - -class LSystem { - - String sentence; // The sentence (a String) - Rule[] ruleset; // The ruleset (an array of Rule objects) - int generation; // Keeping track of the generation # - - // Construct an LSystem with a startin sentence and a ruleset - LSystem(String axiom, Rule[] r) { - sentence = axiom; - ruleset = r; - generation = 0; - } - - // Generate the next generation - void generate() { - // An empty StringBuffer that we will fill - StringBuffer nextgen = new StringBuffer(); - // For every character in the sentence - for (int i = 0; i < sentence.length(); i++) { - // What is the character - char curr = sentence.charAt(i); - // We will replace it with itself unless it matches one of our rules - String replace = "" + curr; - // Check every rule - for (int j = 0; j < ruleset.length; j++) { - char a = ruleset[j].getA(); - // if we match the Rule, get the replacement String out of the Rule - if (a == curr) { - replace = ruleset[j].getB(); - break; - } - } - // Append replacement String - nextgen.append(replace); - } - // Replace sentence - sentence = nextgen.toString(); - // Increment generation - generation++; - } - - String getSentence() { - return sentence; - } - - int getGeneration() { - return generation; - } - - -} - - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/lsys/Rule.pde b/java/examples/Books/Nature of Code/chp8_fractals/lsys/Rule.pde deleted file mode 100644 index 43142063e..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/lsys/Rule.pde +++ /dev/null @@ -1,26 +0,0 @@ -/* Daniel Shiffman */ -/* http://www.shiffman.net */ - -/* A Class to describe an - LSystem Rule */ - -class Rule { - char a; - String b; - - Rule(char a_, String b_) { - a = a_; - b = b_; - } - - char getA() { - return a; - } - - String getB() { - return b; - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/lsys/Turtle.pde b/java/examples/Books/Nature of Code/chp8_fractals/lsys/Turtle.pde deleted file mode 100644 index ac0ffa8d9..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/lsys/Turtle.pde +++ /dev/null @@ -1,53 +0,0 @@ -/* Daniel Shiffman */ -/* http://www.shiffman.net */ - -class Turtle { - - String todo; - float len; - float theta; - - Turtle(String s, float l, float t) { - todo = s; - len = l; - theta = t; - } - - void render() { - stroke(0); - for (int i = 0; i < todo.length(); i++) { - char c = todo.charAt(i); - if (c == 'F' || c == 'G') { - line(0,0,len,0); - translate(len,0); - } - else if (c == '+') { - rotate(theta); - } - else if (c == '-') { - rotate(-theta); - } - else if (c == '[') { - pushMatrix(); - } - else if (c == ']') { - popMatrix(); - } - } - } - - void setLen(float l) { - len = l; - } - - void changeLen(float percent) { - len *= percent; - } - - void setToDo(String s) { - todo = s; - } - -} - - diff --git a/java/examples/Books/Nature of Code/chp8_fractals/lsys/lsys.pde b/java/examples/Books/Nature of Code/chp8_fractals/lsys/lsys.pde deleted file mode 100644 index 8ed2d08bd..000000000 --- a/java/examples/Books/Nature of Code/chp8_fractals/lsys/lsys.pde +++ /dev/null @@ -1,56 +0,0 @@ -/* Daniel Shiffman */ -/* The Nature of Code */ -/* http://www.shiffman.net */ -/* daniel.shiffman@nyu.edu */ - -LSystem lsys; -Turtle turtle; - -void setup() { - size(600, 600); - /* - // Create an empty ruleset - Rule[] ruleset = new Rule[2]; - // Fill with two rules (These are rules for the Sierpinksi Gasket Triangle) - ruleset[0] = new Rule('F',"F--F--F--G"); - ruleset[1] = new Rule('G',"GG"); - // Create LSystem with axiom and ruleset - lsys = new LSystem("F--F--F",ruleset); - turtle = new Turtle(lsys.getSentence(),width*2,TWO_PI/3); - */ - - /*Rule[] ruleset = new Rule[1]; - //ruleset[0] = new Rule('F',"F[F]-F+F[--F]+F-F"); - ruleset[0] = new Rule['F',"FF+[+F-F-F]-[-F+F+F]"); - lsys = new LSystem("F-F-F-F",ruleset); - turtle = new Turtle(lsys.getSentence(),width-1,PI/2); - */ - - Rule[] ruleset = new Rule[1]; - ruleset[0] = new Rule('F', "FF+[+F-F-F]-[-F+F+F]"); - lsys = new LSystem("F", ruleset); - turtle = new Turtle(lsys.getSentence(), width/4, radians(25)); - - - - smooth(); -} - -void draw() { - background(255); - fill(0); - text("Click mouse to generate", 10, height-20); - - translate(width/2, height); - rotate(-PI/2); - turtle.render(); - noLoop(); -} - -void mousePressed() { - lsys.generate(); - turtle.setToDo(lsys.getSentence()); - turtle.changeLen(0.5); - redraw(); -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/DNA.pde b/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/DNA.pde deleted file mode 100644 index 9e7ce4bc8..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/DNA.pde +++ /dev/null @@ -1,51 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// DNA is an array of vectors - -class DNA { - - // The genetic sequence - PVector[] genes; - - // Constructor (makes a DNA of random PVectors) - DNA(int num) { - genes = new PVector[num]; - for (int i = 0; i < genes.length; i++) { - float angle = random(TWO_PI); - genes[i] = new PVector(cos(angle),sin(angle)); - } - } - - // Constructor #2, creates the instance based on an existing array - DNA(PVector[] newgenes) { - // We could make a copy if necessary - // genes = (PVector []) newgenes.clone(); - genes = newgenes; - } - - // CROSSOVER - // Creates new DNA sequence from two (this & and a partner) - DNA crossover(DNA partner) { - PVector[] child = new PVector[genes.length]; - // Pick a midpoint - int crossover = int(random(genes.length)); - // Take "half" from one and "half" from the other - for (int i = 0; i < genes.length; i++) { - if (i > crossover) child[i] = genes[i]; - else child[i] = partner.genes[i]; - } - DNA newgenes = new DNA(child); - return newgenes; - } - - // Based on a mutation probability, picks a new random Vector - void mutate(float m) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < m) { - float angle = random(TWO_PI); - genes[i] = new PVector(cos(angle),sin(angle)); - } - } - } -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/EvolveFlowField.pde b/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/EvolveFlowField.pde deleted file mode 100644 index ce43c9f67..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/EvolveFlowField.pde +++ /dev/null @@ -1,100 +0,0 @@ -// Pathfinding Flowfield w/ Genetic Algorithms -// Daniel Shiffman - -// This example produces an obstacle course with a start and finish -// Virtual "creatures" are rewarded for making it closer to the finish - -// Each creature's DNA is a "flowfield" of PVectors that -// determine steering vectors for each cell on the screen - -int gridscale = 24; // Scale of grid is 1/24 of screen size - -// DNA needs one vector for every spot on the grid -// (it's like a pixel array, but with vectors instead of colors) -int dnasize; - -int lifetime; // How long should each generation live - -// Global maxforce and maxspeed (hmmm, could make this part of DNA??) -float maxspeed = 4.0; -float maxforce = 1.0; - -Population population; // Population -int lifecycle; // Timer for cycle of generation -int recordtime; // Fastest time to target -Obstacle target; // Target location -Obstacle start; // Start location -int diam = 24; // Size of target - -ArrayList obstacles; //an array list to keep track of all the obstacles! - -void setup() { - size(640,480); - smooth(); - - dnasize = (width / gridscale) * (height / gridscale); - lifetime = width/2; - - // Initialize variables - lifecycle = 0; - recordtime = lifetime; - target = new Obstacle(width-diam-diam/2,height/2-diam/2,diam,diam); - start = new Obstacle(diam/2,height/2-diam/2,diam,diam); - - // Create a population with a mutation rate, and population max - int popmax = 1000; - float mutationRate = 0.05; - population = new Population(mutationRate,popmax); - - // Create the obstacle course - obstacles = new ArrayList(); - obstacles.add(new Obstacle(width/4,40,10,height-80)); - obstacles.add(new Obstacle(width/2,0,10,height/2-10)); - obstacles.add(new Obstacle(width/2,height-height/2+10,10,height/2-10)); - obstacles.add(new Obstacle(2*width/3,height/2-height/8,10,height/4)); -} - -void draw() { - background(255); - - // Draw the start and target locations - start.display(); - target.display(); - - // Draw the obstacles - for (Obstacle obs : obstacles) { - obs.display(); - } - - - // If the generation hasn't ended yet - if (lifecycle < lifetime) { - population.live(obstacles); - if ((population.targetReached()) && (lifecycle < recordtime)) { - recordtime = lifecycle; - } - lifecycle++; - // Otherwise a new generation - } else { - lifecycle = 0; - population.calcFitness(); - population.naturalSelection(); - population.generate(); - } - - // Display some info - textAlign(RIGHT); - fill(0); - text("Generation #:" + population.getGenerations(),width-10,18); - text("Cycles left:" + ((lifetime-lifecycle)/10),width-10,36); - text("Record cycles: " + recordtime,width-10,54); - -} - -// Move the target if the mouse is pressed -// System will adapt to new target -void mousePressed() { - target = new Obstacle(mouseX,mouseY,diam,diam); - recordtime = lifetime; -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Obstacle.pde b/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Obstacle.pde deleted file mode 100644 index 250edc4cd..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Obstacle.pde +++ /dev/null @@ -1,34 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// A class for an obstacle, just a simple rectangle that is drawn -// and can check if a creature touches it - -// Also using this class for starting point and target location - -import java.awt.Rectangle; - -class Obstacle { - - Rectangle r; - - Obstacle(int x, int y, int w, int h) { - r = new Rectangle(x,y,w,h); - } - - void display() { - stroke(0); - fill(175); - rectMode(CORNER); - rect(r.x,r.y,r.width,r.height); - } - - boolean contains(PVector spot) { - if (r.contains((int)spot.x,(int)spot.y)) { - return true; - } else { - return false; - } - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Population.pde b/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Population.pde deleted file mode 100644 index a5b283ea6..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Population.pde +++ /dev/null @@ -1,115 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// A class to describe a population of "creatures" - -class Population { - - float mutationRate; // Mutation rate - Rocket[] population; // Array to hold the current population - ArrayList darwin; // ArrayList which we will use for our "mating pool" - int generations; // Number of generations - - int order; // Keep track of the order of creature's finishing the maze - - // Initialize the population - Population(float m, int num) { - mutationRate = m; - population = new Rocket[num]; - darwin = new ArrayList(); - generations = 0; - //make a new set of creatures - for (int i = 0; i < population.length; i++) { - PVector location = new PVector(start.r.x+start.r.width/2,start.r.y+start.r.height/2); - population[i] = new Rocket(location, new DNA(dnasize)); - } - order = 1; // The first one to finish will be #1 - } - - void live (ArrayList o) { - // For every creature - for (int i = 0; i < population.length; i++) { - // If it finishes, mark it down as done! - if ((population[i].finished()) && (!population[i].stopped())) { - population[i].setFinish(order); - order++; - } - // Run it - population[i].run(o); - } - } - - // Did anything finish? - boolean targetReached() { - for (int i = 0; i < population.length; i++) { - if (population[i].finished()) return true; - } - return false; - } - - // Calculate fitness for each creature - void calcFitness() { - for (int i = 0; i < population.length; i++) { - population[i].calcFitness(); - } - order = 1; // Hmmm, awkward place for this, we have to reset this for the next generation - } - - // Generate a mating pool - void naturalSelection() { - // Clear the ArrayList - darwin.clear(); - - // Calculate total fitness of whole population - float totalFitness = getTotalFitness(); - - // Calculate normalized fitness for each member of the population - // Based on normalized fitness, each member will get added to the mating pool a certain number of times a la roulette wheel - // A higher fitness = more entries to mating pool = more likely to be picked as a parent - // A lower fitness = fewer entries to mating pool = less likely to be picked as a parent - for (int i = 0; i < population.length; i++) { - float fitnessNormal = population[i].getFitness() / totalFitness; - int n = (int) (fitnessNormal * 50000); // Arbitrary multiplier, consider mapping fix - for (int j = 0; j < n; j++) { - darwin.add(population[i]); - } - } - } - - // Making the next generation - void generate() { - // Refill the population with children from the mating pool - for (int i = 0; i < population.length; i++) { - int m = int(random(darwin.size())); - int d = int(random(darwin.size())); - // Pick two parents - Rocket mom = darwin.get(m); - Rocket dad = darwin.get(d); - // Get their genes - DNA momgenes = mom.getDNA(); - DNA dadgenes = dad.getDNA(); - // Mate their genes - DNA child = momgenes.crossover(dadgenes); - // Mutate their genes - child.mutate(mutationRate); - // Fill the new population with the new child - PVector location = new PVector(start.r.x+start.r.width/2,start.r.y+start.r.height/2); - population[i] = new Rocket(location, child); - } - generations++; - } - - int getGenerations() { - return generations; - } - - //compute total fitness for the population - float getTotalFitness() { - float total = 0; - for (int i = 0; i < population.length; i++) { - total += population[i].getFitness(); - } - return total; - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Rocket.pde b/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Rocket.pde deleted file mode 100644 index 1327ee928..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/EvolveFlowField/Rocket.pde +++ /dev/null @@ -1,149 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// Rocket class -- this is just like our Boid / Particle class -// the only difference is that it has DNA & fitness - -class Rocket { - - // All of our physics stuff - PVector location; - PVector velocity; - PVector acceleration; - float r; - float recordDist; - - float fitness; - DNA dna; - - boolean stopped; // Am I stuck? - int finish; // What was my finish? (first, second, etc. . . ) - - //constructor - Rocket(PVector l, DNA dna_) { - acceleration = new PVector(); - velocity = new PVector(); - location = l.get(); - r = 2; - dna = dna_; - stopped = false; - finish = 100000; // Some high number to begin with - recordDist = width; - } - - // FITNESS FUNCTION - // distance = distance from target - // finish = what order did i finish (first, second, etc. . .) - // f(distance,finish) = (1.0f / finish^1.5) * (1.0f / distance^6); - // a lower finish is rewarded (exponentially) and/or shorter distance to target (exponetially) - void calcFitness() { - float d = recordDist; - if (d < diam/2) { - d = 1.0; - } - // Reward finishing faster and getting closer - fitness = (1.0f / pow(finish,1.5)) * (1 / (pow(d,6))); - } - - void setFinish(int f) { - finish = f; - } - - // Run in relation to all the obstacles - // If I'm stuck, don't bother updating or checking for intersection - void run(ArrayList o) { - if (!stopped) { - update(); - // If I hit an edge or an obstacle - if ((borders()) || (obstacles(o))) { - stopped = true; - } - } - // Draw me! - display(); - } - - // Did I hit an edge? - boolean borders() { - if ((location.x < 0) || (location.y < 0) || (location.x > width) || (location.y > height)) { - return true; - } else { - return false; - } - } - - // Did I make it to the target? - boolean finished() { - float d = dist(location.x,location.y,target.r.x,target.r.y); - if (d < recordDist) recordDist = d; - if (target.contains(location)) { - stopped = true; - return true; - } - return false; - } - - // Did I hit an obstacle? - boolean obstacles(ArrayList o) { - for (Obstacle obs : o) { - if (obs.contains(location)) { - return true; - } - } - return false; - } - - void update() { - if (!finished()) { - // Where are we? Our location will tell us what steering vector to look up in our DNA; - int x = (int) location.x/gridscale; - int y = (int) location.y/gridscale; - x = constrain(x,0,width/gridscale-1); // Make sure we are not off the edge - y = constrain(y,0,height/gridscale-1); // Make sure we are not off the edge - - // Get the steering vector out of our genes in the right spot - // We could do (desired - velocity) to be more in line with the Reynolds flow field following - acceleration.add(dna.genes[x+y*width/gridscale]); - - // This is all the same stuff we've done before - acceleration.mult(maxforce); - velocity.add(acceleration); - velocity.limit(maxspeed); - location.add(velocity); - acceleration.mult(0); - } - } - - void display() { - //fill(0,150); - //stroke(0); - //ellipse(location.x,location.y,r,r); - float theta = velocity.heading2D() + PI/2; - fill(200,100); - stroke(0); - pushMatrix(); - translate(location.x,location.y); - rotate(theta); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - popMatrix(); - - - } - - float getFitness() { - return fitness; - } - - DNA getDNA() { - return dna; - } - - boolean stopped() { - return stopped; - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/DNA.pde b/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/DNA.pde deleted file mode 100644 index 516c3f98d..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/DNA.pde +++ /dev/null @@ -1,67 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// A class to describe a psuedo-DNA, i.e. genotype -// Here, a virtual organism's DNA is an array of character. -// Functionality: -// -- convert DNA into a string -// -- calculate DNA's "fitness" -// -- mate DNA with another set of DNA -// -- mutate DNA - - -class DNA { - - // The genetic sequence - char[] genes; - - float fitness; - - // Constructor (makes a random DNA) - DNA(int num) { - genes = new char[num]; - for (int i = 0; i < genes.length; i++) { - genes[i] = (char) random(32,128); // Pick from range of chars - } - } - - // Converts character array to a String - String getPhrase() { - return new String(genes); - } - - // Fitness function (returns floating point % of "correct" characters) - void fitness (String target) { - int score = 0; - for (int i = 0; i < genes.length; i++) { - if (genes[i] == target.charAt(i)) { - score++; - } - } - fitness = pow(2,score); - } - - // Crossover - DNA crossover(DNA partner) { - // A new child - DNA child = new DNA(genes.length); - - int midpoint = int(random(genes.length)); // Pick a midpoint - - // Half from one, half from the other - for (int i = 0; i < genes.length; i++) { - if (i > midpoint) child.genes[i] = genes[i]; - else child.genes[i] = partner.genes[i]; - } - return child; - } - - // Based on a mutation probability, picks a new random character - void mutate(float mutationRate) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < mutationRate) { - genes[i] = (char) random(32,128); - } - } - } -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/GA_Shakespeare_fancyfitness.pde b/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/GA_Shakespeare_fancyfitness.pde deleted file mode 100644 index b739fa570..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/GA_Shakespeare_fancyfitness.pde +++ /dev/null @@ -1,86 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// Demonstration of using a genetic algorithm to perform a search - -// setup() -// # Step 1: The populationation -// # Create an empty populationation (an array or ArrayList) -// # Fill it with DNA encoded objects (pick random values to start) - -// draw() -// # Step 1: Selection -// # Create an empty mating pool (an empty ArrayList) -// # For every member of the populationation, evaluate its fitness based on some criteria / function, -// and add it to the mating pool in a manner consistant with its fitness, i.e. the more fit it -// is the more times it appears in the mating pool, in order to be more likely picked for reproduction. - -// # Step 2: Reproduction Create a new empty populationation -// # Fill the new populationation by executing the following steps: -// 1. Pick two "parent" objects from the mating pool. -// 2. Crossover -- create a "child" object by mating these two parents. -// 3. Mutation -- mutate the child's DNA based on a given probability. -// 4. Add the child object to the new populationation. -// # Replace the old populationation with the new populationation -// -// # Rinse and repeat - - -PFont f; -String target; -int popmax; -float mutationRate; -Population population; - -void setup() { - size(600, 200); - f = createFont("Courier", 32, true); - target = "To be or not to be."; - popmax = 150; - mutationRate = 0.01; - - // Create a populationation with a target phrase, mutation rate, and populationation max - population = new Population(target, mutationRate, popmax); -} - -void draw() { - // Generate mating pool - population.naturalSelection(); - //Create next generation - population.generate(); - // Calculate fitness - population.calcFitness(); - displayInfo(); - - // If we found the target phrase, stop - if (population.finished()) { - println(millis()/1000.0); - noLoop(); - } -} - -void displayInfo() { - background(255); - // Display current status of populationation - String answer = population.getBest(); - textFont(f); - textAlign(LEFT); - fill(0); - - - textSize(16); - text("Best phrase:",20,30); - textSize(32); - text(answer, 20, 75); - - textSize(12); - text("total generations: " + population.getGenerations(), 20, 140); - text("average fitness: " + nf(population.getAverageFitness(), 0, 2), 20, 155); - text("total populationation: " + popmax, 20, 170); - text("mutation rate: " + int(mutationRate * 100) + "%", 20, 185); - - textSize(10); - text("All phrases:\n" + population.allPhrases(), 450, 10); -} - - diff --git a/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/Population.pde b/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/Population.pde deleted file mode 100644 index e8dbe67cd..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/GA_Shakespeare_fancyfitness/Population.pde +++ /dev/null @@ -1,127 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// A class to describe a population of virtual organisms -// In this case, each organism is just an instance of a DNA object - - - - -class Population { - - float mutationRate; // Mutation rate - DNA[] population; // Array to hold the current population - ArrayList matingPool; // ArrayList which we will use for our "mating pool" - String target; // Target phrase - int generations; // Number of generations - boolean finished; // Are we finished evolving? -int perfectScore; - - Population(String p, float m, int num) { - target = p; - mutationRate = m; - population = new DNA[num]; - for (int i = 0; i < population.length; i++) { - population[i] = new DNA(target.length()); - } - calcFitness(); - matingPool = new ArrayList(); - finished = false; - generations = 0; - - perfectScore = int(pow(2,target.length())); - } - - // Fill our fitness array with a value for every member of the population - void calcFitness() { - for (int i = 0; i < population.length; i++) { - population[i].fitness(target); - } - } - - // Generate a mating pool - void naturalSelection() { - // Clear the ArrayList - matingPool.clear(); - - float maxFitness = 0; - for (int i = 0; i < population.length; i++) { - if (population[i].fitness > maxFitness) { - maxFitness = population[i].fitness; - } - } - - // Based on fitness, each member will get added to the mating pool a certain number of times - // a higher fitness = more entries to mating pool = more likely to be picked as a parent - // a lower fitness = fewer entries to mating pool = less likely to be picked as a parent - for (int i = 0; i < population.length; i++) { - - float fitness = map(population[i].fitness,0,maxFitness,0,1); - int n = int(fitness * 100); // Arbitrary multiplier, we can also use monte carlo method - for (int j = 0; j < n; j++) { // and pick two random numbers - matingPool.add(population[i]); - } - } - } - - // Create a new generation - void generate() { - // Refill the population with children from the mating pool - for (int i = 0; i < population.length; i++) { - int a = int(random(matingPool.size())); - int b = int(random(matingPool.size())); - DNA partnerA = matingPool.get(a); - DNA partnerB = matingPool.get(b); - DNA child = partnerA.crossover(partnerB); - child.mutate(mutationRate); - population[i] = child; - } - generations++; - } - - - // Compute the current "most fit" member of the population - String getBest() { - float worldrecord = 0.0f; - int index = 0; - for (int i = 0; i < population.length; i++) { - if (population[i].fitness > worldrecord) { - index = i; - worldrecord = population[i].fitness; - } - } - - if (worldrecord == perfectScore ) finished = true; - return population[index].getPhrase(); - } - - boolean finished() { - return finished; - } - - int getGenerations() { - return generations; - } - - // Compute average fitness for the population - float getAverageFitness() { - float total = 0; - for (int i = 0; i < population.length; i++) { - total += population[i].fitness; - } - return total / (population.length); - } - - String allPhrases() { - String everything = ""; - - int displayLimit = min(population.length,50); - - - for (int i = 0; i < displayLimit; i++) { - everything += population[i].getPhrase() + "\n"; - } - return everything; - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/DNA.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/DNA.pde deleted file mode 100644 index dd3e31563..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/DNA.pde +++ /dev/null @@ -1,67 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// A class to describe a psuedo-DNA, i.e. genotype -// Here, a virtual organism's DNA is an array of character. -// Functionality: -// -- convert DNA into a string -// -- calculate DNA's "fitness" -// -- mate DNA with another set of DNA -// -- mutate DNA - - -class DNA { - - // The genetic sequence - char[] genes; - - float fitness; - - // Constructor (makes a random DNA) - DNA(int num) { - genes = new char[num]; - for (int i = 0; i < genes.length; i++) { - genes[i] = (char) random(32,128); // Pick from range of chars - } - } - - // Converts character array to a String - String getPhrase() { - return new String(genes); - } - - // Fitness function (returns floating point % of "correct" characters) - void fitness (String target) { - int score = 0; - for (int i = 0; i < genes.length; i++) { - if (genes[i] == target.charAt(i)) { - score++; - } - } - fitness = (float)score / (float)target.length(); - } - - // Crossover - DNA crossover(DNA partner) { - // A new child - DNA child = new DNA(genes.length); - - int midpoint = int(random(genes.length)); // Pick a midpoint - - // Half from one, half from the other - for (int i = 0; i < genes.length; i++) { - if (i > midpoint) child.genes[i] = genes[i]; - else child.genes[i] = partner.genes[i]; - } - return child; - } - - // Based on a mutation probability, picks a new random character - void mutate(float mutationRate) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < mutationRate) { - genes[i] = (char) random(32,128); - } - } - } -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.pde deleted file mode 100644 index b8831e6c8..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/NOC_9_01_GA_Shakespeare.pde +++ /dev/null @@ -1,87 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// Demonstration of using a genetic algorithm to perform a search - -// setup() -// # Step 1: The Population -// # Create an empty population (an array or ArrayList) -// # Fill it with DNA encoded objects (pick random values to start) - -// draw() -// # Step 1: Selection -// # Create an empty mating pool (an empty ArrayList) -// # For every member of the population, evaluate its fitness based on some criteria / function, -// and add it to the mating pool in a manner consistant with its fitness, i.e. the more fit it -// is the more times it appears in the mating pool, in order to be more likely picked for reproduction. - -// # Step 2: Reproduction Create a new empty population -// # Fill the new population by executing the following steps: -// 1. Pick two "parent" objects from the mating pool. -// 2. Crossover -- create a "child" object by mating these two parents. -// 3. Mutation -- mutate the child's DNA based on a given probability. -// 4. Add the child object to the new population. -// # Replace the old population with the new population -// -// # Rinse and repeat - - -PFont f; -String target; -int popmax; -float mutationRate; -Population population; - -void setup() { - size(800, 200); - f = createFont("Courier", 32, true); - target = "To be or not to be."; - popmax = 150; - mutationRate = 0.01; - - // Create a populationation with a target phrase, mutation rate, and populationation max - population = new Population(target, mutationRate, popmax); -} - -void draw() { - // Generate mating pool - population.naturalSelection(); - //Create next generation - population.generate(); - // Calculate fitness - population.calcFitness(); - displayInfo(); - - // If we found the target phrase, stop - if (population.finished()) { - println(millis()/1000.0); - noLoop(); - } -} - -void displayInfo() { - background(255); - // Display current status of populationation - String answer = population.getBest(); - textFont(f); - textAlign(LEFT); - fill(0); - - - textSize(16); - text("Best phrase:",20,30); - textSize(32); - text(answer, 20, 75); - - textSize(12); - text("total generations: " + population.getGenerations(), 20, 140); - text("average fitness: " + nf(population.getAverageFitness(), 0, 2), 20, 155); - text("total populationation: " + popmax, 20, 170); - text("mutation rate: " + int(mutationRate * 100) + "%", 20, 185); - - textSize(10); - text("All phrases:\n" + population.allPhrases(), 650, 10); -} - - - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/Population.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/Population.pde deleted file mode 100644 index e8dbe67cd..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/Population.pde +++ /dev/null @@ -1,127 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// A class to describe a population of virtual organisms -// In this case, each organism is just an instance of a DNA object - - - - -class Population { - - float mutationRate; // Mutation rate - DNA[] population; // Array to hold the current population - ArrayList matingPool; // ArrayList which we will use for our "mating pool" - String target; // Target phrase - int generations; // Number of generations - boolean finished; // Are we finished evolving? -int perfectScore; - - Population(String p, float m, int num) { - target = p; - mutationRate = m; - population = new DNA[num]; - for (int i = 0; i < population.length; i++) { - population[i] = new DNA(target.length()); - } - calcFitness(); - matingPool = new ArrayList(); - finished = false; - generations = 0; - - perfectScore = int(pow(2,target.length())); - } - - // Fill our fitness array with a value for every member of the population - void calcFitness() { - for (int i = 0; i < population.length; i++) { - population[i].fitness(target); - } - } - - // Generate a mating pool - void naturalSelection() { - // Clear the ArrayList - matingPool.clear(); - - float maxFitness = 0; - for (int i = 0; i < population.length; i++) { - if (population[i].fitness > maxFitness) { - maxFitness = population[i].fitness; - } - } - - // Based on fitness, each member will get added to the mating pool a certain number of times - // a higher fitness = more entries to mating pool = more likely to be picked as a parent - // a lower fitness = fewer entries to mating pool = less likely to be picked as a parent - for (int i = 0; i < population.length; i++) { - - float fitness = map(population[i].fitness,0,maxFitness,0,1); - int n = int(fitness * 100); // Arbitrary multiplier, we can also use monte carlo method - for (int j = 0; j < n; j++) { // and pick two random numbers - matingPool.add(population[i]); - } - } - } - - // Create a new generation - void generate() { - // Refill the population with children from the mating pool - for (int i = 0; i < population.length; i++) { - int a = int(random(matingPool.size())); - int b = int(random(matingPool.size())); - DNA partnerA = matingPool.get(a); - DNA partnerB = matingPool.get(b); - DNA child = partnerA.crossover(partnerB); - child.mutate(mutationRate); - population[i] = child; - } - generations++; - } - - - // Compute the current "most fit" member of the population - String getBest() { - float worldrecord = 0.0f; - int index = 0; - for (int i = 0; i < population.length; i++) { - if (population[i].fitness > worldrecord) { - index = i; - worldrecord = population[i].fitness; - } - } - - if (worldrecord == perfectScore ) finished = true; - return population[index].getPhrase(); - } - - boolean finished() { - return finished; - } - - int getGenerations() { - return generations; - } - - // Compute average fitness for the population - float getAverageFitness() { - float total = 0; - for (int i = 0; i < population.length; i++) { - total += population[i].fitness; - } - return total / (population.length); - } - - String allPhrases() { - String everything = ""; - - int displayLimit = min(population.length,50); - - - for (int i = 0; i < displayLimit; i++) { - everything += population[i].getPhrase() + "\n"; - } - return everything; - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/sketch.properties b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/sketch.properties deleted file mode 100644 index b3cbe600e..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare/sketch.properties +++ /dev/null @@ -1,2 +0,0 @@ -mode.id=processing.mode.java.JavaMode -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare_simplified/DNA.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare_simplified/DNA.pde deleted file mode 100644 index faa7c3ebf..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare_simplified/DNA.pde +++ /dev/null @@ -1,67 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// A class to describe a psuedo-DNA, i.e. genotype -// Here, a virtual organism's DNA is an array of character. -// Functionality: -// -- convert DNA into a string -// -- calculate DNA's "fitness" -// -- mate DNA with another set of DNA -// -- mutate DNA - - -class DNA { - - // The genetic sequence - char[] genes; - - float fitness; - - // Constructor (makes a random DNA) - DNA(int num) { - genes = new char[num]; - for (int i = 0; i < genes.length; i++) { - genes[i] = (char) random(32,128); // Pick from range of chars - } - } - - // Converts character array to a String - String getPhrase() { - return new String(genes); - } - - // Fitness function (returns floating point % of "correct" characters) - void calcFitness (String target) { - int score = 0; - for (int i = 0; i < genes.length; i++) { - if (genes[i] == target.charAt(i)) { - score++; - } - } - fitness = (float)score / (float)target.length(); - } - - // Crossover - DNA crossover(DNA partner) { - // A new child - DNA child = new DNA(genes.length); - - int midpoint = int(random(genes.length)); // Pick a midpoint - - // Half from one, half from the other - for (int i = 0; i < genes.length; i++) { - if (i > midpoint) child.genes[i] = genes[i]; - else child.genes[i] = partner.genes[i]; - } - return child; - } - - // Based on a mutation probability, picks a new random character - void mutate(float mutationRate) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < mutationRate) { - genes[i] = (char) random(32,128); - } - } - } -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare_simplified/NOC_9_01_GA_Shakespeare_simplified.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare_simplified/NOC_9_01_GA_Shakespeare_simplified.pde deleted file mode 100644 index f78e8237a..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_01_GA_Shakespeare_simplified/NOC_9_01_GA_Shakespeare_simplified.pde +++ /dev/null @@ -1,86 +0,0 @@ -// Genetic Algorithm, Evolving Shakespeare -// Daniel Shiffman - -// Demonstration of using a genetic algorithm to perform a search - -// setup() -// # Step 1: The Population -// # Create an empty population (an array or ArrayList) -// # Fill it with DNA encoded objects (pick random values to start) - -// draw() -// # Step 1: Selection -// # Create an empty mating pool (an empty ArrayList) -// # For every member of the population, evaluate its fitness based on some criteria / function, -// and add it to the mating pool in a manner consistant with its fitness, i.e. the more fit it -// is the more times it appears in the mating pool, in order to be more likely picked for reproduction. - -// # Step 2: Reproduction Create a new empty population -// # Fill the new population by executing the following steps: -// 1. Pick two "parent" objects from the mating pool. -// 2. Crossover -- create a "child" object by mating these two parents. -// 3. Mutation -- mutate the child's DNA based on a given probability. -// 4. Add the child object to the new population. -// # Replace the old population with the new population -// -// # Rinse and repeat - - -float mutationRate = 0.01; // Mutation rate -int totalPopulation = 150; // Total Population - -DNA[] population; // Array to hold the current population -ArrayList matingPool; // ArrayList which we will use for our "mating pool" -String target; // Target phrase - -PFont f; - -void setup() { - size(800, 200); - target = "To be or not to be."; - - population = new DNA[totalPopulation]; - - for (int i = 0; i < population.length; i++) { - population[i] = new DNA(target.length()); - } - - f = createFont("Courier",12,true); -} - -void draw() { - for (int i = 0; i < population.length; i++) { - population[i].calcFitness(target); - } - - ArrayList matingPool = new ArrayList(); // ArrayList which we will use for our "mating pool" - - for (int i = 0; i < population.length; i++) { - int nnnn = int(population[i].fitness * 100); // Arbitrary multiplier, we can also use monte carlo method - for (int j = 0; j - -// DNA is an array of vectors - -class DNA { - - // The genetic sequence - PVector[] genes; - - // The maximum strength of the forces - float maxforce = 0.1; - - // Constructor (makes a DNA of random PVectors) - DNA() { - genes = new PVector[lifetime]; - for (int i = 0; i < genes.length; i++) { - float angle = random(TWO_PI); - genes[i] = new PVector(cos(angle), sin(angle)); - genes[i].mult(random(0, maxforce)); - } - } - - // Constructor #2, creates the instance based on an existing array - DNA(PVector[] newgenes) { - // We could make a copy if necessary - // genes = (PVector []) newgenes.clone(); - genes = newgenes; - } - - // CROSSOVER - // Creates new DNA sequence from two (this & and a partner) - DNA crossover(DNA partner) { - PVector[] child = new PVector[genes.length]; - // Pick a midpoint - int crossover = int(random(genes.length)); - // Take "half" from one and "half" from the other - for (int i = 0; i < genes.length; i++) { - if (i > crossover) child[i] = genes[i]; - else child[i] = partner.genes[i]; - } - DNA newgenes = new DNA(child); - return newgenes; - } - - // Based on a mutation probability, picks a new random Vector - void mutate(float m) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < m) { - float angle = random(TWO_PI); - genes[i] = new PVector(cos(angle), sin(angle)); - genes[i].mult(random(0, maxforce)); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/NOC_9_02_SmartRockets_superbasic.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/NOC_9_02_SmartRockets_superbasic.pde deleted file mode 100644 index 5271ef73b..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/NOC_9_02_SmartRockets_superbasic.pde +++ /dev/null @@ -1,73 +0,0 @@ -// Smart Rockets w/ Genetic Algorithms -// Daniel Shiffman - -// Each Rocket's DNA is an array of PVectors -// Each PVector acts as a force for each frame of animation -// Imagine an booster on the end of the rocket that can point in any direction -// and fire at any strength every frame - -// The Rocket's fitness is a function of how close it gets to the target as well as how fast it gets there - -// This example is inspired by Jer Thorp's Smart Rockets -// http://www.blprnt.com/smartrockets/ - -int lifetime; // How long should each generation live - -Population population; // Population - -int lifeCounter; // Timer for cycle of generation - -PVector target; // Target location - -void setup() { - size(800, 200); - smooth(); - - // The number of cycles we will allow a generation to live - lifetime = 200; - - // Initialize variables - lifeCounter = 0; - - target = new PVector(width/2, 24); - - // Create a population with a mutation rate, and population max - float mutationRate = 0.01; - population = new Population(mutationRate, 50); - -} - -void draw() { - background(255); - - // Draw the start and target locations - fill(0); - ellipse(target.x,target.y,24,24); - - - // If the generation hasn't ended yet - if (lifeCounter < lifetime) { - population.live(); - lifeCounter++; - // Otherwise a new generation - } - else { - lifeCounter = 0; - population.fitness(); - population.selection(); - population.reproduction(); - } - - // Display some info - fill(0); - text("Generation #: " + population.getGenerations(), 10, 18); - text("Cycles left: " + (lifetime-lifeCounter), 10, 36); -} - -// Move the target if the mouse is pressed -// System will adapt to new target -void mousePressed() { - target.x = mouseX; - target.y = mouseY; -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/Population.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/Population.pde deleted file mode 100644 index 3a1cce530..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/Population.pde +++ /dev/null @@ -1,100 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// A class to describe a population of "creatures" - -class Population { - - float mutationRate; // Mutation rate - Rocket[] population; // Array to hold the current population - ArrayList matingPool; // ArrayList which we will use for our "mating pool" - int generations; // Number of generations - - // Initialize the population - Population(float m, int num) { - mutationRate = m; - population = new Rocket[num]; - matingPool = new ArrayList(); - generations = 0; - //make a new set of creatures - for (int i = 0; i < population.length; i++) { - PVector location = new PVector(width/2,height+20); - population[i] = new Rocket(location, new DNA()); - } - } - - void live () { - // Run every rocket - for (int i = 0; i < population.length; i++) { - population[i].run(); - } - } - - // Calculate fitness for each creature - void fitness() { - for (int i = 0; i < population.length; i++) { - population[i].fitness(); - } - } - - // Generate a mating pool - void selection() { - // Clear the ArrayList - matingPool.clear(); - - // Calculate total fitness of whole population - float maxFitness = getMaxFitness(); - - // Calculate fitness for each member of the population (scaled to value between 0 and 1) - // Based on fitness, each member will get added to the mating pool a certain number of times - // A higher fitness = more entries to mating pool = more likely to be picked as a parent - // A lower fitness = fewer entries to mating pool = less likely to be picked as a parent - for (int i = 0; i < population.length; i++) { - float fitnessNormal = map(population[i].getFitness(),0,maxFitness,0,1); - int n = (int) (fitnessNormal * 100); // Arbitrary multiplier - for (int j = 0; j < n; j++) { - matingPool.add(population[i]); - } - } - } - - // Making the next generation - void reproduction() { - // Refill the population with children from the mating pool - for (int i = 0; i < population.length; i++) { - // Sping the wheel of fortune to pick two parents - int m = int(random(matingPool.size())); - int d = int(random(matingPool.size())); - // Pick two parents - Rocket mom = matingPool.get(m); - Rocket dad = matingPool.get(d); - // Get their genes - DNA momgenes = mom.getDNA(); - DNA dadgenes = dad.getDNA(); - // Mate their genes - DNA child = momgenes.crossover(dadgenes); - // Mutate their genes - child.mutate(mutationRate); - // Fill the new population with the new child - PVector location = new PVector(width/2,height+20); - population[i] = new Rocket(location, child); - } - generations++; - } - - int getGenerations() { - return generations; - } - - // Find highest fintess for the population - float getMaxFitness() { - float record = 0; - for (int i = 0; i < population.length; i++) { - if(population[i].getFitness() > record) { - record = population[i].getFitness(); - } - } - return record; - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/Rocket.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/Rocket.pde deleted file mode 100644 index e9a77323a..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_02_SmartRockets_superbasic/Rocket.pde +++ /dev/null @@ -1,105 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// Rocket class -- this is just like our Boid / Particle class -// the only difference is that it has DNA & fitness - -class Rocket { - - // All of our physics stuff - PVector location; - PVector velocity; - PVector acceleration; - - // Size - float r; - - // Fitness and DNA - float fitness; - DNA dna; - // To count which force we're on in the genes - int geneCounter = 0; - - boolean hitTarget = false; // Did I reach the target - - //constructor - Rocket(PVector l, DNA dna_) { - acceleration = new PVector(); - velocity = new PVector(); - location = l.get(); - r = 4; - dna = dna_; - } - - // Fitness function - // fitness = one divided by distance squared - void fitness() { - float d = dist(location.x, location.y, target.x, target.y); - fitness = pow(1/d, 2); - } - - // Run in relation to all the obstacles - // If I'm stuck, don't bother updating or checking for intersection - void run() { - checkTarget(); // Check to see if we've reached the target - if (!hitTarget) { - applyForce(dna.genes[geneCounter]); - geneCounter = (geneCounter + 1) % dna.genes.length; - update(); - } - display(); - } - - // Did I make it to the target? - void checkTarget() { - float d = dist(location.x, location.y, target.x, target.y); - if (d < 12) { - hitTarget = true; - } - } - - void applyForce(PVector f) { - acceleration.add(f); - } - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - float theta = velocity.heading2D() + PI/2; - fill(200, 100); - stroke(0); - pushMatrix(); - translate(location.x, location.y); - rotate(theta); - - // Thrusters - rectMode(CENTER); - fill(0); - rect(-r/2, r*2, r/2, r); - rect(r/2, r*2, r/2, r); - - // Rocket body - fill(175); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - - popMatrix(); - } - - float getFitness() { - return fitness; - } - - DNA getDNA() { - return dna; - } - -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/DNA.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/DNA.pde deleted file mode 100644 index 6243526f1..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/DNA.pde +++ /dev/null @@ -1,65 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// DNA is an array of vectors - -class DNA { - - // The genetic sequence - PVector[] genes; - - // The maximum strength of the forces - float maxforce = 0.1; - - // Constructor (makes a DNA of random PVectors) - DNA() { - genes = new PVector[lifetime]; - for (int i = 0; i < genes.length; i++) { - float angle = random(TWO_PI); - genes[i] = new PVector(cos(angle), sin(angle)); - genes[i].mult(random(0, maxforce)); - } - - // Let's give each Rocket an extra boost of strength for its first frame - genes[0].normalize(); - } - - // Constructor #2, creates the instance based on an existing array - DNA(PVector[] newgenes) { - // We could make a copy if necessary - // genes = (PVector []) newgenes.clone(); - genes = newgenes; - } - - // CROSSOVER - // Creates new DNA sequence from two (this & and a partner) - DNA crossover(DNA partner) { - PVector[] child = new PVector[genes.length]; - // Pick a midpoint - int crossover = int(random(genes.length)); - // Take "half" from one and "half" from the other - for (int i = 0; i < genes.length; i++) { - if (i > crossover) child[i] = genes[i]; - else child[i] = partner.genes[i]; - } - DNA newgenes = new DNA(child); - return newgenes; - } - - // Based on a mutation probability, picks a new random Vector - void mutate(float m) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < m) { - float angle = random(TWO_PI); - genes[i] = new PVector(cos(angle), sin(angle)); - genes[i].mult(random(0, maxforce)); - // float angle = random(-0.1,0.1); - // genes[i].rotate(angle); - // float factor = random(0.9,1.1); - // genes[i].mult(factor); - if (i ==0) genes[i].normalize(); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/NOC_9_03_SmartRockets.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/NOC_9_03_SmartRockets.pde deleted file mode 100644 index 46f7d6df4..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/NOC_9_03_SmartRockets.pde +++ /dev/null @@ -1,93 +0,0 @@ -// Smart Rockets w/ Genetic Algorithms -// Daniel Shiffman - -// Each Rocket's DNA is an array of PVectors -// Each PVector acts as a force for each frame of animation -// Imagine an booster on the end of the rocket that can point in any direction -// and fire at any strength every frame - -// The Rocket's fitness is a function of how close it gets to the target as well as how fast it gets there - -// This example is inspired by Jer Thorp's Smart Rockets -// http://www.blprnt.com/smartrockets/ - -int lifetime; // How long should each generation live - -Population population; // Population - -int lifecycle; // Timer for cycle of generation -int recordtime; // Fastest time to target - -Obstacle target; // Target location - -//int diam = 24; // Size of target - -ArrayList obstacles; //an array list to keep track of all the obstacles! - -void setup() { - size(800, 200); - smooth(); - - // The number of cycles we will allow a generation to live - lifetime = 300; - - // Initialize variables - lifecycle = 0; - recordtime = lifetime; - - target = new Obstacle(width/2-12, 24, 24, 24); - - // Create a population with a mutation rate, and population max - float mutationRate = 0.01; - population = new Population(mutationRate, 50); - - // Create the obstacle course - obstacles = new ArrayList(); - obstacles.add(new Obstacle(300, height/2, width-600, 10)); -} - -void draw() { - background(255); - - // Draw the start and target locations - target.display(); - - - // If the generation hasn't ended yet - if (lifecycle < lifetime) { - population.live(obstacles); - if ((population.targetReached()) && (lifecycle < recordtime)) { - recordtime = lifecycle; - } - lifecycle++; - // Otherwise a new generation - } - else { - lifecycle = 0; - population.fitness(); - population.selection(); - population.reproduction(); - } - - // Draw the obstacles - for (Obstacle obs : obstacles) { - obs.display(); - } - - // Display some info - fill(0); - text("Generation #: " + population.getGenerations(), 10, 18); - text("Cycles left: " + (lifetime-lifecycle), 10, 36); - text("Record cycles: " + recordtime, 10, 54); - - -} - -// Move the target if the mouse is pressed -// System will adapt to new target -void mousePressed() { - target.location.x = mouseX; - target.location.y = mouseY; - recordtime = lifetime; -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Obstacle.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Obstacle.pde deleted file mode 100644 index e8d4207b6..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Obstacle.pde +++ /dev/null @@ -1,37 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// A class for an obstacle, just a simple rectangle that is drawn -// and can check if a Rocket touches it - -// Also using this class for target location - - -class Obstacle { - - PVector location; - float w,h; - - Obstacle(float x, float y, float w_, float h_) { - location = new PVector(x,y); - w = w_; - h = h_; - } - - void display() { - stroke(0); - fill(175); - strokeWeight(2); - rectMode(CORNER); - rect(location.x,location.y,w,h); - } - - boolean contains(PVector spot) { - if (spot.x > location.x && spot.x < location.x + w && spot.y > location.y && spot.y < location.y + h) { - return true; - } else { - return false; - } - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Population.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Population.pde deleted file mode 100644 index b5a8ccbed..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Population.pde +++ /dev/null @@ -1,110 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// A class to describe a population of "creatures" - -class Population { - - float mutationRate; // Mutation rate - Rocket[] population; // Array to hold the current population - ArrayList matingPool; // ArrayList which we will use for our "mating pool" - int generations; // Number of generations - - // Initialize the population - Population(float m, int num) { - mutationRate = m; - population = new Rocket[num]; - matingPool = new ArrayList(); - generations = 0; - //make a new set of creatures - for (int i = 0; i < population.length; i++) { - PVector location = new PVector(width/2,height+20); - population[i] = new Rocket(location, new DNA(),population.length); - } - } - - void live (ArrayList os) { - // For every creature - for (int i = 0; i < population.length; i++) { - // If it finishes, mark it down as done! - population[i].checkTarget(); - population[i].run(os); - } - } - - // Did anything finish? - boolean targetReached() { - for (int i = 0; i < population.length; i++) { - if (population[i].hitTarget) return true; - } - return false; - } - - // Calculate fitness for each creature - void fitness() { - for (int i = 0; i < population.length; i++) { - population[i].fitness(); - } - } - - // Generate a mating pool - void selection() { - // Clear the ArrayList - matingPool.clear(); - - // Calculate total fitness of whole population - float maxFitness = getMaxFitness(); - - // Calculate fitness for each member of the population (scaled to value between 0 and 1) - // Based on fitness, each member will get added to the mating pool a certain number of times - // A higher fitness = more entries to mating pool = more likely to be picked as a parent - // A lower fitness = fewer entries to mating pool = less likely to be picked as a parent - for (int i = 0; i < population.length; i++) { - float fitnessNormal = map(population[i].getFitness(),0,maxFitness,0,1); - int n = (int) (fitnessNormal * 100); // Arbitrary multiplier - for (int j = 0; j < n; j++) { - matingPool.add(population[i]); - } - } - } - - // Making the next generation - void reproduction() { - // Refill the population with children from the mating pool - for (int i = 0; i < population.length; i++) { - // Sping the wheel of fortune to pick two parents - int m = int(random(matingPool.size())); - int d = int(random(matingPool.size())); - // Pick two parents - Rocket mom = matingPool.get(m); - Rocket dad = matingPool.get(d); - // Get their genes - DNA momgenes = mom.getDNA(); - DNA dadgenes = dad.getDNA(); - // Mate their genes - DNA child = momgenes.crossover(dadgenes); - // Mutate their genes - child.mutate(mutationRate); - // Fill the new population with the new child - PVector location = new PVector(width/2,height+20); - population[i] = new Rocket(location, child,population.length); - } - generations++; - } - - int getGenerations() { - return generations; - } - - // Find highest fintess for the population - float getMaxFitness() { - float record = 0; - for (int i = 0; i < population.length; i++) { - if(population[i].getFitness() > record) { - record = population[i].getFitness(); - } - } - return record; - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Rocket.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Rocket.pde deleted file mode 100644 index 488b9ea9e..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/Rocket.pde +++ /dev/null @@ -1,147 +0,0 @@ -// Pathfinding w/ Genetic Algorithms -// Daniel Shiffman - -// Rocket class -- this is just like our Boid / Particle class -// the only difference is that it has DNA & fitness - -class Rocket { - - // All of our physics stuff - PVector location; - PVector velocity; - PVector acceleration; - - // Size - float r; - - // How close did it get to the target - float recordDist; - - // Fitness and DNA - float fitness; - DNA dna; - // To count which force we're on in the genes - int geneCounter = 0; - - boolean hitObstacle = false; // Am I stuck on an obstacle? - boolean hitTarget = false; // Did I reach the target - int finishTime; // What was my finish time? - - //constructor - Rocket(PVector l, DNA dna_, int totalRockets) { - acceleration = new PVector(); - velocity = new PVector(); - location = l.get(); - r = 4; - dna = dna_; - finishTime = 0; // We're going to count how long it takes to reach target - recordDist = 10000; // Some high number that will be beat instantly - } - - // FITNESS FUNCTION - // distance = distance from target - // finish = what order did i finish (first, second, etc. . .) - // f(distance,finish) = (1.0f / finish^1.5) * (1.0f / distance^6); - // a lower finish is rewarded (exponentially) and/or shorter distance to target (exponetially) - void fitness() { - if (recordDist < 1) recordDist = 1; - - // Reward finishing faster and getting close - fitness = (1/(finishTime*recordDist)); - - // Make the function exponential - fitness = pow(fitness, 4); - - if (hitObstacle) fitness *= 0.1; // lose 90% of fitness hitting an obstacle - if (hitTarget) fitness *= 2; // twice the fitness for finishing! - } - - // Run in relation to all the obstacles - // If I'm stuck, don't bother updating or checking for intersection - void run(ArrayList os) { - if (!hitObstacle && !hitTarget) { - applyForce(dna.genes[geneCounter]); - geneCounter = (geneCounter + 1) % dna.genes.length; - update(); - // If I hit an edge or an obstacle - obstacles(os); - } - // Draw me! - if (!hitObstacle) { - display(); - } - } - - // Did I make it to the target? - void checkTarget() { - float d = dist(location.x, location.y, target.location.x, target.location.y); - if (d < recordDist) recordDist = d; - - if (target.contains(location) && !hitTarget) { - hitTarget = true; - } - else if (!hitTarget) { - finishTime++; - } - } - - // Did I hit an obstacle? - void obstacles(ArrayList os) { - for (Obstacle obs : os) { - if (obs.contains(location)) { - hitObstacle = true; - } - } - } - - void applyForce(PVector f) { - acceleration.add(f); - } - - - void update() { - velocity.add(acceleration); - location.add(velocity); - acceleration.mult(0); - } - - void display() { - //background(255,0,0); - float theta = velocity.heading2D() + PI/2; - fill(200, 100); - stroke(0); - strokeWeight(1); - pushMatrix(); - translate(location.x, location.y); - rotate(theta); - - // Thrusters - rectMode(CENTER); - fill(0); - rect(-r/2, r*2, r/2, r); - rect(r/2, r*2, r/2, r); - - // Rocket body - fill(175); - beginShape(TRIANGLES); - vertex(0, -r*2); - vertex(-r, r*2); - vertex(r, r*2); - endShape(); - - popMatrix(); - } - - float getFitness() { - return fitness; - } - - DNA getDNA() { - return dna; - } - - boolean stopped() { - return hitObstacle; - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/sketch.properties b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_03_SmartRockets/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Button.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Button.pde deleted file mode 100644 index c7747749a..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Button.pde +++ /dev/null @@ -1,53 +0,0 @@ -// Interactive Selection -// http://www.genarts.com/karl/papers/siggraph91.html -// Daniel Shiffman - -//import java.awt.Rectangle; - -class Button { - Rectangle r; // Button's rectangle - String txt; // Button's text - boolean clickedOn; // Did i click on it? - boolean rolloverOn; // Did i rollover it? - - Button(int x, int y, int w, int h, String s) { - r = new Rectangle(x,y,w,h); - txt = s; - } - - void display() { - // Draw rectangle and text based on whether rollover or clicked - rectMode(CORNER); - stroke(0); noFill(); - if (rolloverOn) fill(0.5); - if (clickedOn) fill(0); - rect(r.x,r.y,r.width,r.height); - float b = 0.0; - if (clickedOn) b = 1; - else if (rolloverOn) b = 0.2; - else b = 0; - fill(b); - textAlign(LEFT); - text(txt,r.x+10,r.y+14); - - } - - - // Methods to check rollover, clicked, or released (must be called from appropriate - // Places in draw, mousePressed, mouseReleased - boolean rollover(int mx, int my) { - if (r.contains(mx,my)) rolloverOn = true; - else rolloverOn = false; - return rolloverOn; - } - - boolean clicked(int mx, int my) { - if (r.contains(mx,my)) clickedOn = true; - return clickedOn; - } - - void released() { - clickedOn = false; - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/DNA.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/DNA.pde deleted file mode 100644 index f5f986159..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/DNA.pde +++ /dev/null @@ -1,46 +0,0 @@ -// Interactive Selection -// http://www.genarts.com/karl/papers/siggraph91.html -// Daniel Shiffman - -class DNA { - - // The genetic sequence - float[] genes; - int len = 20; // Arbitrary length - - //Constructor (makes a random DNA) - DNA() { - // DNA is random floating point values between 0 and 1 (!!) - genes = new float[len]; - for (int i = 0; i < genes.length; i++) { - genes[i] = random(0,1); - } - } - - DNA(float[] newgenes) { - genes = newgenes; - } - - - // Crossover - // Creates new DNA sequence from two (this & - DNA crossover(DNA partner) { - float[] child = new float[genes.length]; - int crossover = int(random(genes.length)); - for (int i = 0; i < genes.length; i++) { - if (i > crossover) child[i] = genes[i]; - else child[i] = partner.genes[i]; - } - DNA newgenes = new DNA(child); - return newgenes; - } - - // Based on a mutation probability, picks a new random character in array spots - void mutate(float m) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < m) { - genes[i] = random(0,1); - } - } - } -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Face.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Face.pde deleted file mode 100644 index d95dbc7c2..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Face.pde +++ /dev/null @@ -1,101 +0,0 @@ -// Interactive Selection -// http://www.genarts.com/karl/papers/siggraph91.html -// Daniel Shiffman - -// The class for our "face", contains DNA sequence, fitness value, position on screen - -// Fitness Function f(t) = t (where t is "time" mouse rolls over face) - -class Face { - - DNA dna; // Face's DNA - float fitness; // How good is this face? - float x, y; // Position on screen - int wh = 70; // Size of square enclosing face - boolean rolloverOn; // Are we rolling over this face? - - Rectangle r; - - // Create a new face - Face(DNA dna_, float x_, float y_) { - dna = dna_; - x = x_; - y = y_; - fitness = 1; - // Using java.awt.Rectangle (see: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Rectangle.html) - r = new Rectangle(int(x-wh/2), int(y-wh/2), int(wh), int(wh)); - } - - // Display the face - void display() { - // We are using the face's DNA to pick properties for this face - // such as: head size, color, eye position, etc. - // Now, since every gene is a floating point between 0 and 1, we map the values - float r = map(dna.genes[0],0,1,0,70); - color c = color(dna.genes[1],dna.genes[2],dna.genes[3]); - float eye_y = map(dna.genes[4],0,1,0,5); - float eye_x = map(dna.genes[5],0,1,0,10); - float eye_size = map(dna.genes[5],0,1,0,10); - color eyecolor = color(dna.genes[4],dna.genes[5],dna.genes[6]); - color mouthColor = color(dna.genes[7],dna.genes[8],dna.genes[9]); - float mouth_y = map(dna.genes[5],0,1,0,25); - float mouth_x = map(dna.genes[5],0,1,-25,25); - float mouthw = map(dna.genes[5],0,1,0,50); - float mouthh = map(dna.genes[5],0,1,0,10); - - // Once we calculate all the above properties, we use those variables to draw rects, ellipses, etc. - pushMatrix(); - translate(x, y); - noStroke(); - - // Draw the head - smooth(); - fill(c); - ellipseMode(CENTER); - ellipse(0, 0, r, r); - - // Draw the eyes - fill(eyecolor); - rectMode(CENTER); - rect(-eye_x, -eye_y, eye_size, eye_size); - rect( eye_x, -eye_y, eye_size, eye_size); - - // Draw the mouth - fill(mouthColor); - rectMode(CENTER); - rect(mouth_x, mouth_y, mouthw, mouthh); - - // Draw the bounding box - stroke(0.25); - if (rolloverOn) fill(0, 0.25); - else noFill(); - rectMode(CENTER); - rect(0, 0, wh, wh); - popMatrix(); - - // Display fitness value - textAlign(CENTER); - if (rolloverOn) fill(0); - else fill(0.25); - text(int(fitness), x, y+55); - } - - float getFitness() { - return fitness; - } - - DNA getDNA() { - return dna; - } - - // Increment fitness if mouse is rolling over face - void rollover(int mx, int my) { - if (r.contains(mx, my)) { - rolloverOn = true; - fitness += 0.25; - } else { - rolloverOn = false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/NOC_9_04_Faces_interactiveselection.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/NOC_9_04_Faces_interactiveselection.pde deleted file mode 100644 index 77a1e5af9..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/NOC_9_04_Faces_interactiveselection.pde +++ /dev/null @@ -1,46 +0,0 @@ -// Interactive Selection -// http://www.genarts.com/karl/papers/siggraph91.html -// Daniel Shiffman - -Population population; -Button button; - -void setup() { - size(800,200); - colorMode(RGB,1.0); - smooth(); - int popmax = 10; - float mutationRate = 0.05; // A pretty high mutation rate here, our population is rather small we need to enforce variety - // Create a population with a target phrase, mutation rate, and population max - population = new Population(mutationRate,popmax); - // A simple button class - button = new Button(15,150,160,20, "evolve new generation"); -} - -void draw() { - background(1.0); - // Display the faces - population.display(); - population.rollover(mouseX,mouseY); - // Display some text - textAlign(LEFT); - fill(0); - text("Generation #:" + population.getGenerations(),15,190); - - // Display the button - button.display(); - button.rollover(mouseX,mouseY); - -} - -// If the button is clicked, evolve next generation -void mousePressed() { - if (button.clicked(mouseX,mouseY)) { - population.selection(); - population.reproduction(); - } -} - -void mouseReleased() { - button.released(); -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Population.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Population.pde deleted file mode 100644 index f4df91d2a..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Population.pde +++ /dev/null @@ -1,99 +0,0 @@ -// Interactive Selection -// http://www.genarts.com/karl/papers/siggraph91.html -// Daniel Shiffman - -// A class to describe a population of faces -// this hasn't changed very much from example to example - -class Population { - - float mutationRate; // Mutation rate - Face[] population; // array to hold the current population - ArrayList matingPool; // ArrayList which we will use for our "mating pool" - int generations; // Number of generations - - // Create the population - Population(float m, int num) { - mutationRate = m; - population = new Face[num]; - matingPool = new ArrayList(); - generations = 0; - for (int i = 0; i < population.length; i++) { - population[i] = new Face(new DNA(), 50+i*75, 60); - } - } - - // Display all faces - void display() { - for (int i = 0; i < population.length; i++) { - population[i].display(); - } - } - - // Are we rolling over any of the faces? - void rollover(int mx, int my) { - for (int i = 0; i < population.length; i++) { - population[i].rollover(mx, my); - } - } - - // Generate a mating pool - void selection() { - // Clear the ArrayList - matingPool.clear(); - - // Calculate total fitness of whole population - float maxFitness = getMaxFitness(); - - // Calculate fitness for each member of the population (scaled to value between 0 and 1) - // Based on fitness, each member will get added to the mating pool a certain number of times - // A higher fitness = more entries to mating pool = more likely to be picked as a parent - // A lower fitness = fewer entries to mating pool = less likely to be picked as a parent - for (int i = 0; i < population.length; i++) { - float fitnessNormal = map(population[i].getFitness(), 0, maxFitness, 0, 1); - int n = (int) (fitnessNormal * 100); // Arbitrary multiplier - for (int j = 0; j < n; j++) { - matingPool.add(population[i]); - } - } - } - - // Making the next generation - void reproduction() { - // Refill the population with children from the mating pool - for (int i = 0; i < population.length; i++) { - // Sping the wheel of fortune to pick two parents - int m = int(random(matingPool.size())); - int d = int(random(matingPool.size())); - // Pick two parents - Face mom = matingPool.get(m); - Face dad = matingPool.get(d); - // Get their genes - DNA momgenes = mom.getDNA(); - DNA dadgenes = dad.getDNA(); - // Mate their genes - DNA child = momgenes.crossover(dadgenes); - // Mutate their genes - child.mutate(mutationRate); - // Fill the new population with the new child - population[i] = new Face(child, 50+i*75, 60); - } - generations++; - } - - int getGenerations() { - return generations; - } - - // Find highest fintess for the population - float getMaxFitness() { - float record = 0; - for (int i = 0; i < population.length; i++) { - if (population[i].getFitness() > record) { - record = population[i].getFitness(); - } - } - return record; - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Rectangle.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Rectangle.pde deleted file mode 100644 index 7f6ad9691..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_04_Faces_interactiveselection/Rectangle.pde +++ /dev/null @@ -1,21 +0,0 @@ -// Re-implementing java.awt.Rectangle -// so JS mode works - -class Rectangle { - int x; - int y; - int width; - int height; - - Rectangle(int x_, int y_, int w, int h) { - x = x_; - y = y_; - width = w; - height = h; - } - - boolean contains(int px, int py) { - return (px > x && px < x + width && py > y && py < y + height); - } - -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/Bloop.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/Bloop.pde deleted file mode 100644 index a7f11cfd3..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/Bloop.pde +++ /dev/null @@ -1,105 +0,0 @@ -// Evolution EcoSystem -// Daniel Shiffman - -// Creature class - -class Bloop { - PVector location; // Location - DNA dna; // DNA - float health; // Life timer - float xoff; // For perlin noise - float yoff; - // DNA will determine size and maxspeed - float r; - float maxspeed; - - // Create a "bloop" creature - Bloop(PVector l, DNA dna_) { - location = l.get(); - health = 200; - xoff = random(1000); - yoff = random(1000); - dna = dna_; - // Gene 0 determines maxspeed and r - // The bigger the bloop, the slower it is - maxspeed = map(dna.genes[0], 0, 1, 15, 0); - r = map(dna.genes[0], 0, 1, 0, 50); - } - - void run() { - update(); - borders(); - display(); - } - - // A bloop can find food and eat it - void eat(Food f) { - ArrayList food = f.getFood(); - // Are we touching any food objects? - for (int i = food.size()-1; i >= 0; i--) { - PVector foodLocation = food.get(i); - float d = PVector.dist(location, foodLocation); - // If we are, juice up our strength! - if (d < r/2) { - health += 100; - food.remove(i); - } - } - } - - // At any moment there is a teeny, tiny chance a bloop will reproduce - Bloop reproduce() { - // asexual reproduction - if (random(1) < 0.0005) { - // Child is exact copy of single parent - DNA childDNA = dna.copy(); - // Child DNA can mutate - childDNA.mutate(0.01); - return new Bloop(location, childDNA); - } - else { - return null; - } - } - - // Method to update location - void update() { - // Simple movement based on perlin noise - float vx = map(noise(xoff),0,1,-maxspeed,maxspeed); - float vy = map(noise(yoff),0,1,-maxspeed,maxspeed); - PVector velocity = new PVector(vx,vy); - xoff += 0.01; - yoff += 0.01; - - location.add(velocity); - // Death always looming - health -= 0.2; - } - - // Wraparound - void borders() { - if (location.x < -r) location.x = width+r; - if (location.y < -r) location.y = height+r; - if (location.x > width+r) location.x = -r; - if (location.y > height+r) location.y = -r; - } - - // Method to display - void display() { - ellipseMode(CENTER); - stroke(0,health); - fill(0, health); - ellipse(location.x, location.y, r, r); - } - - // Death - boolean dead() { - if (health < 0.0) { - return true; - } - else { - return false; - } - } -} - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/DNA.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/DNA.pde deleted file mode 100644 index 807e776a7..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/DNA.pde +++ /dev/null @@ -1,44 +0,0 @@ -// Evolution EcoSystem -// Daniel Shiffman - -// Class to describe DNA -// Has more features for two parent mating (not used in this example) - -class DNA { - - // The genetic sequence - float[] genes; - - // Constructor (makes a random DNA) - DNA() { - // DNA is random floating point values between 0 and 1 (!!) - genes = new float[1]; - for (int i = 0; i < genes.length; i++) { - genes[i] = random(0,1); - } - } - - DNA(float[] newgenes) { - genes = newgenes; - } - - DNA copy() { - float[] newgenes = new float[genes.length]; - //arraycopy(genes,newgenes); - // JS mode not supporting arraycopy - for (int i = 0; i < newgenes.length; i++) { - newgenes[i] = genes[i]; - } - - return new DNA(newgenes); - } - - // Based on a mutation probability, picks a new random character in array spots - void mutate(float m) { - for (int i = 0; i < genes.length; i++) { - if (random(1) < m) { - genes[i] = random(0,1); - } - } - } -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/Food.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/Food.pde deleted file mode 100644 index e94bd5a0a..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/Food.pde +++ /dev/null @@ -1,41 +0,0 @@ -// Evolution EcoSystem -// Daniel Shiffman - -// A collection of food in the world - -class Food { - ArrayList food; - - Food(int num) { - // Start with some food - food = new ArrayList(); - for (int i = 0; i < num; i++) { - food.add(new PVector(random(width),random(height))); - } - } - - // Add some food at a location - void add(PVector l) { - food.add(l.get()); - } - - // Display the food - void run() { - for (PVector f : food) { - rectMode(CENTER); - stroke(0); - fill(175); - rect(f.x,f.y,8,8); - } - - // There's a small chance food will appear randomly - if (random(1) < 0.001) { - food.add(new PVector(random(width),random(height))); - } - } - - // Return the list of food - ArrayList getFood() { - return food; - } -} diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/NOC_9_05_EvolutionEcosystem.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/NOC_9_05_EvolutionEcosystem.pde deleted file mode 100644 index f92a22e37..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/NOC_9_05_EvolutionEcosystem.pde +++ /dev/null @@ -1,33 +0,0 @@ -// Evolution EcoSystem -// Daniel Shiffman -// The Nature of Code - -// A World of creatures that eat food -// The more they eat, the longer they survive -// The longer they survive, the more likely they are to reproduce -// The bigger they are, the easier it is to land on food -// The bigger they are, the slower they are to find food -// When the creatures die, food is left behind - - -World world; - -void setup() { - size(800, 200); - // World starts with 20 creatures - // and 20 pieces of food - world = new World(20); - smooth(); -} - -void draw() { - background(255); - world.run(); -} - -// We can add a creature manually if we so desire -void mousePressed() { - world.born(mouseX,mouseY); -} - - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/World.pde b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/World.pde deleted file mode 100644 index d3edc5dd8..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/World.pde +++ /dev/null @@ -1,56 +0,0 @@ -// Evolution EcoSystem -// Daniel Shiffman -// Spring 2007, The Nature of Code - -// The World we live in -// Has bloops and food - -class World { - - ArrayList bloops; // An arraylist for all the creatures - Food food; - - // Constructor - World(int num) { - // Start with initial food and creatures - food = new Food(num); - bloops = new ArrayList(); // Initialize the arraylist - for (int i = 0; i < num; i++) { - PVector l = new PVector(random(width),random(height)); - DNA dna = new DNA(); - bloops.add(new Bloop(l,dna)); - } - } - - // Make a new creature - void born(float x, float y) { - PVector l = new PVector(x,y); - DNA dna = new DNA(); - bloops.add(new Bloop(l,dna)); - } - - // Run the world - void run() { - // Deal with food - food.run(); - - // Cycle through the ArrayList backwards b/c we are deleting - for (int i = bloops.size()-1; i >= 0; i--) { - // All bloops run and eat - Bloop b = bloops.get(i); - b.run(); - b.eat(food); - // If it's dead, kill it and make food - if (b.dead()) { - bloops.remove(i); - food.add(b.location); - } - // Perhaps this bloop would like to make a baby? - Bloop child = b.reproduce(); - if (child != null) bloops.add(child); - } - } -} - - - diff --git a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/sketch.properties b/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/sketch.properties deleted file mode 100644 index 28faa5897..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/NOC_9_05_EvolutionEcosystem/sketch.properties +++ /dev/null @@ -1 +0,0 @@ -mode=Standard diff --git a/java/examples/Books/Nature of Code/chp9_ga/bruteforce/bruteforce.pde b/java/examples/Books/Nature of Code/chp9_ga/bruteforce/bruteforce.pde deleted file mode 100644 index c8a00ce51..000000000 --- a/java/examples/Books/Nature of Code/chp9_ga/bruteforce/bruteforce.pde +++ /dev/null @@ -1,16 +0,0 @@ - - -int now = millis(); - -int passedTime = millis() - now; -int count = 0; -while (passedTime < 1000) { - for (int i = 0; i < 33; i++) { - float r = random(27); - } - count++; - passedTime = millis() - now; -} -println(count); - - diff --git a/java/examples/Books/Nature of Code/introduction/Exc_I_10_NoiseLandscape/Exc_I_10_NoiseLandscape.pde b/java/examples/Books/Nature of Code/introduction/Exc_I_10_NoiseLandscape/Exc_I_10_NoiseLandscape.pde deleted file mode 100644 index ffef4e6ab..000000000 --- a/java/examples/Books/Nature of Code/introduction/Exc_I_10_NoiseLandscape/Exc_I_10_NoiseLandscape.pde +++ /dev/null @@ -1,32 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/teaching/nature - -// Landscape with height values according to Perlin noise - -Landscape land; -float theta = 0.0; - -void setup() { - - size(800,200,P3D); - - // Create a landscape object - land = new Landscape(20,800,400); -} - -void draw() { - - // Ok, visualize the landscape space - background(255); - pushMatrix(); - translate(width/2,height/2+20,-160); - rotateX(PI/3); - rotateZ(theta); - land.render(); - popMatrix(); - - land.calculate(); - - theta += 0.0025; -} diff --git a/java/examples/Books/Nature of Code/introduction/Exc_I_10_NoiseLandscape/Landscape.pde b/java/examples/Books/Nature of Code/introduction/Exc_I_10_NoiseLandscape/Landscape.pde deleted file mode 100644 index 7d1114ac5..000000000 --- a/java/examples/Books/Nature of Code/introduction/Exc_I_10_NoiseLandscape/Landscape.pde +++ /dev/null @@ -1,65 +0,0 @@ -// Daniel Shiffman -// -// "Landscape" example - -class Landscape { - - int scl; // size of each cell - int w, h; // width and height of thingie - int rows, cols; // number of rows and columns - float zoff = 0.0; // perlin noise argument - float[][] z; // using an array to store all the height values - - Landscape(int scl_, int w_, int h_) { - scl = scl_; - w = w_; - h = h_; - cols = w/scl; - rows = h/scl; - z = new float[cols][rows]; - } - - - // Calculate height values (based off a neural netork) - void calculate() { - float xoff = 0; - for (int i = 0; i < cols; i++) - { - float yoff = 0; - for (int j = 0; j < rows; j++) - { - z[i][j] = map(noise(xoff, yoff,zoff), 0, 1, -120, 120); - yoff += 0.1; - } - xoff += 0.1; - } - zoff+=0.01; - } - - // Render landscape as grid of quads - void render() { - // Every cell is an individual quad - // (could use quad_strip here, but produces funny results, investigate this) - for (int x = 0; x < z.length-1; x++) - { - for (int y = 0; y < z[x].length-1; y++) - { - // one quad at a time - // each quad's color is determined by the height value at each vertex - // (clean this part up) - stroke(0); - fill(100, 100); - pushMatrix(); - beginShape(QUADS); - translate(x*scl-w/2, y*scl-h/2, 0); - vertex(0, 0, z[x][y]); - vertex(scl, 0, z[x+1][y]); - vertex(scl, scl, z[x+1][y+1]); - vertex(0, scl, z[x][y+1]); - endShape(); - popMatrix(); - } - } - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/Exc_I_9_Noise3D/Exc_I_9_Noise3D.pde b/java/examples/Books/Nature of Code/introduction/Exc_I_9_Noise3D/Exc_I_9_Noise3D.pde deleted file mode 100644 index 125f77be9..000000000 --- a/java/examples/Books/Nature of Code/introduction/Exc_I_9_Noise3D/Exc_I_9_Noise3D.pde +++ /dev/null @@ -1,47 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -float increment = 0.01; -// The noise function's 3rd argument, a global variable that increments once per cycle -float zoff = 0.0; -// We will increment zoff differently than xoff and yoff -float zincrement = 0.02; - -void setup() { - size(200,200); -} - -void draw() { - background(0); - - // Optional: adjust noise detail here - // noiseDetail(8,0.65f); - - loadPixels(); - - float xoff = 0.0; // Start xoff at 0 - - // For every x,y coordinate in a 2D space, calculate a noise value and produce a brightness value - for (int x = 0; x < width; x++) { - xoff += increment; // Increment xoff - float yoff = 0.0; // For every xoff, start yoff at 0 - for (int y = 0; y < height; y++) { - yoff += increment; // Increment yoff - - // Calculate noise and scale by 255 - float bright = noise(xoff,yoff,zoff)*255; - - // Try using this line instead - //float bright = random(0,255); - - // Set each pixel onscreen to a grayscale value - pixels[x+y*width] = color(bright,bright,bright); - } - } - updatePixels(); - - zoff += zincrement; // Increment zoff - - -} diff --git a/java/examples/Books/Nature of Code/introduction/Figure_I_2_BellCurve/Figure_I_2_BellCurve.pde b/java/examples/Books/Nature of Code/introduction/Figure_I_2_BellCurve/Figure_I_2_BellCurve.pde deleted file mode 100644 index 72dc64e22..000000000 --- a/java/examples/Books/Nature of Code/introduction/Figure_I_2_BellCurve/Figure_I_2_BellCurve.pde +++ /dev/null @@ -1,38 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -float[] heights; - -void setup() { - size(400, 200); - smooth(); -} - -void draw() { - background(255); - float e = 2.71828183; //"e", see http://mathforum.org/dr.math/faq/faq.e.html for more info - float[] heights = new float[width]; //use an array to store all the "y" values - float m = 0; //default mean of 0 - float sd = map(mouseX,0,width,0.4,2); //standard deviation based on mouseX - for (int i = 0; i < heights.length; i++) { - float xcoord = map(i,0,width,-3,3); - float sq2pi = sqrt(2*PI); //square root of 2 * PI - float xmsq = -1*(xcoord-m)*(xcoord-m); //-(x - mu)^2 - float sdsq = sd*sd; //variance (standard deviation squared) - heights[i] = (1 / (sd * sq2pi)) * (pow(e, (xmsq/sdsq))); //P(x) function - } - - // a little for loop that draws a line between each point on the graph - stroke(0); - strokeWeight(2); - noFill(); - beginShape(); - for (int i = 0; i < heights.length-1; i++) { - float x = i; - float y = map(heights[i], 0, 1, height-2, 2); - vertex(x, y); - } - endShape(); -} - diff --git a/java/examples/Books/Nature of Code/introduction/Figure_I_5_Noise1DGraph/Figure_I_5_Noise1DGraph.pde b/java/examples/Books/Nature of Code/introduction/Figure_I_5_Noise1DGraph/Figure_I_5_Noise1DGraph.pde deleted file mode 100644 index af4efc566..000000000 --- a/java/examples/Books/Nature of Code/introduction/Figure_I_5_Noise1DGraph/Figure_I_5_Noise1DGraph.pde +++ /dev/null @@ -1,27 +0,0 @@ -// The Nature of Code -// http://www.shiffman.net/ - -// TIME -float t = 0.0; - -void setup() { - size(400,200); - smooth(); -} - - -void draw() { - background(255); - float xoff = t; - noFill(); - stroke(0); - strokeWeight(2); - beginShape(); - for (int i = 0; i < width; i++) { - float y = noise(xoff)*height; - xoff += 0.01; - vertex(i,y); - } - endShape(); - t+= 0.01; -} diff --git a/java/examples/Books/Nature of Code/introduction/Figure_I_6_RandomGraph/Figure_I_6_RandomGraph.pde b/java/examples/Books/Nature of Code/introduction/Figure_I_6_RandomGraph/Figure_I_6_RandomGraph.pde deleted file mode 100644 index d1d4bd4fd..000000000 --- a/java/examples/Books/Nature of Code/introduction/Figure_I_6_RandomGraph/Figure_I_6_RandomGraph.pde +++ /dev/null @@ -1,22 +0,0 @@ -// The Nature of Code -// http://www.shiffman.net/ - -void setup() { - size(400,200); - smooth(); -} - - -void draw() { - background(255); - noFill(); - stroke(0); - strokeWeight(2); - beginShape(); - for (int i = 0; i < width; i++) { - float y = random(height); - vertex(i,y); - } - endShape(); - noLoop(); -} diff --git a/java/examples/Books/Nature of Code/introduction/Gaussian2/Gaussian2.pde b/java/examples/Books/Nature of Code/introduction/Gaussian2/Gaussian2.pde deleted file mode 100644 index 43992d4c6..000000000 --- a/java/examples/Books/Nature of Code/introduction/Gaussian2/Gaussian2.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -Random generator; - -void setup() { - size(200,200); - background(0); - smooth(); - generator = new Random(); -} - -void draw() { - //create an alpha blended background - fill(0,1); - rect(0,0,width,height); - - //get 3 gaussian random numbers w/ mean of 0 and standard deviation of 1.0 - float r = (float) generator.nextGaussian(); - float g = (float) generator.nextGaussian(); - float b = (float) generator.nextGaussian(); - - //define standard deviation and mean - float sd = 100; float mean = 100; - //scale by standard deviation and mean - //also constrain to between (0,255) since we are dealing with color - r = constrain((r * sd) + mean,0,255); - - //repeat for g & b - sd = 20; mean = 200; - g = constrain((g * sd) + mean,0,255); - sd = 50; mean = 0; - b = constrain((b * sd) + mean,0,255); - - //get more gaussian numbers, this time for location - float xloc = (float) generator.nextGaussian(); - float yloc = (float) generator.nextGaussian(); - sd = width/10; - mean = width/2; - xloc = ( xloc * sd ) + mean; - yloc = ( yloc * sd ) + mean; - - //draw an ellipse with gaussian generated color and location - noStroke(); - fill(r,g,b); - ellipse(xloc,yloc,8,8); -} - diff --git a/java/examples/Books/Nature of Code/introduction/MonteCarloDistribution/MonteCarlo.pde b/java/examples/Books/Nature of Code/introduction/MonteCarloDistribution/MonteCarlo.pde deleted file mode 100644 index bc7cfab1a..000000000 --- a/java/examples/Books/Nature of Code/introduction/MonteCarloDistribution/MonteCarlo.pde +++ /dev/null @@ -1,66 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -float[] vals; // Array to count how often a random # is picked -float[] norms; // Normalized version of above - -void setup() { - size(200, 200); - smooth(); - vals = new float[width]; - norms = new float[width]; -} - -void draw() { - background(100); - - // Pick a random number between 0 and 1 based on custom probability function - float n = montecarlo(); - - // What spot in the array did we pick - int index = int(n*width); - vals[index]++; - stroke(255); - - boolean normalization = false; - float maxy = 0.0; - - // Draw graph based on values in norms array - // If a value is greater than the height, set normalization to true - for (int x = 0; x < vals.length; x++) { - line(x, height, x, height-norms[x]); - if (vals[x] > height) normalization = true; - if (vals[x] > maxy) maxy = vals[x]; - } - - // If normalization is true then normalize to height - // Otherwise, just copy the info - for (int x = 0; x < vals.length; x++) { - if (normalization) norms[x] = (vals[x] / maxy) * (height); - else norms[x] = vals[x]; - } -} - -// An algorithm for picking a random number based on monte carlo method -// Here probability is determined by formula y = x -float montecarlo() { - // Have we found one yet - boolean foundone = false; - int hack = 0; // let's count just so we don't get stuck in an infinite loop by accident - while (!foundone && hack < 10000) { - // Pick two random numbers - float r1 = (float) random(1); - float r2 = (float) random(1); - float y = r1*r1; // y = x*x (change for different results) - // If r2 is valid, we'll use this one - if (r2 < y) { - foundone = true; - return r1; - } - hack++; - } - // Hack in case we run into a problem (need to improve this) - return 0; -} - diff --git a/java/examples/Books/Nature of Code/introduction/MultipleProbability/MultipleProbability.pde b/java/examples/Books/Nature of Code/introduction/MultipleProbability/MultipleProbability.pde deleted file mode 100644 index 74144ce1c..000000000 --- a/java/examples/Books/Nature of Code/introduction/MultipleProbability/MultipleProbability.pde +++ /dev/null @@ -1,39 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// ITP, Spring 2006 -// http://www.shiffman.net/ - -int x,y; - -void setup() { - size(200,200); - background(0); - smooth(); -} - -void draw() { - //create an alpha blended background - fill(0,1); - rect(0,0,width,height); - - //probabilities for 3 different cases (these need to add up to 100% since something always occurs here!) - float p1 = 0.05; // 5% chance of pure white occurring - float p2 = 0.80 + p1; // 80% chance of gray occuring - //float p3 = 1.0 - p2 ; // 15% chance of black (we don't actually need this line since it is - // by definit n, the "in all other cases" part of our else - float num = random(1); // pick a random number between 0 and 1 - if (num -// The Nature of Code - -Walker w; - -void setup() { - size(800, 200); - smooth(); - frameRate(30); - - // Create a walker object - w = new Walker(); -} - -void draw() { - background(255); - // Run the walker object - w.walk(); - w.display(); -} - - diff --git a/java/examples/Books/Nature of Code/introduction/NOC_I_5_NoiseWalk/Walker.pde b/java/examples/Books/Nature of Code/introduction/NOC_I_5_NoiseWalk/Walker.pde deleted file mode 100644 index dd11e252b..000000000 --- a/java/examples/Books/Nature of Code/introduction/NOC_I_5_NoiseWalk/Walker.pde +++ /dev/null @@ -1,33 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code - -// A random walker class! - -class Walker { - PVector location; - - PVector noff; - - Walker() { - location = new PVector(width/2, height/2); - noff = new PVector(random(1000),random(1000)); - } - - void display() { - strokeWeight(2); - fill(127); - stroke(0); - ellipse(location.x, location.y, 48, 48); - } - - // Randomly move up, down, left, right, or stay in one place - void walk() { - - location.x = map(noise(noff.x),0,1,0,width); - location.y = map(noise(noff.y),0,1,0,height); - - noff.add(0.01,0.01,0); - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/Noise1D/Noise1D.pde b/java/examples/Books/Nature of Code/introduction/Noise1D/Noise1D.pde deleted file mode 100644 index a1be010e8..000000000 --- a/java/examples/Books/Nature of Code/introduction/Noise1D/Noise1D.pde +++ /dev/null @@ -1,31 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -float xoff = 0.0; -float xincrement = 0.01; - -void setup() { - size(200,200); - background(0); - smooth(); - noStroke(); -} - -void draw() { - // Create an alpha blended background - fill(0, 10); - rect(0,0,width,height); - - //float n = random(0,width); // Try this line instead of noise - - // Get a noise value based on xoff and scale it according to the window's width - float n = noise(xoff)*width; - - // With each cycle, increment xoff - xoff += xincrement; - - // Draw the ellipse at the value produced by perlin noise - fill(200); - ellipse(n,height/2,16,16); -} diff --git a/java/examples/Books/Nature of Code/introduction/Noise2D/Noise2D.pde b/java/examples/Books/Nature of Code/introduction/Noise2D/Noise2D.pde deleted file mode 100644 index dc231badc..000000000 --- a/java/examples/Books/Nature of Code/introduction/Noise2D/Noise2D.pde +++ /dev/null @@ -1,41 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -float increment = 0.02; - -void setup() { - size(800,200); - noLoop(); -} - -void draw() { - background(0); - - // Optional: adjust noise detail here - // noiseDetail(8,0.65f); - - loadPixels(); - - float xoff = 0.0; // Start xoff at 0 - - // For every x,y coordinate in a 2D space, calculate a noise value and produce a brightness value - for (int x = 0; x < width; x++) { - xoff += increment; // Increment xoff - float yoff = 0.0; // For every xoff, start yoff at 0 - for (int y = 0; y < height; y++) { - yoff += increment; // Increment yoff - - // Calculate noise and scale by 255 - float bright = noise(xoff,yoff)*255; - - // Try using this line instead - //float bright = random(0,255); - - // Set each pixel onscreen to a grayscale value - pixels[x+y*width] = color(bright); - } - } - - updatePixels(); -} diff --git a/java/examples/Books/Nature of Code/introduction/NoiseDistribution/NoiseDistribution.pde b/java/examples/Books/Nature of Code/introduction/NoiseDistribution/NoiseDistribution.pde deleted file mode 100644 index 56af3a708..000000000 --- a/java/examples/Books/Nature of Code/introduction/NoiseDistribution/NoiseDistribution.pde +++ /dev/null @@ -1,33 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// Testing Distribution of Perlin Noise generated #'s vs. Randoms - -float[] vals; -float[] norms; -float xoff = 0.0f; - -void setup() { - size(300,200); - vals = new float[width]; - norms = new float[width]; -} - -void draw() { - background(100); - float n = noise(xoff); - int index = int(n*width); - vals[index]++; - xoff += 0.01; - stroke(255); - boolean normalization = false; - float maxy = 0.0; - for (int x = 0; x < vals.length; x++) { - line(x,height,x,height-norms[x]); - if (vals[x] > height) normalization = true; - if(vals[x] > maxy) maxy = vals[x]; - } - for (int x = 0; x < vals.length; x++) { - if (normalization) norms[x] = (vals[x] / maxy) * (height); - else norms[x] = vals[x]; - } -} diff --git a/java/examples/Books/Nature of Code/introduction/NoiseWalkAcceleration/NoiseWalkAcceleration.pde b/java/examples/Books/Nature of Code/introduction/NoiseWalkAcceleration/NoiseWalkAcceleration.pde deleted file mode 100644 index e762a22e2..000000000 --- a/java/examples/Books/Nature of Code/introduction/NoiseWalkAcceleration/NoiseWalkAcceleration.pde +++ /dev/null @@ -1,24 +0,0 @@ -// Noise Walker -// Daniel Shiffman -// The Nature of Code - -Walker w; - -void setup() { - size(640,360); - smooth(); - - // Create a walker object - w = new Walker(); - -} - -void draw() { - background(255); - // Run the walker object - w.walk(); - w.display(); -} - - - diff --git a/java/examples/Books/Nature of Code/introduction/NoiseWalkAcceleration/Walker.pde b/java/examples/Books/Nature of Code/introduction/NoiseWalkAcceleration/Walker.pde deleted file mode 100644 index 78190295a..000000000 --- a/java/examples/Books/Nature of Code/introduction/NoiseWalkAcceleration/Walker.pde +++ /dev/null @@ -1,65 +0,0 @@ -// Random Walker -// Daniel Shiffman -// The Nature of Code - -// A random walker class! - -class Walker { - PVector location; - PVector velocity; - PVector acceleration; - - ArrayList history; - - PVector noff; - - - Walker() { - location = new PVector(width/2, height/2); - history = new ArrayList(); - noff = new PVector(random(1000), random(1000)); - velocity = new PVector(); - acceleration = new PVector(); - } - - void display() { - stroke(0); - fill(175); - rectMode(CENTER); - rect(location.x, location.y, 16, 16); - - beginShape(); - stroke(0); - noFill(); - for (PVector v: history) { - vertex(v.x, v.y); - } - endShape(); - } - - // Randomly move up, down, left, right, or stay in one place - void walk() { - - - acceleration.x = map(noise(noff.x), 0, 1, -1, 1); - acceleration.y = map(noise(noff.y), 0, 1, -1, 1); - acceleration.mult(0.1); - - noff.add(0.01, 0.01, 0); - - velocity.add(acceleration); - velocity.limit(1); - location.add(velocity); - - - history.add(location.get()); - if (history.size() > 1000) { - history.remove(0); - } - - // Stay on the screen - location.x = constrain(location.x, 0, width-1); - location.y = constrain(location.y, 0, height-1); - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/NoiseWalkVelocity/NoiseWalkVelocity.pde b/java/examples/Books/Nature of Code/introduction/NoiseWalkVelocity/NoiseWalkVelocity.pde deleted file mode 100644 index f656f8073..000000000 --- a/java/examples/Books/Nature of Code/introduction/NoiseWalkVelocity/NoiseWalkVelocity.pde +++ /dev/null @@ -1,25 +0,0 @@ -// Noise Walker -// Daniel Shiffman -// The Nature of Code - -Walker w; - -void setup() { - size(400,400); - smooth(); - frameRate(30); - - // Create a walker object - w = new Walker(); - -} - -void draw() { - background(255); - // Run the walker object - w.walk(); - w.display(); -} - - - diff --git a/java/examples/Books/Nature of Code/introduction/NoiseWalkVelocity/Walker.pde b/java/examples/Books/Nature of Code/introduction/NoiseWalkVelocity/Walker.pde deleted file mode 100644 index adcd64b17..000000000 --- a/java/examples/Books/Nature of Code/introduction/NoiseWalkVelocity/Walker.pde +++ /dev/null @@ -1,60 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code - -// A random walker class! - -class Walker { - PVector location; - PVector velocity; - - ArrayList history; - - PVector noff; - - - Walker() { - location = new PVector(width/2, height/2); - history = new ArrayList(); - noff = new PVector(random(1000), random(1000)); - velocity = new PVector(); - } - - void display() { - stroke(0); - fill(175); - rectMode(CENTER); - rect(location.x, location.y, 16, 16); - - beginShape(); - stroke(0); - noFill(); - for (PVector v: history) { - vertex(v.x, v.y); - } - endShape(); - } - - // Randomly move up, down, left, right, or stay in one place - void walk() { - - - velocity.x = map(noise(noff.x), 0, 1, -1, 1); - velocity.y = map(noise(noff.y), 0, 1, -1, 1); - velocity.mult(5); - - noff.add(0.01, 0.01, 0); - - location.add(velocity); - - history.add(location.get()); - if (history.size() > 1000) { - history.remove(0); - } - - // Stay on the screen - location.x = constrain(location.x, 0, width-1); - location.y = constrain(location.y, 0, height-1); - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalk/RandomWalk.pde b/java/examples/Books/Nature of Code/introduction/RandomWalk/RandomWalk.pde deleted file mode 100644 index 7e0c65aa6..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalk/RandomWalk.pde +++ /dev/null @@ -1,24 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code - -Walker w; - -void setup() { - size(400,400); - frameRate(30); - - // Create a walker object - w = new Walker(); - -} - -void draw() { - background(255); - // Run the walker object - w.walk(); - w.render(); -} - - - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalk/Walker.pde b/java/examples/Books/Nature of Code/introduction/RandomWalk/Walker.pde deleted file mode 100644 index b0d26d6b1..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalk/Walker.pde +++ /dev/null @@ -1,34 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code - -// A random walker class! - -class Walker { - float x, y; - - Walker() { - x = width/2; - y = height/2; - } - - void render() { - stroke(0); - fill(175); - rectMode(CENTER); - rect(x, y, 40, 40); - } - - // Randomly move up, down, left, right, or stay in one place - void walk() { - float vx = random(-2, 2); - float vy = random(-2, 2); - x += vx; - y += vy; - - // Stay on the screen - x = constrain(x, 0, width-1); - y = constrain(y, 0, height-1); - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkLevy/RandomWalkLevy.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkLevy/RandomWalkLevy.pde deleted file mode 100644 index 6c9507216..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkLevy/RandomWalkLevy.pde +++ /dev/null @@ -1,20 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -Walker w; - -void setup() { - size(640,480); - // Create a walker object - w = new Walker(); - background(0); -} - -void draw() { - // Run the walker object - w.step(); - w.render(); -} - - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkLevy/Walker.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkLevy/Walker.pde deleted file mode 100644 index 147040bbe..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkLevy/Walker.pde +++ /dev/null @@ -1,54 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -// A random walker object! - -class Walker { - float x, y; - - float prevX, prevY; - - Walker() { - x = width/2; - y = height/2; - } - - void render() { - stroke(255); - line(prevX,prevY,x, y); - } - - // Randomly move according to floating point values - void step() { - prevX = x; - prevY = y; - - float stepx = random(-1, 1); - float stepy = random(-1, 1); - - float stepsize = montecarlo()*50; - stepx *= stepsize; - stepy *= stepsize; - - x += stepx; - y += stepy; - x = constrain(x, 0, width-1); - y = constrain(y, 0, height-1); - } -} - - -float montecarlo() { - while (true) { - - float r1 = random(1); - float probability = pow(1.0 - r1,8); - - float r2 = random(1); - if (r2 < probability) { - return r1; - } - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkNoise/RandomWalkNoise.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkNoise/RandomWalkNoise.pde deleted file mode 100644 index 7b85201cb..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkNoise/RandomWalkNoise.pde +++ /dev/null @@ -1,20 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -Walker w; - -void setup() { - size(640,360); - smooth(); - w = new Walker(); - background(0); -} - -void draw() { - // Run the walker object - w.step(); - w.render(); -} - - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkNoise/Walker.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkNoise/Walker.pde deleted file mode 100644 index 4bf77fbdd..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkNoise/Walker.pde +++ /dev/null @@ -1,39 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -// A random walker object! - -class Walker { - float x, y; - float tx, ty; - - float prevX, prevY; - - Walker() { - tx = 0; - ty = 10000; - x = map(noise(tx), 0, 1, 0, width); - y = map(noise(ty), 0, 1, 0, height); - } - - void render() { - stroke(255); - line(prevX, prevY, x, y); - } - - // Randomly move according to floating point values - void step() { - - prevX = x; - prevY = y; - - x = map(noise(tx), 0, 1, 0, width); - y = map(noise(ty), 0, 1, 0, height); - - tx += 0.01; - ty += 0.01; - - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkPVector/RandomWalkPVector.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkPVector/RandomWalkPVector.pde deleted file mode 100644 index 7e0c65aa6..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkPVector/RandomWalkPVector.pde +++ /dev/null @@ -1,24 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code - -Walker w; - -void setup() { - size(400,400); - frameRate(30); - - // Create a walker object - w = new Walker(); - -} - -void draw() { - background(255); - // Run the walker object - w.walk(); - w.render(); -} - - - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkPVector/Walker.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkPVector/Walker.pde deleted file mode 100644 index 161db0a23..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkPVector/Walker.pde +++ /dev/null @@ -1,30 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code -// A random walker class! - -class Walker { - PVector loc; - - Walker() { - loc = new PVector(width/2,height/2); - } - - void render() { - stroke(0); - fill(175); - rectMode(CENTER); - rect(loc.x,loc.y,40,40); - } - - // Randomly move up, down, left, right, or stay in one place - void walk() { - PVector vel = new PVector(random(-2,2),random(-2,2)); - loc.add(vel); - - // Stay on the screen - loc.x = constrain(loc.x,0,width-1); - loc.y = constrain(loc.y,0,height-1); - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional2/RandomWalkTraditional2.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional2/RandomWalkTraditional2.pde deleted file mode 100644 index d45577c28..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional2/RandomWalkTraditional2.pde +++ /dev/null @@ -1,20 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -Walker w; - -void setup() { - size(200,200); - // Create a walker object - w = new Walker(); - background(0); -} - -void draw() { - // Run the walker object - w.step(); - w.render(); -} - - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional2/Walker.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional2/Walker.pde deleted file mode 100644 index a73abfb93..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional2/Walker.pde +++ /dev/null @@ -1,29 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -// A random walker object! - -class Walker { - int x,y; - - Walker() { - x = width/2; - y = height/2; - } - - void render() { - stroke(255); - point(x,y); - } - - // Randomly move to any neighboring pixel (or stay in the same spot) - void step() { - int stepx = int(random(3))-1; - int stepy = int(random(3))-1; - x += stepx; - y += stepy; - x = constrain(x,0,width-1); - y = constrain(y,0,height-1); - } -} diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional3/RandomWalkTraditional3.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional3/RandomWalkTraditional3.pde deleted file mode 100644 index d45577c28..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional3/RandomWalkTraditional3.pde +++ /dev/null @@ -1,20 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -Walker w; - -void setup() { - size(200,200); - // Create a walker object - w = new Walker(); - background(0); -} - -void draw() { - // Run the walker object - w.step(); - w.render(); -} - - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional3/Walker.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional3/Walker.pde deleted file mode 100644 index a3991d367..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkTraditional3/Walker.pde +++ /dev/null @@ -1,30 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -// A random walker object! - -class Walker { - float x, y; - - Walker() { - x = width/2; - y = height/2; - } - - void render() { - stroke(255); - point(x, y); - } - - // Randomly move according to floating point values - void step() { - float stepx = random(-1, 1); - float stepy = random(-1, 1); - x += stepx; - y += stepy; - x = constrain(x, 0, width-1); - y = constrain(y, 0, height-1); - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkTrail/RandomWalkTrail.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkTrail/RandomWalkTrail.pde deleted file mode 100644 index 829dd5a6a..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkTrail/RandomWalkTrail.pde +++ /dev/null @@ -1,24 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code - -Walker w; - -void setup() { - size(400,400); - frameRate(30); - - // Create a walker object - w = new Walker(); - -} - -void draw() { - background(255); - // Run the walker object - w.walk(); - w.display(); -} - - - diff --git a/java/examples/Books/Nature of Code/introduction/RandomWalkTrail/Walker.pde b/java/examples/Books/Nature of Code/introduction/RandomWalkTrail/Walker.pde deleted file mode 100644 index 4fbab1eb1..000000000 --- a/java/examples/Books/Nature of Code/introduction/RandomWalkTrail/Walker.pde +++ /dev/null @@ -1,49 +0,0 @@ -// Random Walker (No Vectors) -// Daniel Shiffman -// The Nature of Code - -// A random walker class! - -class Walker { - PVector location; - - ArrayList history; - - - Walker() { - location = new PVector(width/2, height/2); - history = new ArrayList(); - } - - void display() { - stroke(0); - fill(175); - rectMode(CENTER); - rect(location.x, location.y, 16, 16); - - beginShape(); - stroke(0); - noFill(); - for (PVector v: history) { - vertex(v.x, v.y); - } - endShape(); - } - - // Randomly move up, down, left, right, or stay in one place - void walk() { - PVector vel = new PVector(random(-2, 2), random(-2, 2)); - location.add(vel); - - // Stay on the screen - location.x = constrain(location.x, 0, width-1); - location.y = constrain(location.y, 0, height-1); - - - history.add(location.get()); - if (history.size() > 1000) { - history.remove(0); - } - } -} - diff --git a/java/examples/Books/Nature of Code/introduction/SimpleProbablility/SimpleProbablility.pde b/java/examples/Books/Nature of Code/introduction/SimpleProbablility/SimpleProbablility.pde deleted file mode 100644 index 8843f1581..000000000 --- a/java/examples/Books/Nature of Code/introduction/SimpleProbablility/SimpleProbablility.pde +++ /dev/null @@ -1,34 +0,0 @@ -// Daniel Shiffman -// The Nature of Code -// http://www.shiffman.net/ - -int x,y; - -void setup() { - size(200,200); - background(0); - smooth(); -} - -void draw() { - //create an alpha blended background - fill(0,1); - rect(0,0,width,height); - - //calculate a probability between 0 and 100% based on mouseX location - float prob = (mouseX / (float) width); - - //get a random floating point value between 0 and 1 - float r = random(1); - - //test the random value against the probability and trigger an event - if (r < prob) { - noStroke(); - fill(255); - ellipse(x,y,10,10); - } - - // X and Y walk through a grid - x = (x + 10) % width; - if (x == 0) y = (y + 10) % width; -}