/*
Jasper Travers
Walk: Version 02
Walk--Inspired by Walker from the introduction of "The Nature of Code" by Daniel Shiffman
http://natureofcode.com/book/introduction/
1/13/16
*/
Walker w;
boolean[] open;
boolean end = false;
void setup() {
size(640, 360);
background(255);
frameRate(240);
strokeWeight(1);
//Boolean array which represents all the pixels of the canvas and one more on either side
//to prevent IndexOutOfBoundsExceptions
open = new boolean[(height + 2) * (width + 2)];
//sets open boolean array to true
for (int i = 0; i < open.length; i++) {
open[i] = true;
}
//initiates Walker object
w = new Walker();
}
//Steps to a new point, then draws in that point
void draw() {
w.step();
w.display();
}
//Press any key to pause/unpause the drawing
void keyPressed() {
//saveFrame("Walk-#####.png");
if (end == false) {
end = true;
noLoop();
} else {
end = false;
loop();
}
}
/* Name: Walker
* Purpose: To define a Walker object and specify the random variables which influence the Walker's direction
* Builds color gradient.
* Params: None
*/
class Walker {
//location of object which, updated by random variables
int x;
int y;
//random gen variables for Walker
int choice = 0;
int contLine = 0;
//variables used for color gradient
float randR = 0;
float randG = 0;
float randB = 0;
//probabilities for right, left, down, up. prob4 = max of randintgen
int prob1 = 250;
int prob2 = 500;
int prob3 = 750;
int prob4 = 1000;
Walker() {
//initializes first Walker to middle of screen
//x = width / 2;
//y = height / 2;
//variables used to place a new walker
int randX = 0;
int randY = 0;
//randomizes placement of new walkers
while (true) {
randX = int(random(width / 16, 15 * width / 16));
randY = int(random(height / 16, 15 * height / 16));
//if a walker can be placed on that point
if (open[randX * randY] == true) {
break;
}
}
//sets location of the walker
x = randX;
y = randY;
}
//draws point, color based on location
void display() {
float xf = x;
float yf = y;
float wf = width;
float rgb = 255;
//determines color gradient
//red scales left to right
randR = xf / wf * rgb;
//blue scales top to bottom
randB = yf / wf * rgb;
//green scales right to left / 2
randG = (wf - xf) / wf * rgb / 2;
//draws point
stroke(randR, randG, randB);
point(x, y);
}
void step() {
boolean right = false;
boolean left = false;
boolean up = false;
boolean down = false;
while (x > 0 && x < width && y > 0 && y < height) {
//checks which directions the walker is allowed to draw
right = open[width * y + (x + 1)];
left = open[width * y + (x - 1)];
down = open[width * (y + 1) + x];
up = open[width * (y - 1) + x];
break;
}
while (true) {
//random variable to determine how long a line goes straight or until it is forced a new direction
if (contLine < 10) {
//random variable determines direction based on available directions
choice = int(random(prob4));
}
contLine = int(random(100));
//if I choose right, and can go right
if (choice < prob1 && right == true) {
open[width * y + x] = false; //self
open[width * y + (x - 1)] = false; //left
open[width * (y + 1) + x] = false; //down
open[width * (y - 1) + x] = false; //up
//go right
x++;
break;
}
//if I choose left, and can go left
else if (choice < prob2 && left == true) {
open[width * y + (x + 1)] = false; //right
open[width * y + x] = false; //self
open[width * (y + 1) + x] = false; //down
open[width * (y - 1) + x] = false; //up
//go left
x--;
break;
}
//if I choose down, and can go down
else if (choice < prob3 && down == true) {
open[width * y + (x + 1)] = false; //right
open[width * y + (x - 1)] = false; //left
open[width * y + x] = false; //self
open[width * (y - 1) + x] = false; //up
//go down
y++;
break;
}
//if I choose up, and can go up
else if (choice < prob4 && up == true) {
open[width * y + (x + 1)] = false; //right
open[width * y + (x - 1)] = false; //left
open[width * (y + 1) + x] = false; //down
open[width * y + x] = false; //self
//go up
y--;
break;
}
//if trapped, make a new walker
else if (right == false && left == false && up == false && down == false) {
w = new Walker();
break;
}
}
}
int getX() {
return x;
}
int getY() {
return y;
}
}