point-is-in-polygon
Version:
The `point-is-in-polygon` package is a lightweight package that was developed to check if the coordinates of a point are in the coordinates of a polygon.
128 lines (127 loc) • 4.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Point = void 0;
var line_string_1 = require("../line-string/line-string");
var Point = /** @class */ (function () {
function Point(_a) {
var x = _a.x, y = _a.y;
this.x = x;
this.y = y;
Object.freeze(this);
}
Point.prototype.calc = function (x1, y1, x2, y2, x3, y3, x4, y4) {
var denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
var t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denominator;
var u = ((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) / denominator;
return {
t: t,
u: u,
};
};
Point.prototype.haveIntersectionPoint = function (x1, y1, x2, y2, x3, y3, x4, y4) {
var _a = this.calc(x1, y1, x2, y2, x3, y3, x4, y4), t = _a.t, u = _a.u;
if (t >= 0 && t <= 1 && u >= 0 && u <= 1)
return true;
return false;
};
Point.prototype.getIntersectionPoint = function (x1, y1, x2, y2, x3, y3, x4, y4) {
var t = this.calc(x1, y1, x2, y2, x3, y3, x4, y4).t;
var intersectionPointX = x1 + t * (x2 - x1);
var intersectionPointY = y1 + t * (y2 - y1);
return {
x: intersectionPointX,
y: intersectionPointY,
};
};
Point.prototype.createIntersectionLines = function (polygon) {
var maxX = polygon.coordinates.sort(function (a, b) { return b.x - a.x; })[0].x;
var minX = polygon.coordinates.sort(function (a, b) { return a.x - b.x; })[0].x;
var maxY = polygon.coordinates.sort(function (a, b) { return b.y - a.y; })[0].y;
var minY = polygon.coordinates.sort(function (a, b) { return a.y - b.y; })[0].y;
return [
new line_string_1.LineString([
{
x: this.x,
y: this.y,
},
{
x: maxX,
y: this.y,
},
]),
new line_string_1.LineString([
{
x: this.x,
y: this.y,
},
{
x: minX,
y: this.y,
},
]),
new line_string_1.LineString([
{
x: this.x,
y: this.y,
},
{
x: this.x,
y: maxY,
},
]),
new line_string_1.LineString([
{
x: this.x,
y: this.y,
},
{
x: this.x,
y: minY,
},
]),
];
};
Point.prototype.isInPolygon = function (polygon) {
var _this = this;
var intersectionLines = this.createIntersectionLines(polygon);
var intersectionPoints = [];
var _loop_1 = function (i) {
intersectionLines.forEach(function (intersectionLine) {
var intersections = [];
var x1 = intersectionLine.points[0].x;
var y1 = intersectionLine.points[0].y;
var x2 = intersectionLine.points[1].x;
var y2 = intersectionLine.points[1].y;
var x3 = polygon.coordinates[i - 1].x;
var y3 = polygon.coordinates[i - 1].y;
var x4 = polygon.coordinates[i].x;
var y4 = polygon.coordinates[i].y;
var haveIntersectionPoint = _this.getIntersectionPoint(x1, y1, x2, y2, x3, y3, x4, y4);
if (haveIntersectionPoint) {
var intersectionPoint = _this.getIntersectionPoint(x1, y1, x2, y2, x3, y3, x4, y4);
intersections.push(intersectionPoint);
}
var uniqueIntersections = [];
intersections.forEach(function (_a) {
var x = _a.x, y = _a.y;
var isUnique = !uniqueIntersections.find(function (coordinate) {
return coordinate.x === x && coordinate.y === y;
});
if (isUnique)
uniqueIntersections.push({ x: x, y: y });
});
intersectionPoints.push(uniqueIntersections);
});
};
for (var i = 1; i < polygon.coordinates.length; i++) {
_loop_1(i);
}
return !intersectionPoints
.map(function (coordinates) {
return coordinates.length % 2 === 0;
})
.some(function (result) { return result === true; });
};
return Point;
}());
exports.Point = Point;