xxxxxxxxxx
// kenzo
// 01-30-2025
let particles = [];
let squareSize = 20;
let simulation;
function setup() {
//pixelDensity(5)
createCanvas(400, 600);
background(255);
simulation = create();
for (let i = 0; i < 1; i++) {
let startX = random(width);
let startY = random(height);
particles.push(builder(startX, startY, squareSize));
}
}
function draw() {
for (let particle of particles) {
particle.move();
particle.display();
}
}
function builder(startX, startY, squareSize) {
let queue = [{ x: startX, y: startY }];
let direction = random(TWO_PI);
let speed = random(0.5, 5);
return {
queue,
move() {
if (this.queue.length === 0) return;
let { x, y } = this.queue[this.queue.length - 1];
let newX = x + cos(direction) * speed;
let newY = y + sin(direction) * speed;
if (random() < 0.1) {
direction += random([-HALF_PI, HALF_PI]);
}
if (
newX > 0 &&
newX < width &&
newY > 0 &&
newY < height &&
simulation.isMoveAllowed(newX, newY)
) {
this.queue.push({ x: newX, y: newY });
simulation.markVisited(newX, newY);
} else {
this.queue.pop();
}
},
display() {
if (this.queue.length > 1) {
stroke(0, 150);
strokeWeight(0.5);
beginShape();
for (let point of this.queue) {
vertex(point.x, point.y);
}
endShape(CLOSE);
}
},
};
}
function create() {
let grid = [];
for (let x = 0; x < width; x++) {
grid[x] = [];
for (let y = 0; y < height; y++) {
grid[x][y] = false;
}
}
return {
isMoveAllowed(x, y) {
let col = int(x);
let row = int(y);
return !grid[col][row];
},
markVisited(x, y) {
let col = int(x);
let row = int(y);
grid[col][row] = true;
},
};
}
function touchStarted() {
saveCanvas(canvas, 'myCanvas', 'jpg');
return false;
}