Returns haversine() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere. - Java java.lang

Java examples for java.lang:Math Trigonometric Function

Description

Returns haversine() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere.

Demo Code


//package com.java2s;
import static java.lang.Math.*;

public class Main {
    /**/*from  w w w  .  j a v a2 s .c om*/
     * Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere.
     */
    static double havDistance(double lat1, double lat2, double dLng) {
        return hav(lat1 - lat2) + hav(dLng) * cos(lat1) * cos(lat2);
    }

    /**
     * Returns haversine(angle-in-radians).
     * hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2.
     */
    static double hav(double x) {
        double sinHalf = sin(x * 0.5);
        return sinHalf * sinHalf;
    }
}

Related Tutorials