File size: 1,173 Bytes
96bba34 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import type { PathPoint } from "../api";
export interface XY {
x: number;
y: number;
}
export function projectTracks(
tracks: PathPoint[][],
width: number,
height: number,
pad = 28,
): XY[][] {
const all = tracks.flat();
if (all.length === 0) return tracks.map(() => []);
const lat0 = all.reduce((sum, p) => sum + p.lat, 0) / all.length;
const k = Math.cos((lat0 * Math.PI) / 180);
const xs = all.map((p) => p.lon * k);
const ys = all.map((p) => p.lat);
const minX = Math.min(...xs);
const maxX = Math.max(...xs);
const minY = Math.min(...ys);
const maxY = Math.max(...ys);
const spanX = maxX - minX || 1e-6;
const spanY = maxY - minY || 1e-6;
const scale = Math.min((width - 2 * pad) / spanX, (height - 2 * pad) / spanY);
const offX = (width - scale * spanX) / 2;
const offY = (height - scale * spanY) / 2;
return tracks.map((track) =>
track.map((p) => ({
x: offX + (p.lon * k - minX) * scale,
y: height - (offY + (p.lat - minY) * scale),
})),
);
}
export function toPath(points: XY[]): string {
return points.map((p, i) => `${i === 0 ? "M" : "L"}${p.x.toFixed(1)},${p.y.toFixed(1)}`).join(" ");
}
|