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:InertiaMatrix.java

/**
 * Return an interpolated quaternion, based on this quaternion, the given
 * quaternion q2, and the parameter t.//  w  w w.java  2 s. c om
 * 
 * @param q2
 *            quaternion to interpolate against
 * @param t
 *            interpolation parameter in [0,1]
 * @return
 */
public final Quaternion interpolate(Quaternion q2, double t) {
    // seems to be slerp interpolation of quaterions [02 03 2008]
    Quaternion qa = this;
    Quaternion qb = q2;
    // qa sin((1-t) theta) + qb sin( t theta )
    // qm = --------------------------------------- 0<t<1
    // sin theta
    //
    // theta = arccos( qa dot qb )
    double theta = Math.acos(qa.dot(qb));

    if (Math.abs(theta) < 1e-7) {
        return this;
    }

    return qa.multiply(Math.sin((1 - t) * theta)).add(qb.multiply(Math.sin(t * theta)))
            .multiply(1 / Math.sin(theta));
}

From source file:services.SimulationService.java

public byte getSpecialDirection(Quaternion orientation) {
    byte movementAngle = (byte) 0.0f;
    float wOrient = orientation.w;
    float yOrient = orientation.y;
    float sq = (float) Math.sqrt(1 - (orientation.w * orientation.w));

    if (sq != 0) {
        if (orientation.w > 0 && orientation.y < 0) {
            wOrient *= -1;//from www .  java2 s.c o m
            yOrient *= -1;
        }
        movementAngle = (byte) ((yOrient / sq) * (2 * Math.acos(wOrient) / 0.06283f));
    }

    return movementAngle;
}

From source file:Rotation.java

/** Compute the angular separation between two vectors.
 * <p>This method computes the angular separation between two
 * vectors using the dot product for well separated vectors and the
 * cross product for almost aligned vectors. This allow to have a
 * good accuracy in all cases, even for vectors very close to each
 * other.</p>/*from w w w.  j  a  v  a2s. com*/
 * @param v1 first vector
 * @param v2 second vector
 * @return angular separation between v1 and v2
 * @exception ArithmeticException if either vector has a null norm
 */
public static double angle(Vector3D v1, Vector3D v2) {

    double normProduct = v1.getNorm() * v2.getNorm();
    if (normProduct == 0) {
        throw new ArithmeticException("null norm");
    }

    double dot = dotProduct(v1, v2);
    double threshold = normProduct * 0.9999;
    if ((dot < -threshold) || (dot > threshold)) {
        // the vectors are almost aligned, compute using the sine
        Vector3D v3 = crossProduct(v1, v2);
        if (dot >= 0) {
            return Math.asin(v3.getNorm() / normProduct);
        }
        return Math.PI - Math.asin(v3.getNorm() / normProduct);
    }

    // the vectors are sufficiently separated to use the cosine
    return Math.acos(dot / normProduct);

}

From source file:com.mitre.holdshort.MainActivity.java

public double distanceBetweenTwoPoints(Point pointA, Point pointA2) {

    double lat1 = pointA.getLatitude() * Math.PI / 180;
    double lon1 = pointA.getLongitude() * Math.PI / 180;
    double lat2 = pointA2.getLatitude() * Math.PI / 180;
    double lon2 = pointA2.getLongitude() * Math.PI / 180;

    return (Math
            .acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)));
}

From source file:blusunrize.immersiveengineering.client.ClientEventHandler.java

public static void drawBlockOverlayArrow(Tessellator tessellator, BufferBuilder BufferBuilder,
        Vec3d directionVec, EnumFacing side, AxisAlignedBB targetedBB) {
    Vec3d[] translatedPositions = new Vec3d[arrowCoords.length];
    Matrix4 mat = new Matrix4();
    Vec3d defaultDir = side.getAxis() == Axis.Y ? new Vec3d(0, 0, 1) : new Vec3d(0, 1, 0);
    directionVec = directionVec.normalize();
    double angle = Math.acos(defaultDir.dotProduct(directionVec));
    Vec3d axis = defaultDir.crossProduct(directionVec);
    mat.rotate(angle, axis.x, axis.y, axis.z);
    if (side != null) {
        if (side.getAxis() == Axis.Z)
            mat.rotate(Math.PI / 2, 1, 0, 0).rotate(Math.PI, 0, 1, 0);
        else if (side.getAxis() == Axis.X)
            mat.rotate(Math.PI / 2, 0, 0, 1).rotate(Math.PI / 2, 0, 1, 0);
    }//from w ww  .ja v a2 s. com
    for (int i = 0; i < translatedPositions.length; i++) {
        Vec3d vec = mat.apply(new Vec3d(arrowCoords[i][0], 0, arrowCoords[i][1])).add(.5, .5, .5);
        if (side != null && targetedBB != null)
            vec = new Vec3d(
                    side == EnumFacing.WEST ? targetedBB.minX - .002
                            : side == EnumFacing.EAST ? targetedBB.maxX + .002 : vec.x,
                    side == EnumFacing.DOWN ? targetedBB.minY - .002
                            : side == EnumFacing.UP ? targetedBB.maxY + .002 : vec.y,
                    side == EnumFacing.NORTH ? targetedBB.minZ - .002
                            : side == EnumFacing.SOUTH ? targetedBB.maxZ + .002 : vec.z);
        translatedPositions[i] = vec;
    }

    BufferBuilder.begin(6, DefaultVertexFormats.POSITION_COLOR);
    for (Vec3d point : translatedPositions)
        BufferBuilder.pos(point.x, point.y, point.z).color(Lib.COLOUR_F_ImmersiveOrange[0],
                Lib.COLOUR_F_ImmersiveOrange[1], Lib.COLOUR_F_ImmersiveOrange[2], 0.4F).endVertex();
    tessellator.draw();
    BufferBuilder.begin(2, DefaultVertexFormats.POSITION_COLOR);
    for (Vec3d point : translatedPositions)
        BufferBuilder.pos(point.x, point.y, point.z).color(0, 0, 0, 0.4F).endVertex();
    tessellator.draw();
}

From source file:cc.flydev.launcher.Page.java

private PointF isFlingingToDelete() {
    ViewConfiguration config = ViewConfiguration.get(getContext());
    mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());

    if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
        // Do a quick dot product test to ensure that we are flinging upwards
        PointF vel = new PointF(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
        PointF upVec = new PointF(0f, -1f);
        float theta = (float) Math
                .acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / (vel.length() * upVec.length()));
        if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
            return vel;
        }/*from  www.  j av  a2s  . c  om*/
    }
    return null;
}

From source file:com.n2hsu.launcher.Page.java

private PointF isFlingingToDelete() {
    ViewConfiguration config = ViewConfiguration.get(getContext());
    mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity());

    if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) {
        // Do a quick dot product test to ensure that we are flinging
        // upwards
        PointF vel = new PointF(mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity());
        PointF upVec = new PointF(0f, -1f);
        float theta = (float) Math
                .acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / (vel.length() * upVec.length()));
        if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) {
            return vel;
        }// w w  w .  j a v  a 2  s  . co m
    }
    return null;
}

From source file:com.github.jonmarsh.waveform_processing_for_imagej.WaveformUtils.java

/**
 * Computes real roots to the cubic equation
 * {@code x^3 + a*x^2 + b*x + c = 0}, given real coefficients {@code a},
 * {@code b}, and {@code c}. If there are three distinct roots, they are
 * returned in a three-element array. If there is a double root and a single
 * root, the results are returned in a two-element array. If there is a
 * single real root, the result is returned in a single-element array.
 * <p>/*from   www. j  a v  a  2s  .c  o  m*/
 * Code adapted from GSL poly/solve_cubic.c
 * (<a href="http://www.gnu.org/software/gsl/">www.gnu.org/software/gsl/</a>)
 * </p>
 *
 * @param a quadratic coefficient
 * @param b linear coefficient
 * @param c constant term
 * @return array of distinct roots in order from least to greatest
 */
public static final double[] cubicRoots(double a, double b, double c) {
    if (c == 0.0) {
        double[] qr = quadraticRoots(1.0, a, b);
        if (qr.length == 2) {
            if (qr[0] != 0.0 && qr[1] != 0.0) {
                double[] output = new double[] { 0.0, qr[0], qr[1] };
                Arrays.sort(output);
                return output;
            } else if (qr[0] * qr[1] == 0.0) { // the zero root is already present in qr
                return qr;
            }
        } else if (qr.length == 1) {
            if (qr[0] > 0.0) {
                return new double[] { 0.0, qr[0] };
            } else if (qr[0] < 0.0) {
                return new double[] { qr[0], 0.0 };
            } else {
                return qr;
            }
        } else {
            return new double[] { 0.0 };
        }
    }

    double q = (a * a - 3 * b);
    double r = (2 * a * a * a - 9 * a * b + 27 * c);

    double Q = q / 9;
    double R = r / 54;

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

    double CR2 = 729 * r * r;
    double CQ3 = 2916 * q * q * q;

    if (R == 0 && Q == 0) {
        return new double[] { -a / 3.0 };
    } else if (CR2 == CQ3) {
        /* this test is actually R2 == Q3, written in a form suitable
         for exact computation with integers */

        /* Due to finite precision some double roots may be missed, and
         considered to be a pair of complex roots z = x +/- epsilon i
         close to the real axis. */
        double sqrtQ = Math.sqrt(Q);

        if (R > 0) {
            return new double[] { -2.0 * sqrtQ - a / 3.0, sqrtQ - a / 3.0 };
        } else {
            return new double[] { -sqrtQ - a / 3.0, 2.0 * sqrtQ - a / 3.0 };
        }
    } else if (R2 < Q3) {
        double ratio = Math.signum(R) * Math.sqrt(R2 / Q3);
        double theta = Math.acos(ratio);
        double norm = -2 * Math.sqrt(Q);
        double x0 = norm * Math.cos(theta / 3) - a / 3;
        double x1 = norm * Math.cos((theta + 2.0 * Math.PI) / 3) - a / 3;
        double x2 = norm * Math.cos((theta - 2.0 * Math.PI) / 3) - a / 3;

        /* Sort x0, x1, x2 into increasing order */
        if (x0 > x1) {
            double temp = x1;
            x1 = x0;
            x0 = temp;
        }

        if (x1 > x2) {
            double temp = x2;
            x2 = x1;
            x1 = temp;

            if (x0 > x1) {
                temp = x1;
                x1 = x0;
                x0 = temp;
            }
        }
        return new double[] { x0, x1, x2 };
    } else {
        double sgnR = (R >= 0 ? 1 : -1);
        double A = -sgnR * Math.pow(Math.abs(R) + Math.sqrt(R2 - Q3), 1.0 / 3.0);
        double B = Q / A;
        return new double[] { A + B - a / 3 };
    }
}

From source file:carnero.cgeo.cgBase.java

public static double getDistance(Double lat1, Double lon1, Double lat2, Double lon2) {
    if (lat1 == null || lon1 == null || lat2 == null || lon2 == null) {
        return 0d;
    }/* w w  w. ja v a2  s .  c o  m*/

    lat1 *= deg2rad;
    lon1 *= deg2rad;
    lat2 *= deg2rad;
    lon2 *= deg2rad;

    final double d = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2);
    final double distance = erad * Math.acos(d); // distance in km

    if (Double.isNaN(distance) == false && distance > 0) {
        return distance;
    } else {
        return 0d;
    }
}

From source file:carnero.cgeo.cgBase.java

public static Double getHeading(Double lat1, Double lon1, Double lat2, Double lon2) {
    Double result = new Double(0);

    int ilat1 = (int) Math.round(0.5 + lat1 * 360000);
    int ilon1 = (int) Math.round(0.5 + lon1 * 360000);
    int ilat2 = (int) Math.round(0.5 + lat2 * 360000);
    int ilon2 = (int) Math.round(0.5 + lon2 * 360000);

    lat1 *= deg2rad;/*from   w w  w. j a v  a 2 s.c  o m*/
    lon1 *= deg2rad;
    lat2 *= deg2rad;
    lon2 *= deg2rad;

    if (ilat1 == ilat2 && ilon1 == ilon2) {
        return new Double(result);
    } else if (ilat1 == ilat2) {
        if (ilon1 > ilon2) {
            result = new Double(270);
        } else {
            result = new Double(90);
        }
    } else if (ilon1 == ilon2) {
        if (ilat1 > ilat2) {
            result = new Double(180);
        }
    } else {
        Double c = Math.acos(
                Math.sin(lat2) * Math.sin(lat1) + Math.cos(lat2) * Math.cos(lat1) * Math.cos(lon2 - lon1));
        Double A = Math.asin(Math.cos(lat2) * Math.sin(lon2 - lon1) / Math.sin(c));
        result = new Double(A * rad2deg);
        if (ilat2 > ilat1 && ilon2 > ilon1) {
            // result don't need change
        } else if (ilat2 < ilat1 && ilon2 < ilon1) {
            result = 180f - result;
        } else if (ilat2 < ilat1 && ilon2 > ilon1) {
            result = 180f - result;
        } else if (ilat2 > ilat1 && ilon2 < ilon1) {
            result += 360f;
        }
    }

    return result;
}