mirror of
https://github.com/rough-stuff/rough.git
synced 2026-01-15 01:18:26 -05:00
101 lines
3.9 KiB
JavaScript
101 lines
3.9 KiB
JavaScript
export class Segment {
|
|
constructor(p1, p2) {
|
|
this.xi = Number.MAX_VALUE;
|
|
this.yi = Number.MAX_VALUE;
|
|
this.px1 = p1[0];
|
|
this.py1 = p1[1];
|
|
this.px2 = p2[0];
|
|
this.py2 = p2[1];
|
|
this.a = this.py2 - this.py1;
|
|
this.b = this.px1 - this.px2;
|
|
this.c = this.px2 * this.py1 - this.px1 * this.py2;
|
|
this._undefined = ((this.a === 0) && (this.b === 0) && (this.c === 0));
|
|
}
|
|
isUndefined() {
|
|
return this._undefined;
|
|
}
|
|
intersects(otherSegment) {
|
|
if (this.isUndefined() || otherSegment.isUndefined()) {
|
|
return false;
|
|
}
|
|
let grad1 = Number.MAX_VALUE;
|
|
let grad2 = Number.MAX_VALUE;
|
|
let int1 = 0, int2 = 0;
|
|
const a = this.a, b = this.b, c = this.c;
|
|
if (Math.abs(b) > 0.00001) {
|
|
grad1 = -a / b;
|
|
int1 = -c / b;
|
|
}
|
|
if (Math.abs(otherSegment.b) > 0.00001) {
|
|
grad2 = -otherSegment.a / otherSegment.b;
|
|
int2 = -otherSegment.c / otherSegment.b;
|
|
}
|
|
if (grad1 === Number.MAX_VALUE) {
|
|
if (grad2 === Number.MAX_VALUE) {
|
|
if ((-c / a) !== (-otherSegment.c / otherSegment.a)) {
|
|
return false;
|
|
}
|
|
if ((this.py1 >= Math.min(otherSegment.py1, otherSegment.py2)) && (this.py1 <= Math.max(otherSegment.py1, otherSegment.py2))) {
|
|
this.xi = this.px1;
|
|
this.yi = this.py1;
|
|
return true;
|
|
}
|
|
if ((this.py2 >= Math.min(otherSegment.py1, otherSegment.py2)) && (this.py2 <= Math.max(otherSegment.py1, otherSegment.py2))) {
|
|
this.xi = this.px2;
|
|
this.yi = this.py2;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
this.xi = this.px1;
|
|
this.yi = (grad2 * this.xi + int2);
|
|
if (((this.py1 - this.yi) * (this.yi - this.py2) < -0.00001) || ((otherSegment.py1 - this.yi) * (this.yi - otherSegment.py2) < -0.00001)) {
|
|
return false;
|
|
}
|
|
if (Math.abs(otherSegment.a) < 0.00001) {
|
|
if ((otherSegment.px1 - this.xi) * (this.xi - otherSegment.px2) < -0.00001) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return true;
|
|
}
|
|
if (grad2 === Number.MAX_VALUE) {
|
|
this.xi = otherSegment.px1;
|
|
this.yi = grad1 * this.xi + int1;
|
|
if (((otherSegment.py1 - this.yi) * (this.yi - otherSegment.py2) < -0.00001) || ((this.py1 - this.yi) * (this.yi - this.py2) < -0.00001)) {
|
|
return false;
|
|
}
|
|
if (Math.abs(a) < 0.00001) {
|
|
if ((this.px1 - this.xi) * (this.xi - this.px2) < -0.00001) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return true;
|
|
}
|
|
if (grad1 === grad2) {
|
|
if (int1 !== int2) {
|
|
return false;
|
|
}
|
|
if ((this.px1 >= Math.min(otherSegment.px1, otherSegment.px2)) && (this.px1 <= Math.max(otherSegment.py1, otherSegment.py2))) {
|
|
this.xi = this.px1;
|
|
this.yi = this.py1;
|
|
return true;
|
|
}
|
|
if ((this.px2 >= Math.min(otherSegment.px1, otherSegment.px2)) && (this.px2 <= Math.max(otherSegment.px1, otherSegment.px2))) {
|
|
this.xi = this.px2;
|
|
this.yi = this.py2;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
this.xi = ((int2 - int1) / (grad1 - grad2));
|
|
this.yi = (grad1 * this.xi + int1);
|
|
if (((this.px1 - this.xi) * (this.xi - this.px2) < -0.00001) || ((otherSegment.px1 - this.xi) * (this.xi - otherSegment.px2) < -0.00001)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|