Today's assignment states that I need to write a complete java program which will calculate the relative orthodromic distance between two sets of geographic coordinates declared by the user.
When the user enters latitudes, it should be in degrees with negative numbers representing South latitudes. Similarly, the longitudes should be degrees with negative numbers representing West longitudes. The GCC program below will provide me with the correct positive and negative values. My program must print out the computed distance as an integer value rounded up. I am also required to use the "add half and cast" approach.
When dealing with distances on a globe of the earth, I am able to compute this distance by solving the following formulas:
RADIUS = the radius of the earth = 6391.2 km, declare as a constant
lat1 = the latitude of the first place, user input
long1 = the longitude of the first place, user input
lat2 = the latitude of the second place, user input
long2 = the longitude of the second place, user input
lat1_rad = lat1 converted to radians
long1_rad = long1 converted to radians
lat2_rad = lat2 converted to radians
long2_rad = long2 converted to radians
x1 = sin(lat1_rad)
z1 = cos(lat1_rad)
x2 = sin(lat2_rad)
z2 = cos(lat2_rad)
The website listed below is called the Great Circle Mapper. It allows me to find the latitude and longitude for any airport in the world. I will be able to use these locations to find the distances and the shortest path between two points.
I am required to use the site to verify that my program is computing the correct results. I should also note that it is not a requirement for my distances to be identical to those given by GC Mapper; however, they should be reasonably close.
Some example locations include:
- YQU - Grande Prairie airport
- YEG - Edmonton International airport
- LHR - London Heathrow Airport
- JFK - John F Kennedy International airport, New York
Note: My professor used an Earth radius which is about 20 Km larger than the accepted mean Earth radius (since Earth is not perfectly spherical). This modification in radius gives results which are closer to those computed by GC Mapper (which uses a different algorithm for its computations).
Example run:
Great Circle Distance Program
Start Location
Enter latitude :55.179722
Enter Longitude :-118.884999
End Location
Enter latitude :51.4775
Enter Longitude :-0.461388
Computed Distance : 6890 Km
The resulting code that I wrote to solve this problem is as follows:
import java.util.Scanner;
/**
* SRN: 507-147-9
*/
public class GCC_Program {
public static void main(String[] args) {
// new input
Scanner input = new Scanner(System.in);
// define vars
final double radius = 6391.2;
double lat1, long1, lat2, long2, lat1_rad, long1_rad, lat2_rad, long2_rad, x1, z1, x2, z2, x3, a;
int distance;
// Program name
System.out.println("Great Circle Distance Program");
// prompt coordinates
System.out.println();
System.out.println("Start Location");
System.out.print("Enter latitude : "); //prompt lat1
lat1 = input.nextDouble(); // store input as "lat1"
System.out.print("Enter longitude : "); //prompt long1
long1 = input.nextDouble(); // store input as "long1"
System.out.println();
System.out.println("End Location");
System.out.print("Enter latitude : "); //prompt lat2
lat2 = input.nextDouble(); // store input as "lat2"
System.out.print("Enter longitude : "); //prompt long2
long2 = input.nextDouble(); // store input as "long2"
System.out.println();
// define formulas
lat1_rad = Math.toRadians(lat1);
long1_rad = Math.toRadians(long1);
lat2_rad = Math.toRadians(lat2);
long2_rad = Math.toRadians(long2);
x1 = Math.sin(lat1_rad);
z1 = Math.cos(lat1_rad);
x2 = Math.sin(lat2_rad);
z2 = Math.cos(lat2_rad);
x3 = Math.cos(long2_rad - long1_rad);
a = (x1 * x2) + (z1 * z2 * x3);
distance = (int) ((radius * Math.acos(a)) + 0.5);
// print results
System.out.println("Computed Distance : " + distance + " Km");
}
}
With this, I do have a question regarding whether or not there's a way I can increase the accuracy of my computed distance in relation to the real-life computed distance, while simultaneously retaining the simplicity of the program? In other words, would I be able to implement a more accurate solution which doesn't stray too far from simple IO (input/output), arithmetic operators, Math methods, and conditional/iterative statements?
EDIT: I am unable to find any documentation concerning the GCC algorithm, and therefore I'm at a crossroads of whether my code is accurate enough to hand in...