xxxxxxxxxx
90
//declares variables for random walker and current levy flight positions
let walker1;
let walker2;
//declares variable for background image
let img;
function preload() {
//uploads background image to appear in the sketch
img = loadImage('World_Map.jpg');
}
function setup() {
//uploads background image of the sketch and positions and sizes the image
loadImage('World_Map.jpg', img => {
image(img, 0, 0, 700, 500, 100, 0, 1900, 1200);
});
//creates canvas size for running the sketch
createCanvas(700, 500);
//initializes variable for the random walker as a new object class
walker1 = new Walker(createVector(random(width), random(height)));
walker2 = new Walker(createVector(random(width), random(height)));
}
function draw() {
background(0);
//calls two functions for the movement and display of 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) {
//declares and initializes variable for dot colors of random walker
this.col = color(255, 0, 0);
//declares and initializes variable for current x and y positions of random walker
this.position = pos.copy();
//initializes vector variable for levy flight velocity
this.velocity = createVector(random(-3, 3), random(-3, 3));
//initializes vector variable for levy flight acceleration
this.acceleration = createVector(-1, 1);
}
step() {
//declares and initializes variable for actual levy flight feature for random walker
var step = p5.Vector.random2D();
//declares variable and uses if statement to implement levy flight of the random walker
var r = random(100);
if (r < 1) {
step.mult(random(25, 100));
} else {
step.setMag(15);
}
//makes the levy flight walker jump abruptly from one position to another
this.position.add(step);
//adds random velocity over time to the random walker
this.position.add(this.velocity);
//adds acceleration over time to the random walker
this.velocity.mult(this.acceleration);
//if statement to keep the x position of the random walker inside the canvas
if ((this.position.x > width) || (this.position.x < 0)) {
this.position.x = random(width);
}
//if statement to keep the y position of the random walker inside the canvas
if ((this.position.y > height) || (this.position.y < 0)) {
this.position.y = random(height);
}
}
show() {
//declares variable to identify RGBA pixel values from certain x and y positions of image
let current = get(this.position.x, this.position.y);
//initializes current variable to detect average RGBA pixel values from x and y positions
current = (current[0] + current[1] + current[2]) / 3;
//conditional to display red dots when x and y positions of random walker are on land
//if (current < 10) {
noStroke();
fill(this.col);
ellipse(this.position.x, this.position.y, 64);
//} else {}
}
}