mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-18 18:10:16 -04:00
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import getDistance from './getdistance';
|
|
|
|
export type Location = {
|
|
latitude: number;
|
|
longitude: number;
|
|
};
|
|
|
|
export type Path = {
|
|
from: Location;
|
|
to: Location;
|
|
waypoints: Location[];
|
|
};
|
|
|
|
export default function estimateOptimalPath(path: Path): {
|
|
path: Path;
|
|
distance: number;
|
|
} {
|
|
const { from, to, waypoints } = path;
|
|
let sequence = [from, to];
|
|
|
|
// Calculates all possible paths from the start to the end of the given path
|
|
// and returns the one with the minimum distance
|
|
for (let waypoint of waypoints) {
|
|
// Iterate over all possible insertion points for the waypoint
|
|
let minDistance = Infinity;
|
|
let insertionPoint = 1;
|
|
for (let i = 0; i < sequence.length - 1; i++) {
|
|
const [start, end] = sequence.slice(i, i + 2);
|
|
|
|
const distance = getDistance(start, waypoint, end);
|
|
if (distance < minDistance) {
|
|
minDistance = distance;
|
|
insertionPoint = i;
|
|
}
|
|
}
|
|
|
|
sequence = sequence
|
|
.slice(0, insertionPoint)
|
|
.concat([waypoint])
|
|
.concat(sequence.slice(insertionPoint));
|
|
}
|
|
|
|
const newWaypoints = sequence.slice(1, sequence.length - 1);
|
|
return {
|
|
path: {
|
|
from,
|
|
to,
|
|
waypoints: newWaypoints,
|
|
},
|
|
distance: getDistance(from, ...sequence, to),
|
|
};
|
|
}
|