show hovered point on elevation profile

This commit is contained in:
vcoppe committed 2026-11-08 15:06:03 +01:00
1 parent b80fa7d942
commit 21e27a4416
15 files changed
+260 -106

No files matched your search

+17 -27
View File
@@ -1,32 +1,6 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { closestPointIndex, closestPointIndexIn } from '$lib/geo/closest-point';
describe('closestPointIndex', () => {
const line = [0, 0, 1, 0, 2, 0, 2, 1];
it('has no point on an empty polyline', () => {
assert.equal(closestPointIndex([], { lng: 0, lat: 0 }), undefined);
});
it('is the only point of a polyline of one point', () => {
assert.equal(closestPointIndex([3, 4], { lng: 0, lat: 0 }), 0);
});
it('is the nearest end of the nearest segment', () => {
assert.equal(closestPointIndex(line, { lng: 0.2, lat: 0.1 }), 0);
assert.equal(closestPointIndex(line, { lng: 0.8, lat: 0.1 }), 1);
assert.equal(closestPointIndex(line, { lng: 1.6, lat: -0.1 }), 2);
assert.equal(closestPointIndex(line, { lng: 2.1, lat: 0.9 }), 3);
});
it('uses the distance to the segment, not to its ends', () => {
// far from every point but right next to the long segment
const coordinates = [0, 0, 10, 0, 10, 5];
assert.equal(closestPointIndex(coordinates, { lng: 7, lat: 0.01 }), 1);
assert.equal(closestPointIndex(coordinates, { lng: 3, lat: 0.01 }), 0);
});
});
import { closestPointIndexIn } from '$lib/geo/closest-point';
describe('closestPointIndexIn', () => {
const lng = [0, 1, 2, 2, 5, 6];
@@ -39,6 +13,22 @@ describe('closestPointIndexIn', () => {
assert.equal(closestPointIndexIn(lng, lat, 4, 6, { lng: 5.9, lat: 5.1 }), 5);
});
it('is the nearest end of the nearest segment', () => {
assert.equal(closestPointIndexIn(lng, lat, 0, 4, { lng: 0.2, lat: 0.1 }), 0);
assert.equal(closestPointIndexIn(lng, lat, 0, 4, { lng: 1.6, lat: -0.1 }), 2);
assert.equal(closestPointIndexIn(lng, lat, 0, 4, { lng: 2.1, lat: 0.9 }), 3);
});
it('uses the distance to the segment, not to its ends', () => {
// far from every point but right next to the long segment
const [x, y] = [
[0, 10, 10],
[0, 0, 5],
];
assert.equal(closestPointIndexIn(x, y, 0, 3, { lng: 7, lat: 0.01 }), 1);
assert.equal(closestPointIndexIn(x, y, 0, 3, { lng: 3, lat: 0.01 }), 0);
});
it('has no point in an empty range', () => {
assert.equal(closestPointIndexIn(lng, lat, 3, 3, { lng: 0, lat: 0 }), undefined);
assert.equal(closestPointIndexIn(lng, lat, 4, 2, { lng: 0, lat: 0 }), undefined);
+4 -19
View File
@@ -1,25 +1,10 @@
const metersPerDegree = 111320;
/**
* Index of the point of a polyline (flat `[lng, lat, ...]` coordinates) that is the closest to
* `point`, among the two ends of the segment of the polyline that passes the closest to it.
* `undefined` for an empty polyline. Planar approximation, like the one used to simplify tracks.
*/
export function closestPointIndex(
coordinates: ArrayLike<number>,
point: { lng: number; lat: number }
): number | undefined {
return closestIndex(
Math.floor(coordinates.length / 2),
(i) => coordinates[2 * i],
(i) => coordinates[2 * i + 1],
point
);
}
/**
* Like `closestPointIndex`, for the points `from` (included) to `to` (excluded) of a polyline
* given as the arrays of its longitudes and latitudes. The index is the one in the arrays.
* Index of the point, among the points `from` (included) to `to` (excluded) of a polyline given as
* the arrays of its longitudes and latitudes, that is the closest to `point`: one of the two ends
* of the segment of the polyline that passes the closest to it. The index is the one in the arrays,
* `undefined` for an empty range. Planar approximation, like the one used to simplify tracks.
*/
export function closestPointIndexIn(
lng: ArrayLike<number>,