xxxxxxxxxx
51
class Segment {
constructor(v1, v2) {
this.a = v1;
this.b = v2;
this.trias = [];
}
isAEnd(p) {
if (p == this.a || p == this.b) return true;
return false;
}
hasCommon(l) {
return this.isAEnd(l.a) || this.isAEnd(l.b);
}
calcValue(p) {
let m = (this.b.y - this.a.y) / (this.b.x - this.a.x);
let ans = p.y - this.a.y - m * (p.x - this.a.x);
return ans;
}
onSameSide(p1, p2) {
return this.calcValue(p1) * this.calcValue(p2) > 0;
}
delete(tr) {
for (let i = 0; i < this.trias.length; ++i)
if (this.trias[i].Tr == tr) {
this.trias.splice(i, 1);
}
}
length() {
return Math.sqrt(
Math.pow(this.a.x - this.b.x, 2) + Math.pow(this.a.y - this.b.y, 2)
);
}
isStable() {
if (this.trias.length == 1) return true;
let sum = 0;
for (let tr of this.trias) sum += tr.Tr.angles[tr.side];
return sum < Math.PI;
}
display() {
line(this.a.x, this.a.y, this.b.x, this.b.y);
}
}