agmission/Development/server/helpers/geometry/zone.js

60 lines
1.6 KiB
JavaScript

const Bound = require('./rectbound').Bound;
//type: enum ZoneType { ZONE_NONE, ZONE_NORMAL, ZONE_EXCLUSION, ZONE_DISPLAY, ZONE_VFR };
const zoneType = Object.freeze({ "NONE": 0, "NORMAL": 1, "XCL": 2 });
let zone = function (id, name, type, points, hasLines = false) {
this.init();
if (points && points.length) {
this.points = points;
// Update bounding box
for (let i = 0; i < this.points.length; i++) {
this.bounds.updateP(this.points[i]);
}
}
this.id = id;
this.name = name;
this.type = type;
this.hasLines = hasLines;
this.bbox = [this.bounds.xmin, this.bounds.ymin, this.bounds.xmax, this.bounds.ymax];
}
zone.prototype.init = function () {
this.points = [];
this.bounds = new Bound();
this.type = zoneType.NORMAL;
}
// zone.prototype.toClockwiseOrder = function () {
// if (this.area() > 0 && this.points.length >= 4) {
// const max = div(count(), 2).quot + 1, j = 0;
// exchange(0, 1);
// for (int i = 2; i < max; i++) {
// exchange(i, (count() - 1) - j);
// j++;
// }
// return 1;
// }
// return 0;
// }
zone.prototype.updateBounds = function () {
this.bounds.init();
for (let i = 0; i < this.points.length; i++)
this.bounds.updateP(this.points[i]);
}
zone.prototype.area = function () {
let _area = 0, n = this.points.length;
if (n < 3)
return 0; // not a polygon
let j;
for (let i = 0; i < n; i++) {
j = (i + 1) % n;
_area += this.points[i].x * this.points[j].y;
_area -= this.points[i].y * this.points[j].x;
}
return _area * 0.5;
}
module.exports = { Zone: zone, Type: zoneType }