Java Distance Calculate distAngle(double[] pos1, double[] pos2)

Here you can find the source of distAngle(double[] pos1, double[] pos2)

Description

Distance between two angles

License

Open Source License

Parameter

Parameter Description
pos1 first angle
pos2 second angle.

Return

the distance between two angles

Declaration

public static double distAngle(double[] pos1, double[] pos2) 

Method Source Code

//package com.java2s;
/*/*  w w w . j  a  v a2 s .c  om*/
 * Copyright (C) 2014 Jean-Christophe Malapert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Distance between two angles
     * @param pos1 first angle
     * @param pos2 second angle.
     * @return the distance between two angles
     */
    public static double distAngle(double[] pos1, double[] pos2) {
        double[] xyzPos1 = radec2xyz(pos1);
        double[] xyzPos2 = radec2xyz(pos2);
        double dot = xyzPos1[0] * xyzPos2[0] + xyzPos1[1] * xyzPos2[1] + xyzPos1[2] * xyzPos2[2];
        return Math.acos(dot / (normVector(xyzPos1) * normVector(xyzPos2)));
    }

    /**
     * Projects ra/dec on cartesian reference system.
     * @param pos position in the sky
     * @return the position in the cartesian reference system
     */
    public static double[] radec2xyz(double pos[]) {
        double[] xyz = new double[3];
        xyz[0] = Math.cos(pos[1]) * Math.cos(pos[0]);
        xyz[1] = Math.cos(pos[1]) * Math.sin(pos[0]);
        xyz[2] = Math.sin(pos[1]);
        return xyz;
    }

    /**
     * Norm a vector.
     * @param pos position in the sky
     * @return the norm of the position.
     */
    public static double normVector(double[] pos) {
        return Math.sqrt(pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]);
    }
}

Related

  1. distanceToPoint(float x1, float y1, float x2, float y2)
  2. distanceToSegment(float ax, float ay, float bx, float by, float px, float py)
  3. distanceToSegmentSquared(float px, float py, float vx, float vy, float wx, float wy)
  4. distanceVincenty(final double lat1, final double lon1, final double lat2, final double lon2)
  5. distancia(int[] c1, int[] c2)
  6. distBetween(float x1, float y1, float x2, float y2)
  7. distBetweenSq(double x1, double y1, double z1, double x2, double y2, double z2)
  8. distence(double lat1, double lon1, double lat2, double lon2, double radius)
  9. distEucl(float[] x1, float[] x2)