mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-17 14:29:48 +02:00

The `node16` module resolution requires imports to use the `.js` file extension in type definitions. `@rollup/plugin-typescript` is needed to make this work with the Vite setup used by Mermaid. The module option for Mermaid internally is set to `nodenext`. This is needed to support `.json` imports. Note that setting `module` to `node16` or `nodenext` implies a matching `moduleResolution` value.
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
/* eslint "no-console": off */
|
|
|
|
import intersectLine from './intersect-line.js';
|
|
|
|
export default intersectPolygon;
|
|
|
|
/**
|
|
* Returns the point ({x, y}) at which the point argument intersects with the node argument assuming
|
|
* that it has the shape specified by polygon.
|
|
*
|
|
* @param node
|
|
* @param polyPoints
|
|
* @param point
|
|
*/
|
|
function intersectPolygon(node, polyPoints, point) {
|
|
var x1 = node.x;
|
|
var y1 = node.y;
|
|
|
|
var intersections = [];
|
|
|
|
var minX = Number.POSITIVE_INFINITY;
|
|
var minY = Number.POSITIVE_INFINITY;
|
|
if (typeof polyPoints.forEach === 'function') {
|
|
polyPoints.forEach(function (entry) {
|
|
minX = Math.min(minX, entry.x);
|
|
minY = Math.min(minY, entry.y);
|
|
});
|
|
} else {
|
|
minX = Math.min(minX, polyPoints.x);
|
|
minY = Math.min(minY, polyPoints.y);
|
|
}
|
|
|
|
var left = x1 - node.width / 2 - minX;
|
|
var top = y1 - node.height / 2 - minY;
|
|
|
|
for (var i = 0; i < polyPoints.length; i++) {
|
|
var p1 = polyPoints[i];
|
|
var p2 = polyPoints[i < polyPoints.length - 1 ? i + 1 : 0];
|
|
var intersect = intersectLine(
|
|
node,
|
|
point,
|
|
{ x: left + p1.x, y: top + p1.y },
|
|
{ x: left + p2.x, y: top + p2.y }
|
|
);
|
|
if (intersect) {
|
|
intersections.push(intersect);
|
|
}
|
|
}
|
|
|
|
if (!intersections.length) {
|
|
// console.log('NO INTERSECTION FOUND, RETURN NODE CENTER', node);
|
|
return node;
|
|
}
|
|
|
|
if (intersections.length > 1) {
|
|
// More intersections, find the one nearest to edge end point
|
|
intersections.sort(function (p, q) {
|
|
var pdx = p.x - point.x;
|
|
var pdy = p.y - point.y;
|
|
var distp = Math.sqrt(pdx * pdx + pdy * pdy);
|
|
|
|
var qdx = q.x - point.x;
|
|
var qdy = q.y - point.y;
|
|
var distq = Math.sqrt(qdx * qdx + qdy * qdy);
|
|
|
|
return distp < distq ? -1 : distp === distq ? 0 : 1;
|
|
});
|
|
}
|
|
return intersections[0];
|
|
}
|