From 1612ae5978d157cd6aec564fa2a3e15dc07f40d1 Mon Sep 17 00:00:00 2001 From: Fennel Kora Date: Sun, 14 Apr 2024 00:50:19 -0400 Subject: [PATCH] changed routeSequences to vector of string arrays to fit new direction format. updated calculateRoutes to handle the new formatting. removed distanceBetweenPoints function as it's no longer needed --- Route.java | 66 +++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/Route.java b/Route.java index d7f0be7..a48c565 100644 --- a/Route.java +++ b/Route.java @@ -1,11 +1,13 @@ +import java.util.Vector; + public class Route { private double[] currentLocation; //Lat-Long coordinates for current location - private double[][][] routeSequences; //3D array for routes. 1st dim selects route, 2nd dim selects route point, 3rd dim selects lat/long/speed/tolls (indices 0-2) + private Vector> routeSequences; //3D array for routes. 1st dim selects route, 2nd dim selects route point, 3rd dim selects lat/long/speed/tolls (indices 0-2) private int routeSelected; private boolean tollsFlag; //stores user setting for whether to avoid tolls private boolean hwyFlag; //stores user preference for whether to avoid major highways - public double[] timeEstimate; //time estimate in seconds - public float[] tollsAmount; //stores toll amount if toll roads are used + public Vector timeEstimate; //time estimate in seconds + public Vector tollsAmount; //stores toll amount if toll roads are used public Destination currentDestination; //uses user-created class Destination private void updateCurrentLocation() { @@ -20,15 +22,18 @@ public class Route { //ideally a query would go out using multiple pathfiding algorithms to get route options updateCurrentLocation(); HelperFunctions apiCallToRouting = new HelperFunctions(); - routeSequences[0] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 1 - timeEstimate[0] = estimateTime(routeSequences[0]); - estimateTolls(routeSequences[0]); - routeSequences[1] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 2 - timeEstimate[1] = estimateTime(routeSequences[1]); - estimateTolls(routeSequences[1]); - routeSequences[2] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 3 - timeEstimate[2] = estimateTime(routeSequences[1]); - estimateTolls(routeSequences[1]); + //route option 1 + routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag)); + timeEstimate[0] = estimateTime(routeSequences.get(0)); + estimateTolls(routeSequences.get(0)); + //route option 2 + routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag)); + timeEstimate[1] = estimateTime(routeSequences.get(1)); + estimateTolls(routeSequences.get(1)); + //route option 3 + routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag)); + timeEstimate[2] = estimateTime(routeSequences.get(2)); + estimateTolls(routeSequences.get(2)); } public int enumRoutes() { @@ -49,45 +54,14 @@ public class Route { return runningTolls; } - public double estimateTime(double[][] route) { + public double estimateTime(Vector route) { //calculates distances between route segments, divided by speed in MPH //the API would return expected speed due to traffic conditions as part of the speed (index 2) in the route array double runningTime = 0; //steps through subsequence route points to get distances, calculates time based on speed estimate, and accumulates on runningTime - for (int i = 0; i < route.length; i++) { - runningTime += distanceBetweenPoints(route[i], route[i+1]) / route[i][2]; + for (String[] el : route) { + runningTime += Double.parseDouble(el[1])/Double.parseDouble(el[2]); } return runningTime; } - - private double distanceBetweenPoints(double[] pointA, double[] pointB) { - /** - * Code excerpt by StackOverflow user Dommer, adapted for this application's use - * https://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude/3694410#3694410 - * - * Calculate distance between two points in latitude and longitude taking - * into account height difference. If you are not interested in height - * difference pass 0.0. Uses Haversine method as its base. - * - * lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in miles - * el2 End altitude in miles - * @returns Distance - */ - final int R = 6371; // Radius of the earth - int el1 = 0, el2 = 0; - - double latDistance = Math.toRadians(pointA[0] - pointB[0]); - double lonDistance = Math.toRadians(pointA[1] - pointB[1]); - double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) - + Math.cos(Math.toRadians(pointA[0])) * Math.cos(Math.toRadians(pointB[0])) - * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); - double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); - double distance = R * c * 0.6213711922; // convert to miles - - double height = el1 - el2; - - distance = Math.pow(distance, 2) + Math.pow(height, 2); - - return Math.sqrt(distance); - } }