Java Distance Calculate distHaversineRAD(double lat1, double lon1, double lat2, double lon2)

Here you can find the source of distHaversineRAD(double lat1, double lon1, double lat2, double lon2)

Description

dist Haversine RAD

License

Apache License

Parameter

Parameter Description
lat1 The y coordinate of the first point, in radians
lon1 The x coordinate of the first point, in radians
lat2 The y coordinate of the second point, in radians
lon2 The x coordinate of the second point, in radians

Return

The distance between the two points, as determined by the Haversine formula, in radians.

Declaration

public static double distHaversineRAD(double lat1, double lon1, double lat2, double lon2) 

Method Source Code

//package com.java2s;
/*/*from  w  w w. j a v  a2 s . c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     *
     * @param lat1     The y coordinate of the first point, in radians
     * @param lon1     The x coordinate of the first point, in radians
     * @param lat2     The y coordinate of the second point, in radians
     * @param lon2     The x coordinate of the second point, in radians
     * @return The distance between the two points, as determined by the Haversine formula, in radians.
     */
    public static double distHaversineRAD(double lat1, double lon1, double lat2, double lon2) {
        //TODO investigate slightly different formula using asin() and min() http://www.movable-type.co.uk/scripts/gis-faq-5.1.html

        // Check for same position
        if (lat1 == lat2 && lon1 == lon2)
            return 0.0;
        double hsinX = Math.sin((lon1 - lon2) * 0.5);
        double hsinY = Math.sin((lat1 - lat2) * 0.5);
        double h = hsinY * hsinY + (Math.cos(lat1) * Math.cos(lat2) * hsinX * hsinX);
        return 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h));
    }
}

Related

  1. distEucl(float[] x1, float[] x2)
  2. distExtraBits(int dist)
  3. distFrom(double lat1, double lng1, double lat2, double lng2)
  4. distFrom(double lat1, double lng1, double lat2, double lng2)
  5. distHalf(double angA, double angB)
  6. distinctFrom(String string, String other)
  7. distL1(double[] h1, double[] h2)
  8. distL1(int[] h1, int[] h2)
  9. distortion(double[] xvalues, double[] yvalues, int[] include)