xxxxxxxxxx
63
// Batool Al Tameemi, Decoding Nature Fall 2023
// Asssignemnt 1; Levy Flight X 3D space and color modification
let positions = [];
let stepSize;
let yRotation = 0; // Y-axis rotation angle
function setup() {
// Create a canvas
createCanvas(400, 400, WEBGL); // Use the WEBGL renderer for 3D
// Initialize the starting position within the canvas boundaries
positions.push(createVector(random(-width / 2, width / 2), random(-height / 2, height / 2), random(-100, 100)));
// Initialize the initial step size within a range to control speed
stepSize = random(5, 20); // Adjust this range for different speeds
}
function draw() {
background(220);
// Apply 3D transformations
translate(0, 0, mouseY); // Move the scene along the z-axis based on mouseY
rotateY(yRotation); // Rotate around the y-axis
yRotation += 0.01; // Increment the rotation angle for animation
// Levy Flight: Generate random angles and magnitudes for the step in 3D
let theta = random(TWO_PI); // Random angle around the x-y plane
let phi = random(TWO_PI); // Random angle around the x-z plane
let r = random(1, 100); // Random magnitude for the step
// Calculate the new position in 3D space based on the angles and magnitude
let newX = positions[positions.length - 1].x + cos(theta) * sin(phi) * stepSize;
let newY = positions[positions.length - 1].y + sin(theta) * sin(phi) * stepSize;
let newZ = positions[positions.length - 1].z + cos(phi) * stepSize;
// Constrain the new position to stay within the canvas boundaries
newX = constrain(newX, -width / 2, width / 2);
newY = constrain(newY, -height / 2, height / 2);
newZ = constrain(newZ, -100, 100); // Adjust the constraints for the z-axis
// Update the position to the new position
positions.push(createVector(newX, newY, newZ));
// Draw lines connecting previous positions with varying RGB color
stroke(map(newX, -width / 2, width / 2, 0, 255), map(newY, -height / 2, height / 2, 0, 255), map(newZ, -100, 100, 0, 255), 50); // Color based on position
strokeWeight(1);
for (let i = 1; i < positions.length; i++) {
let prevPos = positions[i - 1];
let currentPos = positions[i];
line(prevPos.x, prevPos.y, prevPos.z, currentPos.x, currentPos.y, currentPos.z);
}
// Draw a point at the current position with RGB color
stroke(map(newX, -width / 2, width / 2, 0, 255), map(newY, -height / 2, height / 2, 0, 255), map(newZ, -100, 100, 0, 255));
strokeWeight(2);
point(positions[positions.length - 1].x, positions[positions.length - 1].y, positions[positions.length - 1].z);
// 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
}
}