Skip to content

Commit

Permalink
route functions improved
Browse files Browse the repository at this point in the history
  • Loading branch information
unfiltered-syrup committed Dec 3, 2023
1 parent 141dbad commit ecc15d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
40 changes: 34 additions & 6 deletions back-end/getOptimizedRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ async function createGraph(routes, busstops) {
function getEuclideanDistance (a, b) {
let x1 = a[0];
let y1 = a[1];
let x2 = b.split(',')[0];
let y2 = b.split(',')[1];
let x2;
let y2;
try{
x2 = b.split(',')[0];
y2 = b.split(',')[1];
}
catch(err){
x2 = b[0];
y2 = b[1];
}

return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
}

Expand Down Expand Up @@ -91,23 +100,42 @@ async function findOptimalRoute(graph, routes, busstops, origin_lat, origin_lng,
let reachableFromOrigin = await findAllReachableStops(graph, origin);

let reachableFromDestination = await findAllReachableStops(graph, destination);
console.log('TOTAL REACHABLE STOPS: '+Number(reachableFromOrigin.length + reachableFromDestination.length))
// Calculate route distances
for (let originStop of reachableFromOrigin) {
for (let destinationStop of reachableFromDestination) {
let onSameRoute = await isOnSameRoute(graph, originStop, destinationStop, routes, busstops);
console.log('on same route result: '+onSameRoute)
if (onSameRoute.length > 0) {
let distanceToOriginStop = await getWalkingDistance(origin, originStop.coordinates);
let distanceFromDestinationStop = await getWalkingDistance(destinationStop.coordinates, destination);
let totalDistance = distanceToOriginStop + distanceFromDestinationStop;
console.log('fetched from Google Maps API, total distance: '+totalDistance);
let distanceToOriginStop
let distanceFromDestinationStop
let totalDistance
console.log('TOTAL REACHABLE STOPS2: '+reachableFromOrigin.length + reachableFromDestination.length)
if (Number(reachableFromOrigin.length + reachableFromDestination.length) < 15){

distanceToOriginStop = await getWalkingDistance(origin, originStop.coordinates);
distanceFromDestinationStop = await getWalkingDistance(destinationStop.coordinates, destination);
totalDistance = distanceToOriginStop + distanceFromDestinationStop;
console.log('fetched from Google Maps API, total distance: '+totalDistance);

}
else{
distanceToOriginStop = await getEuclideanDistance(origin, originStop.coordinates);
distanceFromDestinationStop = await getEuclideanDistance(destinationStop.coordinates, destination);
totalDistance = distanceToOriginStop + distanceFromDestinationStop;
console.log('getting euclidean distance, total distance: '+totalDistance);

}


// Check if this route is better than the current best
if (totalDistance < minTotalDistance) {
console.log('new optimal route found')
minTotalDistance = totalDistance;
optimalRoute = { originStop, destinationStop, onSameRoute };
}
}

}
}

Expand Down
3 changes: 1 addition & 2 deletions front-end/src/components/RoutesPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function RoutesPage() {
catch(typeError){
console.log('same input, no need to fetch new route');
}
if (fromLocation.name && toLocation.name) {
if (fromLocation.name && toLocation.name && fromLocation.name !== toLocation.name) {
awaitingData.current = true;
let reachableRoutes = fetch(`http://localhost:4000/getRoute`, {
method: "POST",
Expand Down Expand Up @@ -103,7 +103,6 @@ function RoutesPage() {
}
setRoutes(data);
setShowSubpage(true);
alert('Starting' + JSON.stringify(data[0]));
return data;
})
.catch((error) => {
Expand Down

0 comments on commit ecc15d9

Please sign in to comment.