Skip to content

Instantly share code, notes, and snippets.

@neoeno
Forked from anonymous/gist:4280689
Created December 13, 2012 23:16
Show Gist options
  • Save neoeno/4281024 to your computer and use it in GitHub Desktop.
Save neoeno/4281024 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;
// Assignment in java, as you'll probably recognize
// from other OO languages, just assigns a reference.
// So, previously, because you pointed them both at
// the same area in memory, editing one edited the other,
// hence the gaps :)
// prevXYPosition = xYPosition;
prevXYPosition = xYPosition.clone();
}
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;
prevXYPosition = xYPosition.clone();
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