xxxxxxxxxx
55
//Initializing variables
let stars = [];
let backgroundStars;
function setup()
{
// setting frame rate to 100 frames per second
frameRate(100);
// canvas fills the entire window
createCanvas(windowWidth, windowHeight);
background(0);
// calling new bg stars with every frame
backgroundStars = new BackgroundStar();
//looping to add a total of 100 stars
for (let i = 0; i < 100; i++)
{
// pushing new star from Star class into the array
stars.push(new Star());
}
}
function draw() {
background(0);
// calling the display function of the bg stars
backgroundStars.displayBgStar();
// Call functions of Star class for ever star in stars array
for (let star of stars)
{
star.display();
star.update();
star.noOverlap();
star.checkEdges();
}
// Draw lines
// loop to get star 'i' in the star array
for (let i = 0; i < stars.length; i++)
{
let linesConnected = 0; // Initialize the count of lines connected to the current star
// then loop again to get the star that follows star 'i' in the array
for (let j = i + 1; j < stars.length; j++)
{
// measure the distance between the first star and the star that follows
let d = dist(stars[i].position.x, stars[i].position.y, stars[j].position.x, stars[j].position.y);
// Add a condition to check if the number of lines connected to the star is less than 4
if (d < 100 && linesConnected < 10) {
stroke(255);
// draw a line between the position of the two stars only if they meet the conditions
line(stars[i].position.x, stars[i].position.y, stars[j].position.x, stars[j].position.y);
linesConnected++; // Increment the count of lines connected to the star
}
}
}
}