From e1e2c479affa20387c3416298bd2b69b0ae78bb4 Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Sun, 28 Dec 2025 14:35:39 -0800 Subject: [PATCH] Clean up line intersection function --- html/images/space.svg | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/html/images/space.svg b/html/images/space.svg index a5b7ab4..7e5dd5c 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -529,10 +529,13 @@ return edge || actualCorner; } - function lineIntxnPt({ x1, y1, x2, y2 }, {x1: x3, y1: y3, x2: x4, y2: y4 }) { + function lineIntxnPt({ x1, y1, x2, y2 }, { x1: x3, y1: y3, x2: x4, y2: y4 }) { // https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line - const x = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); - const y = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); + const denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); + const l1Det = x1 * y2 - y1 * x2; + const l2Det = x3 * y4 - y3 * x4; + const x = (l1Det * (x3 - x4) - (x1 - x2) * l2Det) / denominator; + const y = (l1Det * (y3 - y4) - (y1 - y2) * l2Det) / denominator; return { x: x, y: y }; }