Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2012 22:32
Show Gist options
  • Save anonymous/4280689 to your computer and use it in GitHub Desktop.
Save anonymous/4280689 to your computer and use it in GitHub Desktop.
import java.util.Random;
class Walker {
int[] xYPosition = new int[2];
int[] prevXYPosition = new int[2];
Random generator;
Walker() {
generator = new Random();
xYPosition[0] = width / 2;
xYPosition[1] = height / 2;
prevXYPosition = xYPosition;
}
void display() {
stroke (0);
line (prevXYPosition[0], prevXYPosition[1], xYPosition[0], xYPosition[1]);
}
int gaussianDistance() {
int num = (int) generator.nextGaussian();
int standardDeviation = 2;
return max(standardDeviation * ((int) num), 1);
}
int[] gaussianVector(int[] vector) {
int distance = gaussianDistance();
vector[0] *= distance;
vector[1] *= distance;
println(distance);
return vector;
}
int[] directionTowardsMousePosition() {
int[] mouseXYPosition = { mouseX, mouseY };
int[] direction = { 0, 0 };
for (int axisIndex = 0; axisIndex < xYPosition.length; axisIndex ++) {
if (mouseXYPosition[axisIndex] < xYPosition[axisIndex]) {
direction[axisIndex] = -1;
}
else if (mouseXYPosition[axisIndex] > xYPosition[axisIndex]) {
direction[axisIndex] = 1;
}
else {
direction[axisIndex] = 0;
}
}
return direction;
}
void walk(int[] vector) {
prevXYPosition = xYPosition;
xYPosition[0] += vector[0];
xYPosition[1] += vector[1];
}
int[] randomDirection() {
int[] direction = { 0, 0 };
direction[0] += random(3) - 2;
direction[1] += random(3) - 2;
return direction;
}
void step() {
float randomNumber = random(1);
float chance = 1;
int[] direction = { 0, 0 };
if (randomNumber < chance) {
direction = directionTowardsMousePosition();
}
else {
direction = randomDirection();
}
int[] vector = gaussianVector(direction);
walk(vector);
}
}
Walker w;
void setup() {
size (640, 360);
w = new Walker();
background (255);
}
void draw() {
w.step();
w.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment