Java Distance Calculate computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2, float[] results)

Here you can find the source of computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2, float[] results)

Description

Fast calculation with Cos/Sin/Atan over LockUpTable

License

Open Source License

Parameter

Parameter Description
lat1 a parameter
lon1 a parameter
lat2 a parameter
lon2 a parameter
results a parameter

Declaration

private static void computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2,
        float[] results) 

Method Source Code

//package com.java2s;
/* //from  w w w .  java  2s.  c  o  m
 * Copyright (C) 2014 team-cachebox.de
 *
 * Licensed under the : GNU General Public License (GPL);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * 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 {
    /**
     * precalculation of Math.PI / 180.0
     */
    static public final double DEG_RAD = Math.PI / 180.0;
    /**
     * WGS84 major axis = 6378137.0
     */
    static public final double WGS84_MAJOR_AXIS = 6378137.0;

    /**
     * Fast calculation with Cos/Sin/Atan over LockUpTable
     * 
     * @param lat1
     * @param lon1
     * @param lat2
     * @param lon2
     * @param results
     */
    private static void computeDistanceAndBearingFast(double lat1, double lon1, double lat2, double lon2,
            float[] results) {
        double longitude1 = lon1;
        double longitude2 = lon2;
        double latitude1 = Math.toRadians(lat1);
        double latitude2 = Math.toRadians(lat2);

        lat1 *= DEG_RAD;
        lon1 *= DEG_RAD;
        lat2 *= DEG_RAD;
        lon2 *= DEG_RAD;

        int IntWGS84_MAJOR_AXIS = (int) WGS84_MAJOR_AXIS;

        results[0] = (float) ((IntWGS84_MAJOR_AXIS) * Math
                .acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos((lon2 - lon1))));

        // results[0] = (float) Distance;

        if (results.length > 1) {

            double longDiff = Math.toRadians(longitude2 - longitude1);
            double y = Math.sin(longDiff) * Math.cos(latitude2);
            double x = Math.cos(latitude1) * Math.sin(latitude2)
                    - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(longDiff);

            double angle = Math.toDegrees(Math.atan2(y, x));
            results[1] = (float) (angle);
            if (results.length > 2) {
                results[2] = results[1];
            }
        }
    }
}

Related

  1. computeDistance(double lat1, double lon1, double lat2, double lon2)
  2. computeDistance(Double startLat, Double startLon, Double endLat, Double endLon)
  3. computeDistance(final int[] position1, final int[] position2)
  4. computeDistance(int p1x, int p1y, int p2x, int p2y)
  5. computeDistance(long[] a, long[] b)
  6. computeDistanceImpl(final String firstStringInAlgorithm, final String secondStringInAlgorithm)
  7. computeDistanceInMiles(Double lat1, Double lon1, Double lat2, Double lon2)
  8. computeDistanceMap(int[][] image)
  9. computeDistanceSqr(double t, double b1, double b2, double b3, double b4, double b5, double b6)