xxxxxxxxxx
50
// Batool Al Tameemi, Decoding Nature, Fall 2023
// Prompt from list 1: Try implement the random walk known as a Levy Flight.
// Declare variables to store the current position and step size
let position;
let stepSize;
function setup() {
// Create a canvas
createCanvas(400, 400);
background(220);
// Initialize the starting position within the canvas boundaries
position = createVector(random(width), random(height));
// Initialize the initial step size within a range to control speed
stepSize = random(5, 20); // Adjust this range for different speeds
}
function draw() {
// Draw a point at the current position
stroke(0);
strokeWeight(2);
point(position.x, position.y);
// Levy Flight: Generate a random angle and magnitude for the step
let angle = random(TWO_PI); // Random angle between 0 and 2*PI
let magnitude = random(1, 100); // Random magnitude for the step
// Calculate the new position based on the step's direction and magnitude
let newPosition = createVector(
position.x + cos(angle) * stepSize,
position.y + sin(angle) * stepSize
);
// Constrain the new position to stay within the canvas boundaries
newPosition.x = constrain(newPosition.x, 0, width);
newPosition.y = constrain(newPosition.y, 0, height);
// Draw a line from the current position to the new position
line(position.x, position.y, newPosition.x, newPosition.y);
// Update the position to the new position
position = newPosition;
// Occasionally change the step size with a probability of 10%
if (random(1) < 0.1) {
stepSize = random(5, 20); // Change step size to a new random value for variation
}
}