From 3ca46a065f8521c6456b9e048aef247c366786f4 Mon Sep 17 00:00:00 2001 From: Ouyang Leyan Date: Tue, 15 Oct 2024 23:42:48 +0200 Subject: [PATCH 1/2] more precise angle computation --- src/stylefunction.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/stylefunction.js b/src/stylefunction.js index 1787b361..6fccae77 100644 --- a/src/stylefunction.js +++ b/src/stylefunction.js @@ -835,11 +835,11 @@ export function stylefunction( const minY = Math.min(y1, y2); const maxX = Math.max(x1, x2); const maxY = Math.max(y1, y2); + const xM = midpoint[0]; + const yM = midpoint[1]; if ( - midpoint[0] >= minX && - midpoint[0] <= maxX && - midpoint[1] >= minY && - midpoint[1] <= maxY + Math.abs((y2-y1)*(xM-x1) - (x2-x1)*(yM-y1))<0.001 //midpoint is aligned with the segment + && xM<=maxX && xM>=minX ) { placementAngle = Math.atan2(y1 - y2, x2 - x1); break; From ae56027b71491f8a405feff82ce6fa92edebca2f Mon Sep 17 00:00:00 2001 From: Ouyang Leyan Date: Thu, 17 Oct 2024 21:35:16 +0200 Subject: [PATCH 2/2] fix lint issues --- src/stylefunction.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/stylefunction.js b/src/stylefunction.js index 6fccae77..f1d0a548 100644 --- a/src/stylefunction.js +++ b/src/stylefunction.js @@ -832,14 +832,15 @@ export function stylefunction( const x2 = coordinates[i + stride]; const y2 = coordinates[i + stride + 1]; const minX = Math.min(x1, x2); - const minY = Math.min(y1, y2); const maxX = Math.max(x1, x2); - const maxY = Math.max(y1, y2); const xM = midpoint[0]; const yM = midpoint[1]; + const dotProduct = + (y2 - y1) * (xM - x1) - (x2 - x1) * (yM - y1); if ( - Math.abs((y2-y1)*(xM-x1) - (x2-x1)*(yM-y1))<0.001 //midpoint is aligned with the segment - && xM<=maxX && xM>=minX + Math.abs(dotProduct) < 0.001 && //midpoint is aligned with the segment + xM <= maxX && + xM >= minX //midpoint is on the segment and not outside it ) { placementAngle = Math.atan2(y1 - y2, x2 - x1); break;