xxxxxxxxxx
84
let myLines = [];
var start = 0;
var end = 0;
function setup() {
createCanvas(500, 500);
}
function draw() {
background(220);
stroke("black");
strokeWeight(2);
var longestLen = 0;
var longestIndex = -1;
for (let i = 0; i < myLines.length; i++) {
myLines[i].display();
var len = myLines[i].getLength();
if(len > longestLen) {
longestLen = len;
longestIndex = i;
}
}
for(let i = 0; i < myLines.length; i++) {
if(i == longestIndex) {
strokeWeight(4);
stroke("red");
} else {
strokeWeight(2);
stroke("black");
}
myLines[i].display();
}
}
class MyLine {
constructor(x1, y1, x2, y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
display() {
line(this.x1, this.y1, this.x2, this.y2);
}
getLength() {
var out = dist(this.x1, this.y1, this.x2, this.y2);
return out;
}
}
class point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
function mousePressed() {
start = new point(pmouseX, pmouseY);
}
function mouseReleased() {
end = new point(mouseX, mouseY);
myLines.push(new MyLine(start.x, start.y, end.x, end.y));
}