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:charts.Chart.java

public static void GraficoSeries() {
    //System.out.println("After initComponents");

    JFrame janela = new JFrame("Example of the series graphic");
    janela.getContentPane().setLayout(null);

    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
    jLabel1.setText("");
    janela.getContentPane().add(jLabel1);

    y_of_x = new double[n_points];
    x = new double[n_points];

    XYSeries series1 = new XYSeries("Cos(x) versus x");
    XYSeries series2 = new XYSeries("Cos^2(x) versus x");

    for (int i = 0; i < n_points; i++) {//calculate the data to be plotted

        y_of_x[i] = Math.cos(i * Math.PI / 180);
        series1.add((double) i, y_of_x[i]);//add values to the series

        series2.add((double) i, Math.pow(y_of_x[i], 2));
    }// w  ww .j  a  va2s .  com

    XYDataset dataset1 = new XYSeriesCollection(series1);
    XYDataset dataset2 = new XYSeriesCollection(series2);

    CombinedDomainXYPlot parent = new CombinedDomainXYPlot(new NumberAxis("x-angle argument"));

    XYItemRenderer renderer1 = new StandardXYItemRenderer();
    XYPlot subplot1 = new XYPlot(dataset1, null, new NumberAxis("Cos(x)"), renderer1);
    NumberAxis axis1 = (NumberAxis) subplot1.getRangeAxis();
    axis1.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 10));
    axis1.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));
    axis1.setAutoRangeIncludesZero(false);
    parent.add(subplot1, 1);

    XYItemRenderer renderer2 = new StandardXYItemRenderer();
    XYPlot subplot2 = new XYPlot(dataset2, null, new NumberAxis("Cos^2(x)"), renderer2);
    NumberAxis axis2 = (NumberAxis) subplot2.getRangeAxis();
    axis2.setTickLabelFont(new Font("Monospaced", Font.PLAIN, 10));
    axis2.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));
    axis2.setAutoRangeIncludesZero(false);
    parent.add(subplot2, 1);

    JFreeChart chart = new JFreeChart("Cos(x) versus x", parent);

    ChartPanel myChart = new ChartPanel(chart);
    janela.setSize(500, 600);
    janela.setContentPane(myChart);

    janela.setVisible(true);
}

From source file:lu.fisch.unimozer.Diagram.java

private void drawExtends(Graphics2D g, Point pFrom, Point pTo) {
    int ARROW_SIZE = 16;
    double ARROW_ANGLE = Math.PI / 6;

    //g.setColor(Color.BLACK);
    double angle = Math.atan2(-(pFrom.y - pTo.y), pFrom.x - pTo.x);

    Point pArrow = new Point(pTo.x + (int) ((ARROW_SIZE - 2) * Math.cos(angle)),
            pTo.y - (int) ((ARROW_SIZE - 2) * Math.sin(angle)));

    // draw the arrow head
    int[] xPoints = { pTo.x, pTo.x + (int) ((ARROW_SIZE) * Math.cos(angle + ARROW_ANGLE)),
            pTo.x + (int) (ARROW_SIZE * Math.cos(angle - ARROW_ANGLE)) };
    int[] yPoints = { pTo.y, pTo.y - (int) ((ARROW_SIZE) * Math.sin(angle + ARROW_ANGLE)),
            pTo.y - (int) (ARROW_SIZE * Math.sin(angle - ARROW_ANGLE)) };

    g.drawPolygon(xPoints, yPoints, 3);/*  ww w  .  ja  v  a  2  s .  c  o  m*/
    g.drawLine(pFrom.x, pFrom.y, pArrow.x, pArrow.y);
}

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

@Test
public void secNegative() {
    try {//w ww . j  ava 2  s. com
        Expression expression = getExpressionWithFunctionContext("sec(-10)");
        assertEquals(ExpressionType.DOUBLE, expression.getExpressionType());
        assertEquals(1.0 / Math.cos(-10), expression.evaluateNumerical(), 1e-15);
    } catch (ExpressionException e) {
        assertNotNull(e.getMessage());
    }
}

From source file:fr.fg.server.core.TerritoryManager.java

private static Polygon createPolygon(float[][] coords, int offset, int reduction) {
    int[] xpts = new int[coords.length], ypts = new int[coords.length];

    for (int j = 0; j < coords.length; j++) {
        // Calcule l'angle form par le segment suivant le point
        float dx1 = coords[(j + 1) % coords.length][0] - coords[j][0];
        float dy1 = coords[(j + 1) % coords.length][1] - coords[j][1];
        double angle1 = Math.atan2(dy1, dx1);

        // Calcule l'angle form par le segment prcdent le point
        float dx2 = coords[(j - 1 + coords.length) % coords.length][0] - coords[j][0];
        float dy2 = coords[(j - 1 + coords.length) % coords.length][1] - coords[j][1];
        double angle2 = Math.atan2(dy2, dx2);

        // Dtermine l'angle entre les deux segments, en supposant que le
        // polygone est convexe (angles < pi)
        double angle = (-angle1 - angle2) / 2;
        if (Math.abs(angle1 - angle2) > Math.PI)
            angle += Math.PI;/* w  w w  .  j  a v  a 2 s  . co  m*/

        // Dcale le point vers l'intrieur du polygone selon l'angle form
        // par les deux segments
        xpts[j] = (int) (Math.round(coords[j][0]) + Math.cos(angle) * reduction + offset);
        ypts[j] = (int) (Math.round(-coords[j][1]) + Math.sin(angle) * reduction + offset);
    }

    return new Polygon(xpts, ypts, coords.length);
}

From source file:org.rdv.viz.spectrum.SpectrumAnalyzerPanel.java

/**
 * Create a Blackman window./*  w  ww .  java  2s . co  m*/
 */
private void blackman() {
    int N = window.length;
    double pi = Math.PI;
    for (int n = 0; n < N; n++) {
        window[n] = 0.42 - (0.5 * Math.cos(2 * pi * n / (N - 1))) + (0.08 * Math.cos(4 * pi * n / (N - 1)));
    }
}

From source file:org.rdv.viz.spectrum.SpectrumAnalyzerPanel.java

/**
 * Create a Hamming window.//from w w w .j ava2 s .c  o m
 */
private void hamming() {
    int N = window.length;
    double pi = Math.PI;
    for (int n = 0; n < window.length; n++) {
        window[n] = 0.54 - (0.46 * Math.cos(2 * pi * n / (N - 1)));
    }
}

From source file:GearTest.java

void addCylinderSkins(float shaftRadius, float length, int normalDirection, Appearance look) {
    int insideShaftVertexCount; // #(vertices) for shaft
    int insideShaftStripCount[] = new int[1]; // #(vertices) in strip/strip
    double toothStartAngle, nextToothStartAngle, toothValleyStartAngle;

    // A ray from the gear center, used in normal calculations
    float xDirection, yDirection;

    // The z coordinates for the body disks
    final float frontZ = -0.5f * length;
    final float rearZ = 0.5f * length;

    // Temporary variables for storing coordinates, points, and vectors
    float xShaft3, yShaft3, xShaft4, yShaft4;
    Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
    Vector3f surfaceNormal = new Vector3f();

    Shape3D newShape;/*from w  w  w .j  a v  a2s  .c  o  m*/
    int index;
    int firstIndex;
    int secondIndex;

    /*
     * Construct gear's inside shaft cylinder First the tooth's up, flat
     * outer, and down distances Second the tooth's flat inner distance
     * 
     * Outward facing vertex order: 0_______2____4 | /| /| | / | / | | / | / |
     * |/______|/___| 1 3 5
     * 
     * Inward facing vertex order: 1_______3____5 |\ |\ | | \ | \ | | \ | \ |
     * |______\|___\| 0 2 4
     */
    insideShaftVertexCount = 4 * toothCount + 2;
    insideShaftStripCount[0] = insideShaftVertexCount;

    TriangleStripArray insideShaft = new TriangleStripArray(insideShaftVertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS, insideShaftStripCount);
    xShaft3 = shaftRadius * (float) Math.cos(gearStartAngle);
    yShaft3 = shaftRadius * (float) Math.sin(gearStartAngle);

    if (normalDirection == OutwardNormals) {
        surfaceNormal.set(1.0f, 0.0f, 0.0f);
        firstIndex = 1;
        secondIndex = 0;
    } else {
        surfaceNormal.set(-1.0f, 0.0f, 0.0f);
        firstIndex = 0;
        secondIndex = 1;
    }

    // Coordinate labeled 0 in the strip
    coordinate.set(shaftRadius, 0.0f, frontZ);
    insideShaft.setCoordinate(firstIndex, coordinate);
    insideShaft.setNormal(firstIndex, surfaceNormal);

    // Coordinate labeled 1 in the strip
    coordinate.set(shaftRadius, 0.0f, rearZ);
    insideShaft.setCoordinate(secondIndex, coordinate);
    insideShaft.setNormal(secondIndex, surfaceNormal);

    for (int count = 0; count < toothCount; count++) {
        index = 2 + count * 4;

        toothStartAngle = circularPitchAngle * (double) count;
        toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement;
        nextToothStartAngle = toothStartAngle + circularPitchAngle;

        xDirection = (float) Math.cos(toothValleyStartAngle);
        yDirection = (float) Math.sin(toothValleyStartAngle);
        xShaft3 = shaftRadius * xDirection;
        yShaft3 = shaftRadius * yDirection;
        if (normalDirection == OutwardNormals)
            surfaceNormal.set(xDirection, yDirection, 0.0f);
        else
            surfaceNormal.set(-xDirection, -yDirection, 0.0f);

        // Coordinate labeled 2 in the strip
        coordinate.set(xShaft3, yShaft3, frontZ);
        insideShaft.setCoordinate(index + firstIndex, coordinate);
        insideShaft.setNormal(index + firstIndex, surfaceNormal);

        // Coordinate labeled 3 in the strip
        coordinate.set(xShaft3, yShaft3, rearZ);
        insideShaft.setCoordinate(index + secondIndex, coordinate);
        insideShaft.setNormal(index + secondIndex, surfaceNormal);

        xDirection = (float) Math.cos(nextToothStartAngle);
        yDirection = (float) Math.sin(nextToothStartAngle);
        xShaft4 = shaftRadius * xDirection;
        yShaft4 = shaftRadius * yDirection;
        if (normalDirection == OutwardNormals)
            surfaceNormal.set(xDirection, yDirection, 0.0f);
        else
            surfaceNormal.set(-xDirection, -yDirection, 0.0f);

        // Coordinate labeled 4 in the strip
        coordinate.set(xShaft4, yShaft4, frontZ);
        insideShaft.setCoordinate(index + 2 + firstIndex, coordinate);
        insideShaft.setNormal(index + 2 + firstIndex, surfaceNormal);

        // Coordinate labeled 5 in the strip
        coordinate.set(xShaft4, yShaft4, rearZ);
        insideShaft.setCoordinate(index + 2 + secondIndex, coordinate);
        insideShaft.setNormal(index + 2 + secondIndex, surfaceNormal);

    }
    newShape = new Shape3D(insideShaft, look);
    this.addChild(newShape);
}

From source file:ExBackgroundImage.java

public Arch(double startPhi, double endPhi, int nPhi, double startTheta, double endTheta, int nTheta,
        double startPhiRadius, double endPhiRadius, double startPhiThickness, double endPhiThickness,
        Appearance app) {/*from  www  . j av a 2  s.  c o m*/
    double theta, phi, radius, radius2, thickness;
    double x, y, z;
    double[] xyz = new double[3];
    float[] norm = new float[3];
    float[] tex = new float[3];

    // Compute some values for our looping
    double deltaTheta = (endTheta - startTheta) / (double) (nTheta - 1);
    double deltaPhi = (endPhi - startPhi) / (double) (nPhi - 1);
    double deltaTexX = 1.0 / (double) (nTheta - 1);
    double deltaTexY = 1.0 / (double) (nPhi - 1);
    double deltaPhiRadius = (endPhiRadius - startPhiRadius) / (double) (nPhi - 1);
    double deltaPhiThickness = (endPhiThickness - startPhiThickness) / (double) (nPhi - 1);

    boolean doThickness = true;
    if (startPhiThickness == 0.0 && endPhiThickness == 0.0)
        doThickness = false;

    //  Create geometry
    int vertexCount = nTheta * nPhi;
    if (doThickness)
        vertexCount *= 2;
    int indexCount = (nTheta - 1) * (nPhi - 1) * 4; // Outer surface
    if (doThickness) {
        indexCount *= 2; // plus inner surface
        indexCount += (nPhi - 1) * 4 * 2; // plus left & right edges
    }

    IndexedQuadArray polys = new IndexedQuadArray(vertexCount,
            GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2, indexCount);

    //
    //  Compute coordinates, normals, and texture coordinates
    //
    theta = startTheta;
    tex[0] = 0.0f;
    int index = 0;
    for (int i = 0; i < nTheta; i++) {
        phi = startPhi;
        radius = startPhiRadius;
        thickness = startPhiThickness;
        tex[1] = 0.0f;

        for (int j = 0; j < nPhi; j++) {
            norm[0] = (float) (Math.cos(phi) * Math.cos(theta));
            norm[1] = (float) (Math.sin(phi));
            norm[2] = (float) (-Math.cos(phi) * Math.sin(theta));
            xyz[0] = radius * norm[0];
            xyz[1] = radius * norm[1];
            xyz[2] = radius * norm[2];
            polys.setCoordinate(index, xyz);
            polys.setNormal(index, norm);
            polys.setTextureCoordinate(index, tex);
            index++;

            if (doThickness) {
                radius2 = radius - thickness;
                xyz[0] = radius2 * norm[0];
                xyz[1] = radius2 * norm[1];
                xyz[2] = radius2 * norm[2];
                norm[0] *= -1.0f;
                norm[1] *= -1.0f;
                norm[2] *= -1.0f;
                polys.setCoordinate(index, xyz);
                polys.setNormal(index, norm);
                polys.setTextureCoordinate(index, tex);
                index++;
            }

            phi += deltaPhi;
            radius += deltaPhiRadius;
            thickness += deltaPhiThickness;
            tex[1] += deltaTexY;
        }
        theta += deltaTheta;
        tex[0] += deltaTexX;
    }

    //
    //  Compute coordinate indexes
    //  (also used as normal and texture indexes)
    //
    index = 0;
    int phiRow = nPhi;
    int phiCol = 1;
    if (doThickness) {
        phiRow += nPhi;
        phiCol += 1;
    }
    int[] indices = new int[indexCount];

    // Outer surface
    int n;
    for (int i = 0; i < nTheta - 1; i++) {
        for (int j = 0; j < nPhi - 1; j++) {
            n = i * phiRow + j * phiCol;
            indices[index + 0] = n;
            indices[index + 1] = n + phiRow;
            indices[index + 2] = n + phiRow + phiCol;
            indices[index + 3] = n + phiCol;
            index += 4;
        }
    }

    // Inner surface
    if (doThickness) {
        for (int i = 0; i < nTheta - 1; i++) {
            for (int j = 0; j < nPhi - 1; j++) {
                n = i * phiRow + j * phiCol;
                indices[index + 0] = n + 1;
                indices[index + 1] = n + phiCol + 1;
                indices[index + 2] = n + phiRow + phiCol + 1;
                indices[index + 3] = n + phiRow + 1;
                index += 4;
            }
        }
    }

    // Edges
    if (doThickness) {
        for (int j = 0; j < nPhi - 1; j++) {
            n = j * phiCol;
            indices[index + 0] = n;
            indices[index + 1] = n + phiCol;
            indices[index + 2] = n + phiCol + 1;
            indices[index + 3] = n + 1;
            index += 4;
        }
        for (int j = 0; j < nPhi - 1; j++) {
            n = (nTheta - 1) * phiRow + j * phiCol;
            indices[index + 0] = n;
            indices[index + 1] = n + 1;
            indices[index + 2] = n + phiCol + 1;
            indices[index + 3] = n + phiCol;
            index += 4;
        }
    }

    polys.setCoordinateIndices(0, indices);
    polys.setNormalIndices(0, indices);
    polys.setTextureCoordinateIndices(0, indices);

    //
    //  Build a shape
    //
    arch = new Shape3D();
    arch.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    arch.setGeometry(polys);
    arch.setAppearance(app);
    addChild(arch);
}

From source file:BeanContainer.java

public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Color colorRetainer = g.getColor();

        g.setColor(getBackground());/*from   ww w.ja v  a2  s .  c o m*/
        g.fillRect(0, 0, getWidth(), getHeight());
        getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

        m_calendar.setTime(new Date()); // get current time
        int hrs = m_calendar.get(Calendar.HOUR_OF_DAY);
        int min = m_calendar.get(Calendar.MINUTE);

        g.setColor(getForeground());
        if (m_digital) {
            String time = "" + hrs + ":" + min;
            g.setFont(getFont());
            FontMetrics fm = g.getFontMetrics();
            int y = (getHeight() + fm.getAscent()) / 2;
            int x = (getWidth() - fm.stringWidth(time)) / 2;
            g.drawString(time, x, y);
        } else {
            int x = getWidth() / 2;
            int y = getHeight() / 2;
            int rh = getHeight() / 4;
            int rm = getHeight() / 3;

            double ah = ((double) hrs + min / 60.0) / 6.0 * Math.PI;
            double am = min / 30.0 * Math.PI;

            g.drawLine(x, y, (int) (x + rh * Math.sin(ah)), (int) (y - rh * Math.cos(ah)));
            g.drawLine(x, y, (int) (x + rm * Math.sin(am)), (int) (y - rm * Math.cos(am)));
        }

        g.setColor(colorRetainer);
    }