grid-apps-cmms/src/geo/line.js

71 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-06-03 22:39:30 -04:00
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
2017-04-18 22:22:29 -04:00
2017-04-18 17:09:33 -04:00
"use strict";
// dep: geo.base
gapp.register("geo.line", [], (root, exports) => {
2017-04-18 17:09:33 -04:00
const { base } = root;
2020-06-03 22:39:30 -04:00
2022-01-31 20:50:19 -05:00
class Line {
constructor(p1, p2, key) {
if (!key) key = [p1.key, p2.key].join(';');
2017-04-18 17:09:33 -04:00
this.p1 = p1;
this.p2 = p2;
this.key = key;
}
2022-01-31 20:50:19 -05:00
length() {
2017-04-18 17:09:33 -04:00
return Math.sqrt(this.length2());
2022-01-31 20:50:19 -05:00
}
2017-04-18 17:09:33 -04:00
/**
* @returns {number} square of length
*/
2022-01-31 20:50:19 -05:00
length2() {
2017-04-18 17:09:33 -04:00
return this.p1.distToSq2D(this.p2);
2022-01-31 20:50:19 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 20:50:19 -05:00
slope() {
return base.newSlope(this.p1.slopeTo(this.p2));
}
2017-04-18 17:09:33 -04:00
2022-01-31 20:50:19 -05:00
reverse() {
2020-06-03 22:39:30 -04:00
let t = this.p1;
2017-04-18 17:09:33 -04:00
this.p1 = this.p2;
this.p2 = t;
return this;
2022-01-31 20:50:19 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 20:50:19 -05:00
midpoint() {
2017-04-18 17:09:33 -04:00
return this.p1.midPointTo(this.p2);
2022-01-31 20:50:19 -05:00
}
2017-04-18 17:09:33 -04:00
2022-01-31 20:50:19 -05:00
isCollinear(line) {
2020-06-03 22:39:30 -04:00
let p1 = this.p1,
2017-04-18 17:09:33 -04:00
p2 = this.p2,
p3 = line.p1,
p4 = line.p2,
d1x = (p2.x - p1.x),
d1y = (p2.y - p1.y),
d2x = (p4.x - p3.x),
d2y = (p4.y - p3.y);
return Math.abs( (d2y * d1x) - (d2x * d1y) ) < 0.0001;
2022-01-31 20:50:19 -05:00
}
}
2017-04-18 17:09:33 -04:00
2022-01-31 20:50:19 -05:00
function newLine(p1, p2, key) {
return new Line(p1, p2, key);
}
2017-04-18 17:09:33 -04:00
2022-01-31 20:50:19 -05:00
function newOrderedLine(p1, p2, key) {
return p1.key < p2.key ? newLine(p1,p2,key) : newLine(p2,p1,key);
}
2017-04-18 17:09:33 -04:00
gapp.overlay(base, {
Line,
newLine,
newOrderedLine
});
2017-04-18 17:09:33 -04:00
});