@mlightcad/geometry-engine
Version:
The geometry-engine package provides comprehensive geometric entities, mathematical operations, and transformations for 2D and 3D space. This package mimics AutoCAD ObjectARX's AcGe (Geometry) classes and provides the mathematical foundation for CAD opera
56 lines • 1.99 kB
JavaScript
import { AcGeBox2d } from '../math';
import { DEFAULT_TOL } from './AcGeTol';
/**
* Determine if the 2d point is inside the polygon
* @param point 2d point to check whether it is in the specified polygon
* @param polygon an polygon consisted by 2d point array
* @param includeOnSide if ture, return true if the specified point is inside the polygon or the polygon border.
* @returns Return true if the 2d point is inside the polygon
*/
function isPointInPolygon(point, polygon, includeOnSide) {
if (includeOnSide === void 0) { includeOnSide = false; }
var x = point.x, y = point.y;
var inside = false;
var len = polygon.length;
for (var i = 0, j = len - 1; i < len; j = i++) {
var xi = polygon[i].x, yi = polygon[i].y;
var xj = polygon[j].x, yj = polygon[j].y;
var isInSide = yi > y !== yj > y;
if (includeOnSide) {
isInSide = yi >= y !== yj >= y;
}
var intersect = isInSide && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
if (intersect) {
inside = !inside;
}
}
return inside;
}
function isPolygonIntersect(polygon1, polygon2) {
if (polygon1.length === 0 || polygon2.length === 0) {
return false;
}
// fast exclude
var tempBox1 = new AcGeBox2d().setFromPoints(polygon1);
var tempBox2 = new AcGeBox2d().setFromPoints(polygon2);
if (!tempBox1.intersectsBox(tempBox2)) {
return false;
}
for (var i = 0; i < polygon1.length;) {
if (isPointInPolygon(polygon1[i], polygon2, true)) {
return true;
}
if (i < polygon1.length - 1 &&
DEFAULT_TOL.equalPoint2d(polygon1[i + 1], polygon1[i])) {
++i;
}
++i;
}
return false;
}
var AcGeGeometryUtil = {
isPointInPolygon: isPointInPolygon,
isPolygonIntersect: isPolygonIntersect
};
export { isPointInPolygon, isPolygonIntersect, AcGeGeometryUtil };
//# sourceMappingURL=AcGeGeometryUtil.js.map