i think i really got it this time, nature of code in

This commit is contained in:
shiffman
2012-09-04 01:47:20 +00:00
parent b5b67a12b5
commit 1d86e1f5bf
541 changed files with 35009 additions and 0 deletions
@@ -0,0 +1,32 @@
// 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;
}
@@ -0,0 +1,65 @@
// Daniel Shiffman
// <http://www.shiffman.net>
// "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();
}
}
}
}
@@ -0,0 +1,47 @@
// 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
}
@@ -0,0 +1,38 @@
// 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();
}
@@ -0,0 +1,27 @@
// 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;
}
@@ -0,0 +1,22 @@
// 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();
}
@@ -0,0 +1,49 @@
// 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);
}
@@ -0,0 +1,66 @@
// 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;
}
@@ -0,0 +1,39 @@
// 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 <p1) {
fill(255);
} else if (num < p2) {
fill(150);
} else {
fill(0);
}
stroke(200);
rect(x,y,10,10);
// X and Y walk through a grid
x = (x + 10) % width;
if (x == 0) y = (y + 10) % width;
}
@@ -0,0 +1,20 @@
// Daniel Shiffman
// The Nature of Code
// http://www.shiffman.net/
Walker w;
void setup() {
size(800,200);
// Create a walker object
w = new Walker();
background(255);
}
void draw() {
// Run the walker object
w.step();
w.render();
}
@@ -0,0 +1,38 @@
// 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(0);
point(x,y);
}
// Randomly move up, down, left, right, or stay in one place
void step() {
int choice = int(random(4));
if (choice == 0) {
x++;
} else if (choice == 1) {
x--;
} else if (choice == 2) {
y++;
} else {
y--;
}
x = constrain(x,0,width-1);
y = constrain(y,0,height-1);
}
}
@@ -0,0 +1,26 @@
// An array to keep track of how often random numbers are picked
float[] randomCounts;
void setup() {
size(800,200);
randomCounts = new float[20];
}
void draw() {
background(255);
// Pick a random number and increase the count
int index = int(random(randomCounts.length));
randomCounts[index]++;
// Draw a rectangle to graph results
stroke(0);
strokeWeight(2);
fill(127);
int w = width/randomCounts.length;
for (int x = 0; x < randomCounts.length; x++) {
rect(x*w,height-randomCounts[x],w-1,randomCounts[x]);
}
}
@@ -0,0 +1,20 @@
// Daniel Shiffman
// The Nature of Code
// http://www.shiffman.net/
Walker w;
void setup() {
size(800,200);
// Create a walker object
w = new Walker();
background(255);
}
void draw() {
// Run the walker object
w.step();
w.render();
}
@@ -0,0 +1,39 @@
// 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(0);
strokeWeight(2);
point(x,y);
}
// Randomly move up, down, left, right, or stay in one place
void step() {
float r = random(1);
// A 40% of moving to the right!
if (r < 0.4) {
x++;
} else if (r < 0.6) {
x--;
} else if (r < 0.8) {
y++;
} else {
y--;
}
x = constrain(x,0,width-1);
y = constrain(y,0,height-1);
}
}
@@ -0,0 +1,32 @@
// Daniel Shiffman
// The Nature of Code
// http://www.shiffman.net/
// Declare a Random number generator object
Random generator;
void setup() {
size(800, 200);
background(255);
generator = new Random(); // Initialize generator
smooth();
}
void draw() {
// Get a gaussian random number w/ mean of 0 and standard deviation of 1.0
float xloc = (float) generator.nextGaussian();
float sd = 60; // Define a standard deviation
float mean = width/2; // Define a mean value (middle of the screen along the x-axis)
xloc = ( xloc * sd ) + mean; // Scale the gaussian random number by standard deviation and mean
noStroke();
fill(0, 10);
noStroke();
ellipse(xloc, height/2, 16, 16); // Draw an ellipse at our "normal" random location
}
@@ -0,0 +1,23 @@
// Noise Walker
// Daniel Shiffman <http://www.shiffman.net>
// 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();
}
@@ -0,0 +1,33 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// 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);
}
}
@@ -0,0 +1,31 @@
// 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);
}
@@ -0,0 +1,41 @@
// 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();
}
@@ -0,0 +1,33 @@
// 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];
}
}
@@ -0,0 +1,24 @@
// Noise Walker
// Daniel Shiffman <http://www.shiffman.net>
// 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();
}
@@ -0,0 +1,65 @@
// Random Walker
// Daniel Shiffman <http://www.shiffman.net>
// The Nature of Code
// A random walker class!
class Walker {
PVector location;
PVector velocity;
PVector acceleration;
ArrayList<PVector> history;
PVector noff;
Walker() {
location = new PVector(width/2, height/2);
history = new ArrayList<PVector>();
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);
}
}
@@ -0,0 +1,25 @@
// Noise Walker
// Daniel Shiffman <http://www.shiffman.net>
// 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();
}
@@ -0,0 +1,60 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// The Nature of Code
// A random walker class!
class Walker {
PVector location;
PVector velocity;
ArrayList<PVector> history;
PVector noff;
Walker() {
location = new PVector(width/2, height/2);
history = new ArrayList<PVector>();
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);
}
}
@@ -0,0 +1,24 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// 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();
}
@@ -0,0 +1,34 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// 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);
}
}
@@ -0,0 +1,20 @@
// 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();
}
@@ -0,0 +1,54 @@
// 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;
}
}
}
@@ -0,0 +1,20 @@
// 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();
}
@@ -0,0 +1,39 @@
// 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;
}
}
@@ -0,0 +1,24 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// 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();
}
@@ -0,0 +1,30 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// 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);
}
}
@@ -0,0 +1,20 @@
// 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();
}
@@ -0,0 +1,29 @@
// 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);
}
}
@@ -0,0 +1,20 @@
// 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();
}
@@ -0,0 +1,30 @@
// 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);
}
}
@@ -0,0 +1,24 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// 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();
}
@@ -0,0 +1,49 @@
// Random Walker (No Vectors)
// Daniel Shiffman <http://www.shiffman.net>
// The Nature of Code
// A random walker class!
class Walker {
PVector location;
ArrayList<PVector> history;
Walker() {
location = new PVector(width/2, height/2);
history = new ArrayList<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() {
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);
}
}
}
@@ -0,0 +1,34 @@
// 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;
}