|
| 1 | +package com.thealgorithms.geometry; |
| 2 | +/** |
| 3 | + * This Class implements the Haversine formula to calculate the distance between two points on a sphere (like Earth) from their latitudes and longitudes. |
| 4 | + * |
| 5 | + * The Haversine formula is used in navigation and mapping to find the great-circle distance, |
| 6 | + * which is the shortest distance between two points along the surface of a sphere. It is often |
| 7 | + * used to calculate the "as the crow flies" distance between two geographical locations. |
| 8 | + * |
| 9 | + * The formula is reliable for all distances, including small ones, and avoids issues with |
| 10 | + * numerical instability that can affect other methods. |
| 11 | + * |
| 12 | + * @see "https://en.wikipedia.org/wiki/Haversine_formula" - Wikipedia |
| 13 | + */ |
| 14 | +public final class Haversine { |
| 15 | + |
| 16 | + // Average radius of Earth in kilometers |
| 17 | + private static final double EARTH_RADIUS_KM = 6371.0; |
| 18 | + |
| 19 | + /** |
| 20 | + * Private constructor to prevent instantiation of this utility class. |
| 21 | + */ |
| 22 | + private Haversine() { |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Calculates the great-circle distance between two points on the earth |
| 27 | + * (specified in decimal degrees). |
| 28 | + * |
| 29 | + * @param lat1 Latitude of the first point in decimal degrees. |
| 30 | + * @param lon1 Longitude of the first point in decimal degrees. |
| 31 | + * @param lat2 Latitude of the second point in decimal degrees. |
| 32 | + * @param lon2 Longitude of the second point in decimal degrees. |
| 33 | + * @return The distance between the two points in kilometers. |
| 34 | + */ |
| 35 | + public static double haversine(double lat1, double lon1, double lat2, double lon2) { |
| 36 | + // Convert latitude and longitude from degrees to radians |
| 37 | + double dLat = Math.toRadians(lat2 - lat1); |
| 38 | + double dLon = Math.toRadians(lon2 - lon1); |
| 39 | + |
| 40 | + double lat1Rad = Math.toRadians(lat1); |
| 41 | + double lat2Rad = Math.toRadians(lat2); |
| 42 | + |
| 43 | + // Apply the Haversine formula |
| 44 | + double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1Rad) * Math.cos(lat2Rad); |
| 45 | + double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); |
| 46 | + |
| 47 | + return EARTH_RADIUS_KM * c; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Main method for demonstrating the Haversine formula calculation. |
| 52 | + */ |
| 53 | + public static void main(String[] args) { |
| 54 | + System.out.println("Haversine formula usage examples:"); |
| 55 | + |
| 56 | + // Example 1: Distance between Paris, France and Tokyo, Japan |
| 57 | + double parisLat = 48.8566; |
| 58 | + double parisLon = 2.3522; |
| 59 | + double tokyoLat = 35.6895; |
| 60 | + double tokyoLon = 139.6917; |
| 61 | + |
| 62 | + double distance = haversine(parisLat, parisLon, tokyoLat, tokyoLon); |
| 63 | + System.out.printf("The distance between Paris and Tokyo is: %.2f km\n", distance); |
| 64 | + |
| 65 | + // Example 2: Distance between New York, USA and London, UK |
| 66 | + double nyLat = 40.7128; |
| 67 | + double nyLon = -74.0060; |
| 68 | + double londonLat = 51.5074; |
| 69 | + double londonLon = -0.1278; |
| 70 | + |
| 71 | + distance = haversine(nyLat, nyLon, londonLat, londonLon); |
| 72 | + System.out.printf("The distance between New York and London is: %.2f km\n", distance); |
| 73 | + } |
| 74 | +} |
0 commit comments