From 05989d06fa7eca5a0832eb2e8c198ed323063af1 Mon Sep 17 00:00:00 2001 From: Knut Sveidqvist Date: Fri, 13 Sep 2024 08:37:07 +0200 Subject: [PATCH] Fix for issue with incorrect calculations of which edge a line intersects with --- .../layout-algorithms/fixed/index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js b/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js index 8c059fcfc..28ad30df8 100644 --- a/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js +++ b/packages/mermaid/src/rendering-util/layout-algorithms/fixed/index.js @@ -30,16 +30,21 @@ let nodeDB = new Map(); const calcIntersectionPoint = (node, point) => { const intersection = node.intersect(point); - const dx = intersection.x - node.x; - const dy = intersection.y - node.y; - let pos = 'l'; + const dx = point.x - node.x; + const dy = point.y - node.y; - // Determine the position of the intersection relative to the node - if (Math.abs(dx) > Math.abs(dy)) { - pos = dx > 0 ? 'r' : 'l'; // Right or left + const angle = Math.atan2(dy, dx) * (180 / Math.PI); + + let pos; + if (angle > -30 && angle <= 30) { + pos = 'r'; // Right + } else if (angle > 30 && angle <= 150) { + pos = 'b'; // Bottom + } else if (angle <= -30 && angle > -150) { + pos = 't'; // Top } else { - pos = dy > 0 ? 'b' : 't'; // Bottom or top + pos = 'l'; // Left } return { x: intersection.x, y: intersection.y, pos };