class Geo {

    public static double distance(double lat1, double lon1, double lat2, double lon2) {
        final double lat1r = Math.toRadians(lat1);
        final double lat2r = Math.toRadians(lat2);
        final double delta_lat = lat2r - lat1r;
        final double delta_lon = Math.toRadians(lon2 - lon1);

        final double sin_lat = Math.sin(delta_lat / 2);
        final double sin_lon = Math.sin(delta_lon / 2);

        // Haversine formula
        final double a = sin_lat * sin_lat + Math.cos(lat1r) * Math.cos(lat2r) * sin_lon * sin_lon;
        final double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return R * c;
    }

}