diff --git a/cmd/server/helpers_test.go b/cmd/server/helpers_test.go index 1b65bd99..9df42610 100644 --- a/cmd/server/helpers_test.go +++ b/cmd/server/helpers_test.go @@ -345,3 +345,29 @@ func TestWriteJSON(t *testing.T) { t.Errorf("expected 'value', got %v", body["key"]) } } + +func TestHaversineKm(t *testing.T) { + // Same point should be 0 + if d := haversineKm(37.0, -122.0, 37.0, -122.0); d != 0 { + t.Errorf("same point: expected 0, got %f", d) + } + + // SF to LA ~559km + d := haversineKm(37.7749, -122.4194, 34.0522, -118.2437) + if d < 550 || d > 570 { + t.Errorf("SF to LA: expected ~559km, got %f", d) + } + + // Symmetry + d1 := haversineKm(37.7749, -122.4194, 34.0522, -118.2437) + d2 := haversineKm(34.0522, -118.2437, 37.7749, -122.4194) + if d1 != d2 { + t.Errorf("not symmetric: %f vs %f", d1, d2) + } + + // Oslo to Stockholm ~415km (old Euclidean dLat*111, dLon*85 would give ~627km) + d = haversineKm(59.9, 10.7, 59.3, 18.0) + if d < 400 || d > 430 { + t.Errorf("Oslo to Stockholm: expected ~415km, got %f", d) + } +} diff --git a/public/analytics.js b/public/analytics.js index fd4eda3a..e0b32565 100644 --- a/public/analytics.js +++ b/public/analytics.js @@ -1488,9 +1488,9 @@ for (let i = 0; i < data.nodes.length - 1; i++) { const a = data.nodes[i], b = data.nodes[i+1]; if (a.lat && a.lon && b.lat && b.lon && !(a.lat===0&&a.lon===0) && !(b.lat===0&&b.lon===0)) { - const dLat = (a.lat - b.lat) * 111; - const dLon = (a.lon - b.lon) * 85; - const km = Math.sqrt(dLat*dLat + dLon*dLon); + const km = window.HopResolver && window.HopResolver.haversineKm + ? window.HopResolver.haversineKm(a.lat, a.lon, b.lat, b.lon) + : (() => { const R=6371, dLat=(b.lat-a.lat)*Math.PI/180, dLon=(b.lon-a.lon)*Math.PI/180, h=Math.sin(dLat/2)**2+Math.cos(a.lat*Math.PI/180)*Math.cos(b.lat*Math.PI/180)*Math.sin(dLon/2)**2; return R*2*Math.atan2(Math.sqrt(h),Math.sqrt(1-h)); })(); total += km; const cls = km > 200 ? 'color:var(--status-red);font-weight:bold' : km > 50 ? 'color:var(--status-yellow)' : 'color:var(--status-green)'; dists.push(`