// File: GreatCircle.java // Written by: Deepak Kumar // Date: September 18, 2018 // Purpose: a Java program that takes four double // command-line arguments—x1, y1, x2, y2—(the latitude and // longitude, in degrees, of two points on the earth) and // prints the great-circle distance between them. // The great-circle distance, d (in nautical miles) is // given by the following equation: // // d = E * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2)) // // Where the earth’s radius, E = 3986 miles. // The distance, d will be miles. // // Example: To compute the distance between // Philadelphia (39.9526 N and 75.1652 W), and // Honolulu (21.3069 N and 157.8583 W) // // [xena@codewarrior ~]\$ java GreatCircle 39.9526 75.1652 21.3069 157.8583 // 4945.290632589314 miles public class GreatCircle { public static void main(String[] args) { // Input lat and long for source and destination double x1 = Math.toRadians(Double.parseDouble(args[0])); double y1 = Math.toRadians(Double.parseDouble(args[1])); double x2 = Math.toRadians(Double.parseDouble(args[2])); double y2 = Math.toRadians(Double.parseDouble(args[3])); double E = 3986.0; // Miles // double E = 6367.0; // KM double distance; distance = E * Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2)); // Output System.out.println(distance + " miles"); } // main() } // class GreatCircle