I have this loop:
for (let i = 0; i < vertices.length; i++) {
let ranVert1 = floor(random(1, vertices.length - 1)) + 1;
let ranVert2 = floor(random(1, vertices.length - 1)) + 1;
let found = lines.filter((line, index, array) => {
console.log(`Line ${index}:`);
console.log(line.v1);
});
if(found.length > 0){
continue;
}
lines.push(new Line(ranVert1, ranVert2));
}
It loops through each vertex in a vertices array, and creates a line between any 2 vertices. But I want to remove duplicates.
So if we have vertices 0, 1, 2, 3, 4. Then it generates line [2, 4]. I don't want it to generate another line that is either [2, 4] or [4, 2].
What's the best way to do this? (Preferably without using a for loop)
Thanks in advance.
Edit:
A vertex looks like this:
{
x: 300,
y: 500
}
A line looks like this:
{
v1: 5,
v2: 6
}