Example usage for java.lang Math acos

List of usage examples for java.lang Math acos

Introduction

In this page you can find the example usage for java.lang Math acos.

Prototype

public static double acos(double a) 

Source Link

Document

Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.

Usage

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void acosNegative() {
    try {/*www. jav  a 2 s.c o m*/
        Expression expression = getExpressionWithFunctionContext("acos(-10)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.acos(-10), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void acosNull() {
    try {/*from  ww w .j a va  2 s . c o  m*/
        Expression expression = getExpressionWithFunctionContext("acos(0)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.acos(0), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void acosNinety() {
    try {//from  w w w.ja  v  a 2  s .c  o  m
        Expression expression = getExpressionWithFunctionContext("acos(90)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.acos(90), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void acosPi() {
    try {/*from  w  w  w.  j  a  v  a  2s .c  o m*/
        Expression expression = getExpressionWithFunctionContext("acos(pi)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.acos(Math.PI), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:geogebra.common.kernel.EquationSolver.java

public int solveQuartic(double eqn[], double res[], double eps) {

    if (Math.abs(eqn[4]) < 0)
        return solveCubic(eqn, res, Kernel.STANDARD_PRECISION);

    double a = eqn[3] / eqn[4], b = eqn[2] / eqn[4], c = eqn[1] / eqn[4], d = eqn[0] / eqn[4];

    /*/*from   w  w  w.j  a  va2s  .  co  m*/
     * This code is based on a simplification of the algorithm from
     * zsolve_quartic.c for real roots
     */
    double u[] = new double[3], v[] = new double[3], zarr[] = new double[4];
    double aa, pp, qq, rr, rc, sc, tc, mt;
    double w1r, w1i, w2r, w2i, w3r;
    double v1, v2, arg, theta;
    double disc, h;
    int k1 = 0, k2 = 0;

    int roots = 0;

    /*
     * Deal easily with the cases where the quartic is degenerate. The
     * ordering of solutions is done explicitly.
     */
    if (0 == b && 0 == c) {
        if (0 == d) {
            if (a > 0) {
                res[roots++] = -a;
                res[roots++] = 0.0;
                res[roots++] = 0.0;
                res[roots++] = 0.0;
            } else {
                res[roots++] = 0.0;
                res[roots++] = 0.0;
                res[roots++] = 0.0;
                res[roots++] = -a;
            }
            return 4;
        } else if (0 == a) {
            if (d > 0) {
                return 0;
            }

            res[roots++] = Math.sqrt(Math.sqrt(-d));
            res[roots] = -res[roots - 1];
            roots++;
            return 2;

        }
    }

    if (0.0 == c && 0.0 == d) {
        res[roots++] = 0.0;
        res[roots++] = 0.0;
        double[] res2 = new double[3];
        res2[2] = 1.0;
        res2[1] = a;
        res2[0] = b;
        int n = solveQuadratic(res2, res2, eps);
        res[roots++] = res2[0];
        res[roots++] = res2[1];
        // if (gsl_poly_solve_quadratic(1.0,a,b,x2,x3)==0) {
        if (n == 0) {
            mt = 3;
        } else {
            mt = 1;
        }
    } else {
        /*
         * For non-degenerate solutions, proceed by constructing and solving
         * the resolvent cubic
         */
        aa = a * a;
        pp = b - (3.0 / 8.0) * aa;
        qq = c - (1.0 / 2.0) * a * (b - (1.0 / 4.0) * aa);
        rr = d - (1.0 / 4.0) * (a * c - (1.0 / 4.0) * aa * (b - (3.0 / 16.0) * aa));
        rc = (1.0 / 2.0) * pp;
        sc = (1.0 / 4.0) * ((1.0 / 4.0) * pp * pp - rr);
        tc = -((1.0 / 8.0) * qq * (1.0 / 8.0) * qq);

        /*
         * This code solves the resolvent cubic in a convenient fashion for
         * this implementation of the quartic. If there are three real
         * roots, then they are placed directly into u[]. If two are
         * complex, then the real root is put into u[0] and the real and
         * imaginary part of the complex roots are placed into u[1] and
         * u[2], respectively. Additionally, this calculates the
         * discriminant of the cubic and puts it into the variable disc.
         */
        {
            double qcub = (rc * rc - 3 * sc);
            double rcub = (2 * rc * rc * rc - 9 * rc * sc + 27 * tc);

            double Q = qcub / 9;
            double R = rcub / 54;

            double Q3 = Q * Q * Q;
            double R2 = R * R;

            double CR2 = 729 * rcub * rcub;
            double CQ3 = 2916 * qcub * qcub * qcub;

            disc = (CR2 - CQ3) / 2125764.0;

            if (0 == R && 0 == Q) {
                u[0] = -rc / 3;
                u[1] = -rc / 3;
                u[2] = -rc / 3;
            } else if (CR2 == CQ3) {
                double sqrtQ = Math.sqrt(Q);
                if (R > 0) {
                    u[0] = -2 * sqrtQ - rc / 3;
                    u[1] = sqrtQ - rc / 3;
                    u[2] = sqrtQ - rc / 3;
                } else {
                    u[0] = -sqrtQ - rc / 3;
                    u[1] = -sqrtQ - rc / 3;
                    u[2] = 2 * sqrtQ - rc / 3;
                }
            } else if (CR2 < CQ3) {
                double sqrtQ = Math.sqrt(Q);
                double sqrtQ3 = sqrtQ * sqrtQ * sqrtQ;
                theta = Math.acos(R / sqrtQ3);
                if (R / sqrtQ3 >= 1.0)
                    theta = 0.0;
                {
                    double norm = -2 * sqrtQ;

                    u[0] = norm * Math.cos(theta / 3) - rc / 3;
                    u[1] = norm * Math.cos((theta + 2.0 * Math.PI) / 3) - rc / 3;
                    u[2] = norm * Math.cos((theta - 2.0 * Math.PI) / 3) - rc / 3;
                }
            } else {
                double sgnR = (R >= 0 ? 1 : -1);
                double modR = Math.abs(R);
                double sqrt_disc = Math.sqrt(R2 - Q3);
                double A = -sgnR * Math.pow(modR + sqrt_disc, 1.0 / 3.0);
                double B = Q / A;
                double mod_diffAB = Math.abs(A - B);

                u[0] = A + B - rc / 3;
                u[1] = -0.5 * (A + B) - rc / 3;
                u[2] = -(Math.sqrt(3.0) / 2.0) * mod_diffAB;
            }
        }
        /* End of solution to resolvent cubic */

        /*
         * Combine the square roots of the roots of the cubic resolvent
         * appropriately. Also, calculate 'mt' which designates the nature
         * of the roots: mt=1 : 4 real roots (disc == 0) mt=2 : 0 real roots
         * (disc < 0) mt=3 : 2 real roots (disc > 0)
         */

        if (0.0 == disc)
            u[2] = u[1];

        if (0 >= disc) {
            mt = 2;

            /*
             * One would think that we could return 0 here and exit, since
             * mt=2. However, this assignment is temporary and changes to
             * mt=1 under certain conditions below.
             */

            v[0] = Math.abs(u[0]);
            v[1] = Math.abs(u[1]);
            v[2] = Math.abs(u[2]);

            v1 = Math.max(Math.max(v[0], v[1]), v[2]);
            /* Work out which two roots have the largest moduli */
            k1 = 0;
            k2 = 0;
            if (v1 == v[0]) {
                k1 = 0;
                v2 = Math.max(v[1], v[2]);
            } else if (v1 == v[1]) {
                k1 = 1;
                v2 = Math.max(v[0], v[2]);
            } else {
                k1 = 2;
                v2 = Math.max(v[0], v[1]);
            }

            if (v2 == v[0]) {
                k2 = 0;
            } else if (v2 == v[1]) {
                k2 = 1;
            } else {
                k2 = 2;
            }

            if (0.0 <= u[k1]) {
                w1r = Math.sqrt(u[k1]);
                w1i = 0.0;
            } else {
                w1r = 0.0;
                w1i = Math.sqrt(-u[k1]);
            }
            if (0.0 <= u[k2]) {
                w2r = Math.sqrt(u[k2]);
                w2i = 0.0;
            } else {
                w2r = 0.0;
                w2i = Math.sqrt(-u[k2]);
            }
        } else {
            mt = 3;

            if (0.0 == u[1] && 0.0 == u[2]) {
                arg = 0.0;
            } else {
                arg = Math.sqrt(Math.sqrt(u[1] * u[1] + u[2] * u[2]));
            }
            theta = Math.atan2(u[2], u[1]);

            w1r = arg * Math.cos(theta / 2.0);
            w1i = arg * Math.sin(theta / 2.0);
            w2r = w1r;
            w2i = -w1i;
        }

        /* Solve the quadratic to obtain the roots to the quartic */
        w3r = qq / 8.0 * (w1i * w2i - w1r * w2r) / (w1i * w1i + w1r * w1r) / (w2i * w2i + w2r * w2r);
        h = a / 4.0;

        zarr[0] = w1r + w2r + w3r - h;
        zarr[1] = -w1r - w2r + w3r - h;
        zarr[2] = -w1r + w2r - w3r - h;
        zarr[3] = w1r - w2r - w3r - h;

        /* Arrange the roots into the variables z0, z1, z2, z3 */
        if (2 == mt) {
            if (u[k1] >= 0 && u[k2] >= 0) {
                mt = 1;
                res[roots++] = zarr[0];
                res[roots++] = zarr[1];
                res[roots++] = zarr[2];
                res[roots++] = zarr[3];
            } else {
                return 0;
            }
        } else {
            res[roots++] = zarr[0];
            res[roots++] = zarr[1];
        }
    }

    /* Sort the roots as usual */
    if (1 == mt) {
        /*
         * Roots are all real, sort them by the real part if (*x0 > *x1)
         * SWAPD (*x0, *x1); if (*x0 > *x2) SWAPD (*x0, *x2); if (*x0 > *x3)
         * SWAPD (*x0, *x3);
         * 
         * if (*x1 > *x2) SWAPD (*x1, *x2); if (*x2 > *x3) { SWAPD (*x2,
         * *x3); if (*x1 > *x2) SWAPD (*x1, *x2); }
         */
        return 4;
    }
    /*
     * 2 real roots if (*x0 > *x1) SWAPD (*x0, *x1);
     */

    return 2;
}

From source file:com.rapidminer.tools.expression.internal.function.AntlrParserTrigonometricTest.java

@Test
public void acosPiHalf() {
    try {//w  ww.  j a va  2  s.c  o m
        Expression expression = getExpressionWithFunctionContext("acos(pi/2)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(Math.acos(Math.PI / 2), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:Rotation.java

/** Get the Cardan or Euler angles corresponding to the instance.
        /*from  w  ww.  j  av a  2  s  . c o m*/
 * <p>The equations show that each rotation can be defined by two
 * different values of the Cardan or Euler angles set. For example
 * if Cardan angles are used, the rotation defined by the angles
 * a<sub>1</sub>, a<sub>2</sub> and a<sub>3</sub> is the same as
 * the rotation defined by the angles &pi; + a<sub>1</sub>, &pi;
 * - a<sub>2</sub> and &pi; + a<sub>3</sub>. This method implements
 * the following arbitrary choices:</p>
 * <ul>
 *   <li>for Cardan angles, the chosen set is the one for which the
 *   second angle is between -&pi;/2 and &pi;/2 (i.e its cosine is
 *   positive),</li>
 *   <li>for Euler angles, the chosen set is the one for which the
 *   second angle is between 0 and &pi; (i.e its sine is positive).</li>
 * </ul>
        
 * <p>Cardan and Euler angle have a very disappointing drawback: all
 * of them have singularities. This means that if the instance is
 * too close to the singularities corresponding to the given
 * rotation order, it will be impossible to retrieve the angles. For
 * Cardan angles, this is often called gimbal lock. There is
 * <em>nothing</em> to do to prevent this, it is an intrinsic problem
 * with Cardan and Euler representation (but not a problem with the
 * rotation itself, which is perfectly well defined). For Cardan
 * angles, singularities occur when the second angle is close to
 * -&pi;/2 or +&pi;/2, for Euler angle singularities occur when the
 * second angle is close to 0 or &pi;, this implies that the identity
 * rotation is always singular for Euler angles!</p>
        
 * @param order rotation order to use
 * @return an array of three angles, in the order specified by the set
 * @exception CardanEulerSingularityException if the rotation is
 * singular with respect to the angles set specified
 */
public double[] getAngles(RotationOrder order) {

    if (order == RotationOrder.XYZ) {

        // r (Vector3D.plusK) coordinates are :
        //  sin (theta), -cos (theta) sin (phi), cos (theta) cos (phi)
        // (-r) (Vector3D.plusI) coordinates are :
        // cos (psi) cos (theta), -sin (psi) cos (theta), sin (theta)
        // and we can choose to have theta in the interval [-PI/2 ; +PI/2]
        Vector3D v1 = applyTo(Vector3D.plusK);
        Vector3D v2 = applyInverseTo(Vector3D.plusI);
        if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(-(v1.getY()), v1.getZ()), Math.asin(v2.getZ()),
                Math.atan2(-(v2.getY()), v2.getX()) };

    } else if (order == RotationOrder.XZY) {

        // r (Vector3D.plusJ) coordinates are :
        // -sin (psi), cos (psi) cos (phi), cos (psi) sin (phi)
        // (-r) (Vector3D.plusI) coordinates are :
        // cos (theta) cos (psi), -sin (psi), sin (theta) cos (psi)
        // and we can choose to have psi in the interval [-PI/2 ; +PI/2]
        Vector3D v1 = applyTo(Vector3D.plusJ);
        Vector3D v2 = applyInverseTo(Vector3D.plusI);
        if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getZ(), v1.getY()), -Math.asin(v2.getY()),
                Math.atan2(v2.getZ(), v2.getX()) };

    } else if (order == RotationOrder.YXZ) {

        // r (Vector3D.plusK) coordinates are :
        //  cos (phi) sin (theta), -sin (phi), cos (phi) cos (theta)
        // (-r) (Vector3D.plusJ) coordinates are :
        // sin (psi) cos (phi), cos (psi) cos (phi), -sin (phi)
        // and we can choose to have phi in the interval [-PI/2 ; +PI/2]
        Vector3D v1 = applyTo(Vector3D.plusK);
        Vector3D v2 = applyInverseTo(Vector3D.plusJ);
        if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getX(), v1.getZ()), -Math.asin(v2.getZ()),
                Math.atan2(v2.getX(), v2.getY()) };

    } else if (order == RotationOrder.YZX) {

        // r (Vector3D.plusI) coordinates are :
        // cos (psi) cos (theta), sin (psi), -cos (psi) sin (theta)
        // (-r) (Vector3D.plusJ) coordinates are :
        // sin (psi), cos (phi) cos (psi), -sin (phi) cos (psi)
        // and we can choose to have psi in the interval [-PI/2 ; +PI/2]
        Vector3D v1 = applyTo(Vector3D.plusI);
        Vector3D v2 = applyInverseTo(Vector3D.plusJ);
        if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(-(v1.getZ()), v1.getX()), Math.asin(v2.getX()),
                Math.atan2(-(v2.getZ()), v2.getY()) };

    } else if (order == RotationOrder.ZXY) {

        // r (Vector3D.plusJ) coordinates are :
        // -cos (phi) sin (psi), cos (phi) cos (psi), sin (phi)
        // (-r) (Vector3D.plusK) coordinates are :
        // -sin (theta) cos (phi), sin (phi), cos (theta) cos (phi)
        // and we can choose to have phi in the interval [-PI/2 ; +PI/2]
        Vector3D v1 = applyTo(Vector3D.plusJ);
        Vector3D v2 = applyInverseTo(Vector3D.plusK);
        if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(-(v1.getX()), v1.getY()), Math.asin(v2.getY()),
                Math.atan2(-(v2.getX()), v2.getZ()) };

    } else if (order == RotationOrder.ZYX) {

        // r (Vector3D.plusI) coordinates are :
        //  cos (theta) cos (psi), cos (theta) sin (psi), -sin (theta)
        // (-r) (Vector3D.plusK) coordinates are :
        // -sin (theta), sin (phi) cos (theta), cos (phi) cos (theta)
        // and we can choose to have theta in the interval [-PI/2 ; +PI/2]
        Vector3D v1 = applyTo(Vector3D.plusI);
        Vector3D v2 = applyInverseTo(Vector3D.plusK);
        if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getY(), v1.getX()), -Math.asin(v2.getX()),
                Math.atan2(v2.getY(), v2.getZ()) };

    } else if (order == RotationOrder.XYX) {

        // r (Vector3D.plusI) coordinates are :
        //  cos (theta), sin (phi1) sin (theta), -cos (phi1) sin (theta)
        // (-r) (Vector3D.plusI) coordinates are :
        // cos (theta), sin (theta) sin (phi2), sin (theta) cos (phi2)
        // and we can choose to have theta in the interval [0 ; PI]
        Vector3D v1 = applyTo(Vector3D.plusI);
        Vector3D v2 = applyInverseTo(Vector3D.plusI);
        if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getY(), -v1.getZ()), Math.acos(v2.getX()),
                Math.atan2(v2.getY(), v2.getZ()) };

    } else if (order == RotationOrder.XZX) {

        // r (Vector3D.plusI) coordinates are :
        //  cos (psi), cos (phi1) sin (psi), sin (phi1) sin (psi)
        // (-r) (Vector3D.plusI) coordinates are :
        // cos (psi), -sin (psi) cos (phi2), sin (psi) sin (phi2)
        // and we can choose to have psi in the interval [0 ; PI]
        Vector3D v1 = applyTo(Vector3D.plusI);
        Vector3D v2 = applyInverseTo(Vector3D.plusI);
        if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getZ(), v1.getY()), Math.acos(v2.getX()),
                Math.atan2(v2.getZ(), -v2.getY()) };

    } else if (order == RotationOrder.YXY) {

        // r (Vector3D.plusJ) coordinates are :
        //  sin (theta1) sin (phi), cos (phi), cos (theta1) sin (phi)
        // (-r) (Vector3D.plusJ) coordinates are :
        // sin (phi) sin (theta2), cos (phi), -sin (phi) cos (theta2)
        // and we can choose to have phi in the interval [0 ; PI]
        Vector3D v1 = applyTo(Vector3D.plusJ);
        Vector3D v2 = applyInverseTo(Vector3D.plusJ);
        if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getX(), v1.getZ()), Math.acos(v2.getY()),
                Math.atan2(v2.getX(), -v2.getZ()) };

    } else if (order == RotationOrder.YZY) {

        // r (Vector3D.plusJ) coordinates are :
        //  -cos (theta1) sin (psi), cos (psi), sin (theta1) sin (psi)
        // (-r) (Vector3D.plusJ) coordinates are :
        // sin (psi) cos (theta2), cos (psi), sin (psi) sin (theta2)
        // and we can choose to have psi in the interval [0 ; PI]
        Vector3D v1 = applyTo(Vector3D.plusJ);
        Vector3D v2 = applyInverseTo(Vector3D.plusJ);
        if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getZ(), -v1.getX()), Math.acos(v2.getY()),
                Math.atan2(v2.getZ(), v2.getX()) };

    } else if (order == RotationOrder.ZXZ) {

        // r (Vector3D.plusK) coordinates are :
        //  sin (psi1) sin (phi), -cos (psi1) sin (phi), cos (phi)
        // (-r) (Vector3D.plusK) coordinates are :
        // sin (phi) sin (psi2), sin (phi) cos (psi2), cos (phi)
        // and we can choose to have phi in the interval [0 ; PI]
        Vector3D v1 = applyTo(Vector3D.plusK);
        Vector3D v2 = applyInverseTo(Vector3D.plusK);
        if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
            System.out.println("CardanEulerSingularityException");
        }
        return new double[] { Math.atan2(v1.getX(), -v1.getY()), Math.acos(v2.getZ()),
                Math.atan2(v2.getX(), v2.getY()) };

    } else { // last possibility is ZYZ

        // r (Vector3D.plusK) coordinates are :
        //  cos (psi1) sin (theta), sin (psi1) sin (theta), cos (theta)
        // (-r) (Vector3D.plusK) coordinates are :
        // -sin (theta) cos (psi2), sin (theta) sin (psi2), cos (theta)
        // and we can choose to have theta in the interval [0 ; PI]
        Vector3D v1 = applyTo(Vector3D.plusK);
        Vector3D v2 = applyInverseTo(Vector3D.plusK);
        if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
            throw new RuntimeException("false");
        }
        return new double[] { Math.atan2(v1.getY(), v1.getX()), Math.acos(v2.getZ()),
                Math.atan2(v2.getY(), -v2.getX()) };

    }

}

From source file:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

/**
* Compute offset following path on circle
*
* @param childCenter//from  w  ww.jav  a 2 s.  c  o  m
* @return offset from position on unitary circle
*/
private float getOffsetOnCircle(int childCenter) {
    float x = getRelativePosition(childCenter) / mRadius;
    if (x < -1.0f)
        x = -1.0f;
    if (x > 1.0f)
        x = 1.0f;

    return (float) (1 - Math.sin(Math.acos(x)));
}

From source file:org.kalypsodeegree_impl.tools.GeometryUtilities.java

/**
 * Calculates the 'direction' of a vector in degrees. The degree value represents the angle between the vector and the
 * x-Axis in coordinate space.//  www  . j a  v a  2s  .  c  o  m
 * <p>
 * Orientation is anti.clockwise (i.e. positive).
 * </p>
 * 
 * @return The angle in degree or {@link Double#NaN} if the given vector has length 0.
 */
public static double directionFromVector(final double vx, final double vy) {
    final double length = Math.sqrt(vx * vx + vy * vy);
    if (length == 0.0) // double comparison problems?
        return Double.NaN;

    final double alpha = Math.acos(vx / length);

    if (vy < 0)
        return Math.toDegrees(2 * Math.PI - alpha);

    return Math.toDegrees(alpha);
}

From source file:com.mainpanel.LifeApp_Map.java

public double cal_distance(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
            + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);/*from w w  w  .  j  a  v  a 2  s. c  om*/
    dist = dist * 60 * 1.1515;

    return (dist);
}