xxxxxxxxxx
72
// Coding Assignment - Week #4
// 30/ 09/ 2023
// By Lukrecija Paulikaite
// Music downloaded from:
// https://www.chosic.com/download-audio/25899/
let song;
let particles = [];
let numRows = 6;
let numCols = 6;
function preload() {
song = loadSound("classical.mp3");
}
function setup() {
createCanvas(windowWidth,windowHeight);
background(0);
song.play();
let spacingX = width / numCols;
let spacingY = height / numRows;
// initially displaying particles on a grid
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
let x = spacingX * col + spacingX / 2;
let y = spacingY * row + spacingY / 2;
particles.push(new Particle(x, y));
}
}
}
function draw() {
background(0, 10);
// looping through all the particles
for (let i = 0; i < particles.length; i++) {
let particleA = particles[i];
particleA.update();
particleA.display();
// initializing an inner loop to compare the current particle to other particles and calculating distances
for (let j = i + 1; j < particles.length; j++) {
let particleB = particles[j];
let distance = dist(
particleA.position.x,
particleA.position.y,
particleB.position.x,
particleB.position.y
);
// drawing a line between particles that are closer than 100 pixels
if (distance < 100) {
line(
particleA.position.x,
particleA.position.y,
particleB.position.x,
particleB.position.y
);
}
}
}
}