xxxxxxxxxx
75
//declares variables for random walker and all levy flight positions
let walker1;
let walker2;
let pos;
let prev;
function setup() {
//uploads background image to appear in the sketch
loadImage('WorldMap.jpg', img => {
image(img, 0, 0, width, height, 300, 200, 1300, 700);
});
//creates canvas size for running the sketch
createCanvas(900, 700);
//initializes variable for levy flight starting position
pos = createVector(random(width), random(height));
//initializes variable for next levy flight positions after the previous ones
prev = pos.copy();
//initializes variable for the random walker as a new object class
walker1 = new Walker(pos);
walker2 = new Walker(pos);
}
function draw() {
//calls two functions for the random walker object class
walker1.step();
walker1.show();
walker2.step();
walker2.show();
}
//creates and uses a class for the levy flight random walker object
class Walker {
constructor(pos) {
this.col = color(random(255),0,random(255), 50);
// TODO:
// Replace the lines below with separate x,y with:
// this.position = pos;
// TODO:
// this.prev???
//declares and initializes variables for x and y positions of the random walker
this.x = pos.x;
this.y = pos.y;
}
step() {
//declares and initializes variable for actual levy flight feature for random walker
var step = p5.Vector.random2D();
//next few lines of code sets random behavior for levy flight walker and position distance
var r = random(100);
if (r < 1) {
step.mult(random(25, 100));
} else {
step.setMag(12);
}
//makes the levy flight walker jump abruptly from one position to another far apart
// pos.add(step);
this.x += step.x;
this.y += step.y;
}
show() {
//gives a red color to all of the circles from the random walker
fill(this.col);
strokeWeight(0.25);
ellipse(this.x, this.y, random(-35, 35));
prev.set(this.x, this.y);
}
}