xxxxxxxxxx
let lines = [];
let longestLineIndex = -1;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
for (let i = 0; i < lines.length; i++) {
let lineSegment = lines[i];
stroke(0);
if (i === longestLineIndex) {
stroke(255, 0, 0);
}
line(lineSegment[0], lineSegment[1], lineSegment[2], lineSegment[3]);
}
}
function mousePressed() {
lines.push([mouseX, mouseY, mouseX, mouseY]);
}
function mouseDragged() {
let currentLine = lines[lines.length - 1];
currentLine[2] = mouseX;
currentLine[3] = mouseY;
let longestLength = 0;
for (let i = 0; i < lines.length; i++) {
let lineSegment = lines[i];
let d = dist(lineSegment[0], lineSegment[1], lineSegment[2], lineSegment[3]);
if (d > longestLength) {
longestLength = d;
longestLineIndex = i;
}
}
}