JSFiddle - React, Tailwind, and code Playground
by Brock Whittaker
HTML
<canvas id="canvas"></canvas>
JavaScript
var rough = (function () {
'use strict';
function RoughSegmentRelation() {
return {
LEFT: 0,
RIGHT: 1,
INTERSECTS: 2,
AHEAD: 3,
BEHIND: 4,
SEPARATE: 5,
UNDEFINED: 6
};
}
class RoughSegment {
constructor(px1, py1, px2, py2) {
this.RoughSegmentRelationConst = RoughSegmentRelation();
this.px1 = px1;
this.py1 = py1;
this.px2 = px2;
this.py2 = py2;
this.xi = Number.MAX_VALUE;
this.yi = Number.MAX_VALUE;
this.a = py2 - py1;
this.b = px1 - px2;
this.c = px2 * py1 - px1 * py2;
this._undefined = ((this.a == 0) && (this.b == 0) && (this.c == 0));
}
isUndefined() {
return this._undefined;
}
compare(otherSegment) {
if (this.isUndefined() || otherSegment.isUndefined()) {
return this.RoughSegmentRelationConst.UNDEFINED;
}
var grad1 = Number.MAX_VALUE;
var grad2 = Number.MAX_VALUE;
var int1 = 0, int2 = 0;
var 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 this.RoughSegmentRelationConst.SEPARATE;
}
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 this.RoughSegmentRelationConst.INTERSECTS;
}
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 this.RoughSegmentRelationConst.INTERSECTS;
}
return this.RoughSegmentRelationConst.SEPARATE;
...