Java Angle Between AngleBetweenDegrees(double longitudeFirstBody, double latitudeFirstBody, double longitudeSecondBody, double latitudeSecondBody)

Here you can find the source of AngleBetweenDegrees(double longitudeFirstBody, double latitudeFirstBody, double longitudeSecondBody, double latitudeSecondBody)

Description

Angle Between Degrees

License

Open Source License

Declaration

public static double AngleBetweenDegrees(double longitudeFirstBody, double latitudeFirstBody,
            double longitudeSecondBody, double latitudeSecondBody) 

Method Source Code

//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;
    }
}

Related

  1. angle_between(double x1, double y1, double z1, double x2, double y2, double z2)
  2. angleBetween(double[] v1, double[] v2)
  3. angleBetween(final double ax, final double ay, final double bx, final double by)
  4. angleBetween2Lines(double l1x1, double l1y1, double l1x2, double l1y2, double l2x1, double l2y1, double l2x2, double l2y2)
  5. angleBetween2LinesVectorAlgebra(double x11, double y11, double x12, double y12, double x21, double y21, double x22, double y22)
  6. angleBetweenPoints(double ax, double ay, double bx, double by)
  7. AngleBetweenVectors(double X1, double Y1, double Z1, double X2, double Y2, double Z2)