xxxxxxxxxx
function setup() {
createCanvas(400, 400);
stroke(255); // set stroke color to white
strokeWeight(2); // set stroke weight to 2 pixels
// Set the length of the lines
const lineLength = 50;
// Initialize the lines array
let lines = [];
// Generate 1000 random lines
for (let i = 0; i < 1000; i++) {
// Generate random coordinates for the start and end points of the line
let x1 = random(0, width);
let y1 = random(0, height);
let x2 = x1 + lineLength;
let y2 = y1 + lineLength;
// Draw the line on the canvas
line(x1, y1, x2, y2);
// Add the line to the lines array
lines.push({ x1, y1, x2, y2 });
}
// Check for overlaps and remove overlapped lines
for (let i = 0; i < lines.length; i++) {
for (let j = i + 1; j < lines.length; j++) {
// Check if the lines overlap
if (linesOverlap(lines[i], lines[j])) {
// Remove the overlapped line
lines.splice(j, 1);
// Decrement the loop variable to avoid skipping the next line
j--;
}
}
}
}
// Function to check if two lines overlap
function linesOverlap(line1, line2) {
// Check if the lines have a common point
return (line1.x1 == line2.x1 && line1.y1 == line2.y1) ||
(line1.x1 == line2.x2 && line1.y1 == line2.y2) ||
(line1.x2 == line2.x1 && line1.y2 == line2.y1) ||
(line1.x2 == line2.x2 && line1.y2 == line2.y2);
}