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:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

private float getSpacingMultiplierOnCirlce(int childCenter) {
    float x = getRelativePosition(childCenter) / mRadius;
    return (float) Math.sin(Math.acos(x));
}

From source file:geogebra.kernel.EquationSolver.java

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

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

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

    /* /*  w w w. j a  v  a 2 s .  c o  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;
            } else {
                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);
        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;
    } else {
        /* 2 real roots 
        if (*x0 > *x1)
          SWAPD (*x0, *x1);*/
    }

    return 2;
}

From source file:ffx.autoparm.Potential2.java

/**
 * <p>/*from   w  ww . ja  v a 2s  .c o m*/
 * sphere</p>
 *
 * @param ndot a int.
 * @return an array of double.
 */
public double[][] sphere(int ndot) {
    double dot[][] = new double[ndot][3];
    double tot = (double) ndot;
    double tot1 = (double) (ndot - 1);
    double h;
    double phi;
    double phiold = 0;
    double theta;

    for (int i = 0; i < ndot; i++) {
        h = -1 + 2 * i / tot1;
        theta = Math.acos(h);
        if (i == 0 || i == ndot - 1) {
            phi = 0;
        } else {
            phi = (phiold + 3.6 / Math.sqrt(tot * (1 - h * h))) % (2 * Math.PI);
        }
        dot[i][0] = sin(theta) * cos(phi);
        dot[i][1] = sin(theta) * sin(phi);
        dot[i][2] = cos(theta);
        phiold = phi;
    }

    return dot;
}

From source file:org.apache.calcite.runtime.SqlFunctions.java

/** SQL <code>ACOS</code> operator applied to long values. */
public static double acos(long b0) {
    return Math.acos(b0);
}

From source file:org.apache.calcite.runtime.SqlFunctions.java

/** SQL <code>ACOS</code> operator applied to BigDecimal values. */
public static double acos(BigDecimal b0) {
    return Math.acos(b0.doubleValue());
}

From source file:org.apache.calcite.runtime.SqlFunctions.java

/** SQL <code>ACOS</code> operator applied to double values. */
public static double acos(double b0) {
    return Math.acos(b0);
}

From source file:SIFT_Volume_Stitching.java

/**
  Compute the best 2D model between two substacks
  @param front substack//from  w w  w. jav  a 2s. c om
  @param back substack
  @param sift object with parameters set as detailled in run method
  @param MIP size
  @return computed model
 */
public AbstractAffineModel2D<?> CompareCrossSection(ImageStack subStack1, ImageStack subStack2, SIFT ijSIFT,
        int MIP) {
    /* Sauvegarde des Substacks */
    ImageStack frontCOPY = subStack1.duplicate();
    ImageStack backCOPY = subStack2.duplicate();

    /* Construction des MIP */
    if (MIP > 1) {
        int step = MIP;

        if (MIP > subStack1.getSize()) {
            step = subStack1.getSize();
        }
        subStack1 = createMIP(subStack1, step);
        subStack2 = createMIP(subStack2, step);
    }

    /* Parameters Initialisation */
    ImageProcessor ip2;
    ImageProcessor ip1;

    fsf.clear();
    fsb.clear();

    int taille = subStack2.getSize();
    IJ.log("taille " + taille + " and size of stacks, 1= " + subStack1.getSize() + " 2= "
            + subStack2.getSize());
    /* Comparisons of cross-section simultaneously */
    for (int i = 1; i <= taille; ++i) {
        ip1 = subStack1.getProcessor(i);
        ijSIFT.extractFeatures(ip1, fsf);

        ip2 = subStack2.getProcessor(i);
        ijSIFT.extractFeatures(ip2, fsb);
    }

    /* Candidate computation */
    System.out.print("identifying correspondences using brute force ...");
    Vector<PointMatch> candidates = FloatArray2DSIFT.createMatches(fsb, fsf, 1.5f, null, Float.MAX_VALUE,
            p.rod);

    Vector<PointMatch> inliers = new Vector<PointMatch>();

    /* Model Computation */
    AbstractAffineModel2D<?> BestModel;
    switch (p.modelIndex) {
    case 0:
        BestModel = new TranslationModel2D();
        break;
    case 1:
        BestModel = new RigidModel2D();
        break;
    case 2:
        BestModel = new SimilarityModel2D();
        break;
    case 3:
        BestModel = new AffineModel2D();
        break;
    default:
        BestModel = new RigidModel2D();
    }

    try {
        modelFound = BestModel.filterRansac(candidates, inliers, 1000, p.maxEpsilon, p.minInlierRatio);
    } catch (Exception e) {
        modelFound = false;
        System.err.println(e.getMessage());
    }

    if (modelFound) {
        double[][] data = new double[2][3];
        BestModel.toMatrix(data);
        IJ.log("Rotation : " + Math.acos(data[0][0]) * (180 / Math.PI) + "");
        IJ.log("Horizontal Translation : " + data[0][2] + "pixels");
        IJ.log("Vertical Translation : " + data[1][1] + "pixels");

        ip1 = createMIP(frontCOPY, frontCOPY.getSize()).getProcessor(1);
        ip2 = createMIP(backCOPY, backCOPY.getSize()).getProcessor(1);
        displayFeatures(ip1, ip2, candidates, inliers, modelFound);

        IJ.log("(Info) Number of Matching Features : " + inliers.size());
    }

    return BestModel;
}

From source file:matteroverdrive.util.RenderUtils.java

public static void rotateTowards(Vec3 from, Vec3 to, Vec3 up) {
    double dot = from.dotProduct(to);
    if (Math.abs(dot - (-1.0)) < Double.MIN_VALUE) {
        glRotated(180, up.xCoord, up.yCoord, up.zCoord);
    }/*from w w  w. ja  v  a  2  s .  c om*/
    if (Math.abs(dot - (1.0)) < Double.MIN_VALUE) {
        return;
    }

    double rotAngle = Math.acos(dot);
    Vec3 rotAxis = from.crossProduct(to).normalize();
    glRotated(rotAngle * (180d / Math.PI), rotAxis.xCoord, rotAxis.yCoord, rotAxis.zCoord);
}

From source file:curveavg.MedialAxisTransform.java

/**
 * Solves a cubic equation using a closed-form approach.
 * http://stackoverflow.com/questions/13328676/c-solving-cubic-equations
 *///  w  w w . jav a  2  s. c  om
public static SolverResult solveCubic(float a, float b, float c, float d) {

    SolverResult res = new SolverResult();
    res.re = new float[3];
    res.im = new float[3];

    // The problem is in the form of ax^3 + bx^2 + cx = 0
    // Solve quadratic
    if (Math.abs(d) < 1e-3) {

        // One of the roots is 0.
        res.re[0] = 0.0f;
        res.im[0] = 0.0f;

        // Solve the quadratic equation
        res.re[1] = res.re[2] = (-b / (2.0f * a));
        float discr = b * b - 4.0f * a * c;
        if (discr < 0) {
            float temp = (float) (Math.sqrt(-discr) / (2.0f * a));
            res.im[1] = temp;
            res.im[2] = -temp;
        } else {
            float temp = (float) (Math.sqrt(discr) / (2.0f * a));
            res.im[1] = res.im[2] = 0.0f;
            res.re[1] += temp;
            res.re[2] -= temp;
        }
        System.out.println("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d);
        System.out.println("results: (" + res.re[0] + ", " + res.im[0] + "i), (" + res.re[1] + ", " + res.im[1]
                + "i), (" + res.re[2] + ", " + res.im[2] + "i)");
        LOG.severe("Given system is not a general cubic - may cause problems");
        return res;
        // assert (false);
    }

    b /= a;
    c /= a;
    d /= a;
    float disc, q, r, dum1, s, t, term1, r13;
    q = (3.0f * c - (b * b)) / 9.0f;
    r = -(27.0f * d) + b * (9.0f * c - 2.0f * (b * b));
    r /= 54.0f;
    disc = q * q * q + r * r;
    term1 = (b / 3.0f);
    res.im[0] = 0.0f;
    if (disc > 0) { // one root real, two are complex
        s = r + (float) Math.sqrt(disc);
        s = (float) ((s < 0) ? -Math.pow(-s, (1.0 / 3.0)) : Math.pow(s, (1.0 / 3.0)));
        t = r - (float) Math.sqrt(disc);
        t = (float) ((t < 0) ? -Math.pow(-t, (1.0 / 3.0)) : Math.pow(t, (1.0 / 3.0)));
        res.re[0] = -term1 + s + t;
        term1 += (s + t) / 2.0;
        res.re[2] = res.re[1] = -term1;
        term1 = (float) Math.sqrt(3.0) * (-t + s) / 2;
        res.im[1] = term1;
        res.im[2] = -term1;
        return res;
    }

    // The remaining options are all real
    res.im[1] = res.im[2] = 0;
    if (disc == 0) { // All roots real, at least two are equal.
        r13 = (float) ((r < 0) ? -Math.pow(-r, (1.0 / 3.0)) : Math.pow(r, (1.0 / 3.0)));
        res.re[0] = -term1 + 2.0f * r13;
        res.re[0] = res.re[1] = -(r13 + term1);
        return res;
    }

    // Only option left is that all roots are real and unequal (to get here, q < 0)
    q = -q;
    dum1 = q * q * q;
    dum1 = (float) Math.acos(r / Math.sqrt(dum1));
    r13 = (float) (2.0f * Math.sqrt(q));
    res.re[0] = -term1 + (float) (r13 * Math.cos(dum1 / 3.0));
    res.re[1] = -term1 + (float) (r13 * Math.cos((dum1 + 2.0 * Math.PI) / 3.0));
    res.re[2] = -term1 + (float) (r13 * Math.cos((dum1 + 4.0 * Math.PI) / 3.0));
    return res;
}

From source file:org.bimserver.GeometryGenerator.java

/**
 * Get the angle in radians between two planes
 * /* ww  w .j ava2 s .c o m*/
 * @param v1
 * @param v2
 * @param v3
 * @param u1
 * @param u2
 * @param u3
 * @return
 */
private static double getPlaneAngle(float[] v1, float[] v2, float[] v3, float[] u1, float[] u2, float[] u3) {
    float[] cross1 = Vector.crossProduct(new float[] { v2[0] - v1[0], v2[1] - v1[1], v2[2] - v1[2] },
            new float[] { v3[0] - v1[0], v3[1] - v1[1], v3[2] - v1[2] });
    float[] cross2 = Vector.crossProduct(new float[] { u2[0] - u1[0], u2[1] - u1[1], u2[2] - u1[2] },
            new float[] { u3[0] - u1[0], u3[1] - u1[1], u3[2] - u1[2] });

    float num = Vector.dot(cross1, cross2);
    float den = Vector.length(cross1) * Vector.length(cross2);

    float a = num / den;
    if (a > 1) {
        a = 1;
    }
    if (a < -1) {
        a = -1;
    }
    double result = Math.acos(a);

    if (Double.isNaN(result)) {
        System.out.println();
    }
    return result;
}