Here you can find the source of AngleBetweenDegrees(double longitudeFirstBody, double latitudeFirstBody, double longitudeSecondBody, double latitudeSecondBody)
public static double AngleBetweenDegrees(double longitudeFirstBody, double latitudeFirstBody, double longitudeSecondBody, double latitudeSecondBody)
//package com.java2s; //License from project: Open Source License public class Main { private static double return_result = 0; public static double AngleBetweenDegrees(double longitudeFirstBody, double latitudeFirstBody, double longitudeSecondBody, double latitudeSecondBody) { // converting coordinates to vectors double[] vectorFirst = latLonToVector(longitudeFirstBody, latitudeFirstBody); double[] vectorSecond = latLonToVector(longitudeSecondBody, latitudeSecondBody); // dot product calculation double dotproduct = vectorDotProduct(vectorFirst[0], vectorFirst[1], vectorFirst[2], vectorSecond[0], vectorSecond[1], vectorSecond[2]); // absolute values of vectors double absoluteFirst = vectorAbsoluteValue(vectorFirst[0], vectorFirst[1], vectorFirst[2]); double absoluteSecond = vectorAbsoluteValue(vectorSecond[0], vectorSecond[1], vectorSecond[2]); double angleResult = Math.acos(dotproduct / (absoluteFirst * absoluteSecond)); return angleResult; }/*from w ww . j a v a2 s .com*/ public static double[] latLonToVector(double longitude, double latitude) { // convert degrees to radians double RadLon = DegreesToRad(longitude); double RadLat = DegreesToRad(latitude); // convert Lat/lon to Cartesian coordinates double X = Math.cos(RadLat) * Math.cos(RadLon); double Y = Math.cos(RadLat) * Math.sin(RadLon); double Z = Math.sin(RadLat); double[] vector = new double[] { X, Y, Z }; return vector; } public static double vectorDotProduct(double X1, double Y1, double Z1, double X2, double Y2, double Z2) { double dorProduct = X1 * X2 + Y1 * Y2 + Z1 * Z2; return dorProduct; } public static double vectorAbsoluteValue(double X, double Y, double Z) { double absoluteValue = Math.sqrt(Math.pow(X, 2) + Math.pow(Y, 2) + Math.pow(Z, 2)); return absoluteValue; } public static double DegreesToRad(double degrees) { return_result = degrees * (Math.PI / 180); return return_result; } }