xxxxxxxxxx
33
// Drunk Walk I: Brownian Motion. Create a sketch in which a small 2D element travels erratically from one moment to the next, leaving a trail across the canvas as it moves. At every timestep, it should update its current posision with a small random displacement in both X and Y.
let currentX;
let currentY;
let lastX;
let lastY;
function setup() {
createCanvas(400, 400);
currentX = width/2;
currentY = height/2;
lastX = currentX;
lastY = currentY;
background(255);
}
function draw() {
stroke(0);
let randomX = random(-30,30);
let randomY = random(-30,30);
currentX += randomX;
currentY += randomY;
line(lastX, lastY, currentX, currentY);
lastX = currentX;
lastY = currentY;
}