From f348daeea352cda83df14fd0f6d9411243e0ea5c Mon Sep 17 00:00:00 2001 From: Stream Date: Tue, 5 May 2026 23:50:01 +0300 Subject: [PATCH] auto-sync: 2026-05-05 23:50:01 --- tasks/enduro-trails/prototype/static/app.js | 23 ++++++++------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/tasks/enduro-trails/prototype/static/app.js b/tasks/enduro-trails/prototype/static/app.js index cfcaf6b..55ad66a 100644 --- a/tasks/enduro-trails/prototype/static/app.js +++ b/tasks/enduro-trails/prototype/static/app.js @@ -549,7 +549,7 @@ function formatSegmentDist(m) { // Returns array of route-distance segments (meters) for each waypoint. // segDists[0] = 0, segDists[i] = distance along route geometry from wp[i-1] to wp[i]. -// Falls back to haversine if route geometry is unavailable or snap fails. +// First waypoint snaps to geometry[0], last to geometry[n-1] — guarantees sum == full route length. function getRouteSegmentDistances() { const route = routeResults[activeRouteIdx]; if (!route || !route.geometry || !route.geometry.coordinates) return null; @@ -561,10 +561,9 @@ function getRouteSegmentDistances() { // Convert geometry coords to {lat, lon} for haversineM const geoPts = coords.map(([lon, lat]) => ({ lat, lon })); - // Snap each waypoint to the nearest geometry point index + // Snap each waypoint to nearest geometry index const snapIdx = routeWaypoints.map(wp => { - let bestIdx = 0; - let bestDist = Infinity; + let bestIdx = 0, bestDist = Infinity; for (let j = 0; j < n; j++) { const d = haversineM(wp, geoPts[j]); if (d < bestDist) { bestDist = d; bestIdx = j; } @@ -572,17 +571,20 @@ function getRouteSegmentDistances() { return bestIdx; }); - // For each segment i→i+1, sum haversine along geometry from snapIdx[i] to snapIdx[i+1] + // Force first waypoint → geometry start, last waypoint → geometry end + // This ensures segments sum exactly to the full route geometry length + snapIdx[0] = 0; + snapIdx[snapIdx.length - 1] = n - 1; + + // For each segment i→i+1, sum haversine along geometry points const segDists = [0]; for (let i = 1; i < routeWaypoints.length; i++) { const from = snapIdx[i - 1]; const to = snapIdx[i]; if (from === to) { - // Same snap point — fallback to straight-line segDists.push(haversineM(routeWaypoints[i - 1], routeWaypoints[i])); continue; } - // Walk geometry in the correct direction const step = to > from ? 1 : -1; let dist = 0; for (let j = from; j !== to; j += step) { @@ -591,13 +593,6 @@ function getRouteSegmentDistances() { segDists.push(dist); } - // Scale segments so their sum equals route.distance_m exactly - const total = segDists.slice(1).reduce((a, b) => a + b, 0); - if (total > 0 && route.distance_m > 0) { - const scale = route.distance_m / total; - for (let i = 1; i < segDists.length; i++) segDists[i] *= scale; - } - return segDists; }