Example usage for java.lang Math cos

List of usage examples for java.lang Math cos

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double cos(double a) 

Source Link

Document

Returns the trigonometric cosine of an angle.

Usage

From source file:msi.gaml.operators.Maths.java

@operator(value = "cos_rad", can_be_const = true, category = { IOperatorCategory.ARITHMETIC })
@doc(value = "Returns the value (in [-1,1]) of the cosinus of the operand (in decimal degrees).  The argument is casted to an int before being evaluated.", masterDoc = true, special_cases = "Operand values out of the range [0-359] are normalized.", see = {
        "sin", "tan" })
public static Double cos_rad(final Double rv) {
    return Math.cos(rv);
}

From source file:itdelatrisu.opsu.Utils.java

public static float[] mirrorPoint(float x, float y) {
    double dx = x - displayContainer.width / 2d;
    double dy = y - displayContainer.height / 2d;
    double ang = Math.atan2(dy, dx);
    double d = -Math.sqrt(dx * dx + dy * dy);
    return new float[] { (float) (displayContainer.width / 2d + Math.cos(ang) * d),
            (float) (displayContainer.height / 2d + Math.sin(ang) * d) };
}

From source file:Matrix.java

/**
 *  Rotation  about an arbitrary Axis//from   www. ja  v  a 2 s  .c o  m
 *  @param alpha the angle of the rotation
 *  @param p1 first axis point
 *  @param p2 second axis point
 *  @return the rotation matrix
 */
public static float[] matrixRotate(float alpha, float[] p1, float[] p2) {
    alpha = alpha * PI / 180f;

    float a1 = p1[0];
    float a2 = p1[1];
    float a3 = p1[2];

    //Compute the vector defines by point p1 and p2
    float v1 = p2[0] - a1;
    float v2 = p2[1] - a2;
    float v3 = p2[2] - a3;

    double theta = Math.atan2(v2, v1);
    double phi = Math.atan2(Math.sqrt(v1 * v1 + v2 * v2), v3);

    float cosAlpha, sinAlpha, sinPhi2;
    float cosTheta, sinTheta, cosPhi2;
    float cosPhi, sinPhi, cosTheta2, sinTheta2;

    cosPhi = (float) Math.cos(phi);
    cosTheta = (float) Math.cos(theta);
    cosTheta2 = (float) cosTheta * cosTheta;
    sinPhi = (float) Math.sin(phi);
    sinTheta = (float) Math.sin(theta);
    sinTheta2 = (float) sinTheta * sinTheta;

    sinPhi2 = (float) sinPhi * sinPhi;
    cosPhi2 = (float) cosPhi * cosPhi;

    cosAlpha = (float) Math.cos(alpha);
    sinAlpha = (float) Math.sin(alpha);

    float c = (float) 1.0 - cosAlpha;

    float r11, r12, r13, r14, r21, r22, r23, r24, r31, r32, r33, r34;
    r11 = cosTheta2 * (cosAlpha * cosPhi2 + sinPhi2) + cosAlpha * sinTheta2;
    r12 = sinAlpha * cosPhi + c * sinPhi2 * cosTheta * sinTheta;
    r13 = sinPhi * (cosPhi * cosTheta * c - sinAlpha * sinTheta);

    r21 = sinPhi2 * cosTheta * sinTheta * c - sinAlpha * cosPhi;
    r22 = sinTheta2 * (cosAlpha * cosPhi2 + sinPhi2) + cosAlpha * cosTheta2;
    r23 = sinPhi * (cosPhi * sinTheta * c + sinAlpha * cosTheta);

    r31 = sinPhi * (cosPhi * cosTheta * c + sinAlpha * sinTheta);
    r32 = sinPhi * (cosPhi * sinTheta * c - sinAlpha * cosTheta);
    r33 = cosAlpha * sinPhi2 + cosPhi2;

    r14 = a1 - a1 * r11 - a2 * r21 - a3 * r31;
    r24 = a2 - a1 * r12 - a2 * r22 - a3 * r32;
    r34 = a3 - a1 * r13 - a2 * r23 - a3 * r33;

    float[] m2 = { r11, r12, r13, 0f, r21, r22, r23, 0f, r31, r32, r33, 0f, r14, r24, r34, 1f };
    return m2;
}

From source file:com.automaster.autoview.server.servlet.ExcelServlet.java

private double caculaDistanciaEntreDoisPontos(double lat1, double lon1, double lat2, double lon2) {

    //Transforma cordenadas em radianos
    /*String lat1Reduzida = String.valueOf(lat1);
    int index = lat1Reduzida.indexOf(".");
    String latFinal1 = lat1Reduzida.substring(0, index+5);
            //from   w ww.  java  2  s  .co m
    String lon1Reduzida = String.valueOf(lon1);
    String lonFinal1 = lon1Reduzida.substring(0, index+5);
            
    String lat2Reduzida = String.valueOf(lat2);
    String latFinal2 = lat2Reduzida.substring(0, index+5);
            
    String lon2Reduzida = String.valueOf(lon2);
    String lonFinal2 = lon2Reduzida.substring(0, index+5);*/

    double lat01 = Math.toRadians(lat1);
    double lon01 = Math.toRadians(lon1);
    double lat02 = Math.toRadians(lat2);
    double lon02 = Math.toRadians(lon2);
    //calcula a distncia em KM atravs da frmula
    double dist = (6371 * Math.acos(
            Math.cos(lat01) * Math.cos(lat02) * Math.cos(lon02 - lon01) + Math.sin(lat01) * Math.sin(lat02)));
    //formata o resultado
    if (dist > 0) {
        BigDecimal decimalFormatado = new BigDecimal(dist).setScale(2, RoundingMode.HALF_EVEN);
        return decimalFormatado.doubleValue();
    }
    return 0;
    //return dist;

}

From source file:gbt.ubt.tool.Orthorectifier.java

public static double vincentyFormulae(double lat1, double long1, double lat2, double long2) {
    double latitude1 = deg2rad(lat1);
    double longitude1 = deg2rad(long1);
    double latitude2 = deg2rad(lat2);
    double longitude2 = deg2rad(long2);
    double a = 6378137.0;
    double f = 1.0 / 298.257223563;
    double b = (1.0 - f) * a;
    double u1 = Math.atan((1.0 - f) * Math.tan(latitude1));
    double u2 = Math.atan((1.0 - f) * Math.tan(latitude2));
    double L = longitude2 - longitude1;
    double lambda = L;
    double diff = 1.0;
    double tol = 10.0e-8;
    double cos2Alpha = 0.0;
    double sinOmega = 0.0;
    double cos2OmegaM = 0.0;
    double cosOmega = 0.0;
    double omega = 0.0;
    while (diff > tol) {
        sinOmega = Math.sqrt(Math.pow((Math.cos(u2) * Math.sin(lambda)), 2) + (Math
                .pow(((Math.cos(u1) * Math.sin(u2)) - (Math.sin(u1) * Math.cos(u2) * Math.cos(lambda))), 2)));
        cosOmega = (Math.sin(u1) * Math.sin(u2)) + (Math.cos(u1) * Math.cos(u2) * Math.cos(lambda));
        omega = Math.atan(sinOmega / cosOmega);
        double sinAlpha = (Math.cos(u1) * Math.cos(u2) * Math.sin(lambda)) / Math.sin(omega);
        cos2Alpha = 1 - (Math.pow(sinAlpha, 2));
        cos2OmegaM = cosOmega - ((2 * Math.sin(u1) * Math.sin(u2)) / cos2Alpha);
        double C = (f / 16.0) * cos2Alpha * (4 + (f * (4 - (3 * cos2Alpha))));
        double lambdaNew = L + ((1 - C) * f * sinAlpha * (omega
                + (C * sinOmega * (cos2OmegaM + (C * cosOmega * (-1 + (2 * Math.pow(cos2OmegaM, 2))))))));
        diff = Math.abs(lambdaNew - lambda);
        lambda = lambdaNew;/* w w w . j  av a 2  s . co  m*/
    }
    double uSquared = cos2Alpha * ((Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(b, 2));
    double A = 1 + ((uSquared / 16384.0)
            * (4096.0 + (uSquared * (-768.0 + (uSquared * (320.0 - (175.0 * uSquared)))))));
    double B = (uSquared / 1024.0) * (256.0 + (uSquared * (-128.0 + (uSquared * (74.0 - (47.0 * uSquared))))));
    double deltaOmega = B * sinOmega * (cos2OmegaM
            + (0.25 * B * ((cosOmega * (-1 + (2 * Math.pow(cos2OmegaM, 2)))) - ((1.0 / 6.0) * B * cos2OmegaM
                    * (-3.0 + (4.0 * Math.pow(sinOmega, 2))) * (-3.0 + (4.0 * Math.pow(cos2OmegaM, 2)))))));
    double s = b * A * (omega - deltaOmega);
    return s;
}

From source file:de.biomedical_imaging.ij.steger.Width.java

public void compute_line_width(float[] dx, float[] dy, int width, int height, double sigma, int mode,
        boolean correct_pos, ArrayList<Line> contours, MutableInt num_contours) {
    float[] grad;
    int i, j, k;/*www  .  ja  v a 2  s.  co m*/
    int r, c, l;
    int x, y, dir;
    Offset[] line;
    int max_line, num_line = 0;
    double length;
    Line cont;
    int num_points, max_num_points;
    double[] width_r, width_l;
    double[] grad_r, grad_l;
    double[] pos_x, pos_y, correct, asymm, contrast;
    double d, dr, dc, drr, drc, dcc;
    double i1, i2, i3, i4, i5, i6, i7, i8, i9;
    double t1, t2, t3, t4, t5, t6;
    double[] eigval = new double[2];
    double[][] eigvec = new double[2][2];
    double a, b, t = 0;
    int num = 0;
    double nx, ny;
    double n1, n2;
    double p1, p2;
    double val;
    double px, py;
    Position p = new Position();
    max_num_points = 0;
    for (i = 0; i < num_contours.getValue(); i++) {
        num_points = contours.get(i).num;
        if (num_points > max_num_points)
            max_num_points = num_points;
    }

    width_l = new double[max_num_points];
    width_r = new double[max_num_points];
    grad_l = new double[max_num_points];
    grad_r = new double[max_num_points];
    pos_x = new double[max_num_points];
    pos_y = new double[max_num_points];
    correct = new double[max_num_points];
    contrast = new double[max_num_points];
    asymm = new double[max_num_points];

    grad = new float[(width * height)];

    length = 2.5 * sigma;
    max_line = (int) Math.ceil(length * 3);
    line = new Offset[max_line];
    for (int o = 0; o < line.length; o++) {
        line[o] = new Offset();
    }

    /* Compute the gradient image. */
    for (r = 0; r < height; r++) {
        for (c = 0; c < width; c++) {
            l = LinesUtil.LINCOOR(r, c, width);
            grad[l] = (float) Math.sqrt(dx[l] * dx[l] + dy[l] * dy[l]);
        }
    }

    for (i = 0; i < num_contours.getValue(); i++) {
        cont = contours.get(i);
        num_points = cont.num;

        for (j = 0; j < num_points; j++) {
            px = cont.row[j];
            py = cont.col[j];
            pos_x[j] = px;
            pos_y[j] = py;
            r = (int) Math.floor(px + 0.5);
            c = (int) Math.floor(py + 0.5);
            nx = Math.cos(cont.angle[j]);
            ny = Math.sin(cont.angle[j]);
            /* Compute the search line. */
            MutableInt num_lineh = new MutableInt(num_line);
            bresenham(nx, ny, 0.0, 0.0, length, line, num_lineh);
            num_line = num_lineh.intValue();
            width_r[j] = width_l[j] = 0;
            /* Look on both sides of the line. */
            for (dir = -1; dir <= 1; dir += 2) {
                for (k = 0; k < num_line; k++) {
                    x = LinesUtil.BR(r + dir * line[k].x, height);
                    y = LinesUtil.BC(c + dir * line[k].y, width);
                    i1 = grad[LinesUtil.LINCOOR(LinesUtil.BR(x - 1, height), LinesUtil.BC(y - 1, width),
                            width)];
                    i2 = grad[LinesUtil.LINCOOR(LinesUtil.BR(x - 1, height), y, width)];
                    i3 = grad[LinesUtil.LINCOOR(LinesUtil.BR(x - 1, height), LinesUtil.BC(y + 1, width),
                            width)];
                    i4 = grad[LinesUtil.LINCOOR(x, LinesUtil.BC(y - 1, width), width)];
                    i5 = grad[LinesUtil.LINCOOR(x, y, width)];
                    i6 = grad[LinesUtil.LINCOOR(x, LinesUtil.BC(y + 1, width), width)];
                    i7 = grad[LinesUtil.LINCOOR(LinesUtil.BR(x + 1, height), LinesUtil.BC(y - 1, width),
                            width)];
                    i8 = grad[LinesUtil.LINCOOR(LinesUtil.BR(x + 1, height), y, width)];
                    i9 = grad[LinesUtil.LINCOOR(LinesUtil.BR(x + 1, height), LinesUtil.BC(y + 1, width),
                            width)];
                    t1 = i1 + i2 + i3;
                    t2 = i4 + i5 + i6;
                    t3 = i7 + i8 + i9;
                    t4 = i1 + i4 + i7;
                    t5 = i2 + i5 + i8;
                    t6 = i3 + i6 + i9;
                    dr = (t3 - t1) / 6;
                    dc = (t6 - t4) / 6;
                    drr = (t1 - 2 * t2 + t3) / 6;
                    dcc = (t4 - 2 * t5 + t6) / 6;
                    drc = (i1 - i3 - i7 + i9) / 4;
                    p.compute_eigenvals(2 * drr, drc, 2 * dcc, eigval, eigvec);
                    val = -eigval[0];
                    if (val > 0.0) {
                        n1 = eigvec[0][0];
                        n2 = eigvec[0][1];
                        a = 2.0 * (drr * n1 * n1 + drc * n1 * n2 + dcc * n2 * n2);
                        b = dr * n1 + dc * n2;
                        MutableDouble th = new MutableDouble(t);
                        MutableInt numh = new MutableInt(num);
                        p.solve_linear(a, b, th, numh);
                        t = th.getValue();
                        num = numh.getValue();
                        if (num != 0) {
                            p1 = t * n1;
                            p2 = t * n2;
                            if (Math.abs(p1) <= 0.5 && Math.abs(p2) <= 0.5) {
                                /* Project the maximum point position perpendicularly onto the
                                   search line. */
                                a = 1;
                                b = nx * (px - (r + dir * line[k].x + p1))
                                        + ny * (py - (c + dir * line[k].y + p2));
                                th = new MutableDouble(t);
                                numh = new MutableInt(num);
                                p.solve_linear(a, b, th, numh);
                                t = th.getValue();
                                num = numh.getValue();
                                d = (-i1 + 2 * i2 - i3 + 2 * i4 + 5 * i5 + 2 * i6 - i7 + 2 * i8 - i9) / 9;
                                if (dir == 1) {
                                    grad_r[j] = d + p1 * dr + p2 * dc + p1 * p1 * drr + p1 * p2 * drc
                                            + p2 * p2 * dcc;
                                    width_r[j] = Math.abs(t);
                                } else {
                                    grad_l[j] = d + p1 * dr + p2 * dc + p1 * p1 * drr + p1 * p2 * drc
                                            + p2 * p2 * dcc;
                                    width_l[j] = Math.abs(t);
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }

        fix_locations(width_l, width_r, grad_l, grad_r, pos_x, pos_y, correct, contrast, asymm, sigma, mode,
                correct_pos, cont);
    }
}

From source file:itdelatrisu.opsu.Utils.java

public static float[] mirrorPoint(float x, float y, float degrees) {
    double dx = x - displayContainer.width / 2d;
    double dy = y - displayContainer.height / 2d;
    double ang = Math.atan2(dy, dx) + (degrees * Math.PI / 180d);
    double d = Math.sqrt(dx * dx + dy * dy);
    return new float[] { (float) (displayContainer.width / 2d + Math.cos(ang) * d),
            (float) (displayContainer.height / 2d + Math.sin(ang) * d) };
}

From source file:net.sourceforge.processdash.ui.web.reports.RadarPlot.java

protected void drawRadar(Graphics2D g2, Rectangle2D plotArea, PlotRenderingInfo info, int pieIndex,
        PieDataset data) {// www  .  j  a  v  a2s. c  om

    // adjust the plot area by the interior spacing value
    double gapHorizontal = plotArea.getWidth() * this.interiorGap;
    double gapVertical = plotArea.getHeight() * this.interiorGap;
    double radarX = plotArea.getX() + gapHorizontal / 2;
    double radarY = plotArea.getY() + gapVertical / 2;
    double radarW = plotArea.getWidth() - gapHorizontal;
    double radarH = plotArea.getHeight() - gapVertical;

    // make the radar area a square if the radar chart is to be circular...
    // NOTE that non-circular radar charts are not currently supported.
    if (true) { //circular) {
        double min = Math.min(radarW, radarH) / 2;
        radarX = (radarX + radarX + radarW) / 2 - min;
        radarY = (radarY + radarY + radarH) / 2 - min;
        radarW = 2 * min;
        radarH = 2 * min;
    }

    double radius = radarW / 2;
    double centerX = radarX + radarW / 2;
    double centerY = radarY + radarH / 2;

    Rectangle2D radarArea = new Rectangle2D.Double(radarX, radarY, radarW, radarH);

    // plot the data (unless the dataset is null)...
    if ((data != null) && (data.getKeys().size() > 0)) {

        // get a list of categories...
        List keys = data.getKeys();
        int numAxes = keys.size();

        // draw each of the axes on the radar chart, and register
        // the shape of the radar line.

        double multiplier = 1.0;
        GeneralPath lineShape = new GeneralPath(GeneralPath.WIND_NON_ZERO, numAxes + 1);
        GeneralPath gridShape = new GeneralPath(GeneralPath.WIND_NON_ZERO, numAxes + 1);

        int axisNumber = -1;
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            Comparable currentKey = (Comparable) iterator.next();
            axisNumber++;
            Number dataValue = data.getValue(currentKey);

            double value = (dataValue != null ? dataValue.doubleValue() : 0);
            if (value > 1 || Double.isNaN(value) || Double.isInfinite(value))
                value = 1.0;
            if (value < 0)
                value = 0.0;
            multiplier *= value;

            double angle = 2 * Math.PI * axisNumber / numAxes;
            double deltaX = Math.sin(angle) * radius;
            double deltaY = -Math.cos(angle) * radius;

            // draw the spoke
            g2.setPaint(axisPaint);
            g2.setStroke(axisStroke);
            Line2D line = new Line2D.Double(centerX, centerY, centerX + deltaX, centerY + deltaY);
            g2.draw(line);

            // register the grid line and the shape line
            if (axisNumber == 0) {
                gridShape.moveTo((float) deltaX, (float) deltaY);
                lineShape.moveTo((float) (deltaX * value), (float) (deltaY * value));
            } else {
                gridShape.lineTo((float) deltaX, (float) deltaY);
                lineShape.lineTo((float) (deltaX * value), (float) (deltaY * value));
            }

            if (showAxisLabels) {
                // draw the label
                double labelX = centerX + deltaX * (1 + axisLabelGap);
                double labelY = centerY + deltaY * (1 + axisLabelGap);
                String label = currentKey.toString();
                drawLabel(g2, radarArea, label, axisNumber, labelX, labelY);
            }

        }
        gridShape.closePath();
        lineShape.closePath();

        // draw five gray concentric gridlines
        g2.translate(centerX, centerY);
        g2.setPaint(gridLinePaint);
        g2.setStroke(gridLineStroke);
        for (int i = 5; i > 0; i--) {
            Shape scaledGrid = gridShape
                    .createTransformedShape(AffineTransform.getScaleInstance(i / 5.0, i / 5.0));
            g2.draw(scaledGrid);
        }

        // get the color for the plot shape.
        Paint dataPaint = plotLinePaint;
        if (dataPaint == ADAPTIVE_COLORING) {
            //multiplier = Math.exp(Math.log(multiplier) * 2 / numAxes);
            dataPaint = getMultiplierColor((float) multiplier);
        }

        // compute a slightly transparent version of the plot color for
        // the fill.
        Paint dataFill = null;
        if (dataPaint instanceof Color && getForegroundAlpha() != 1.0)
            dataFill = new Color(((Color) dataPaint).getRed() / 255f, ((Color) dataPaint).getGreen() / 255f,
                    ((Color) dataPaint).getBlue() / 255f, getForegroundAlpha());
        else
            dataFill = dataPaint;

        // draw the plot shape.  First fill with a parially
        // transparent color, then stroke with the opaque color.
        g2.setPaint(dataFill);
        g2.fill(lineShape);
        g2.setPaint(dataPaint);
        g2.setStroke(plotLineStroke);
        g2.draw(lineShape);

        // cleanup the graphics context.
        g2.translate(-centerX, -centerY);
    }
}

From source file:etomica.virial.MCMoveClusterRingRegrowOrientation.java

public void rotateVectorV(double angle, IVector axis, IVectorMutable v) {
    double q0 = Math.cos(angle / 2.0);
    double sth2 = Math.sin(angle / 2.0);
    IVectorMutable a1 = space.makeVector();
    a1.E(axis);// w w w. j  a va2  s.  c  o m
    a1.TE(sth2);
    double q1 = a1.getX(0);
    double q2 = a1.getX(1);
    double q3 = a1.getX(2);
    Quaternion q = new Quaternion(q0, q1, q2, q3);
    Quaternion vec = new Quaternion(0, v.getX(0), v.getX(1), v.getX(2));
    Quaternion w = q.multiply(vec).multiply(q.getConjugate());
    if (Math.abs(w.getScalarPart()) > 1E-10)
        throw new RuntimeException("Quaternion product is not a vector!");
    v.E(w.getVectorPart());
}

From source file:business.ImageManager.java

private Path2D.Double createArrow() {
    int length = this.getYellowBall().getIconWidth() - 5;
    int barb = this.getYellowBall().getIconWidth() / 5;
    double angle = Math.toRadians(20);
    Path2D.Double path = new Path2D.Double();
    path.moveTo(-length / 2, 0);//www  .ja v  a2  s . c  o  m
    path.lineTo(length / 2, 0);
    double x = length / 2 - barb * Math.cos(angle);
    double y = barb * Math.sin(angle);
    path.lineTo(x, y);
    x = length / 2 - barb * Math.cos(-angle);
    y = barb * Math.sin(-angle);
    path.moveTo(length / 2, 0);
    path.lineTo(x, y);
    return path;
}