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:beans.BL.java

public void test(TPVObject tpv) {
    /**/*from   w  w w.  j av a  2s.co m*/
     * Neue Idee alles zu speichern
     */
    /*Umwandlung von Double in LDT Problem das es zu ungenau ist
     Double test = (tpv.getTimestamp());
     long int_timestamp = test.longValue();
     LocalDateTime ldt = LocalDateTime.ofEpochSecond(int_timestamp, 0, ZoneOffset.UTC);
     */

    System.out.println(
            tpv.getTimestamp() + " Longitude: " + tpv.getLatitude() + " Latitude: " + tpv.getLongitude());
    //TODO:
    /**
     * Algorithmus um die gefahrenen Km zu messen
     */

    if (tpv.getSpeed() > 2.77) {
        if (latOld == -1 && lonOld == -1) {
            latNew = tpv.getLatitude();
            lonNew = tpv.getLongitude();
        }
        latOld = latNew;
        lonOld = lonNew;
        latNew = tpv.getLatitude();
        lonNew = tpv.getLongitude();

        double R = 6371000; // metres

        double dLat = Math.toRadians(latNew - latOld);
        double dLon = Math.toRadians(lonNew - lonOld);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(latOld))
                * Math.cos(Math.toRadians(latNew)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        d = d + (R * c);
        System.out.println("Distance:" + d + "Speed:" + tpv.getSpeed());
    }
    Point point = new Point(LocalDateTime.now(), tpv.getLatitude(), tpv.getLongitude(), d, tpv.getSpeed(),
            track);

    try {
        points.put(point);
    } catch (InterruptedException ex) {
        Logger.getLogger(BL.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        System.out.println("points size:" + points.size());
        System.out.println("traks size:" + tracks.size());
        for (Track track1 : tracks) {
            System.out.println("track: " + track1.getId());
        }
        data_manager.writeFile(points);
    } catch (ParseException ex) {
        Logger.getLogger(BL.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(BL.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Randoms.java

/** Return a random double drawn from a Gaussian distribution with mean 0 and variance 1. */
public synchronized double nextGaussian() {
    if (!haveNextGaussian) {
        double v1 = nextUniform(), v2 = nextUniform();
        double x1, x2;
        x1 = Math.sqrt(-2 * Math.log(v1)) * Math.cos(2 * Math.PI * v2);
        x2 = Math.sqrt(-2 * Math.log(v1)) * Math.sin(2 * Math.PI * v2);
        nextGaussian = x2;/*from  w  w  w .j a  v  a  2 s .  c  om*/
        haveNextGaussian = true;
        return x1;
    } else {
        haveNextGaussian = false;
        return nextGaussian;
    }
}

From source file:com.opengamma.analytics.math.TrigonometricFunctionUtils.java

public static ComplexNumber sinh(final ComplexNumber z) {
    Validate.notNull(z, "z");
    return new ComplexNumber(Math.sinh(z.getReal()) * Math.cos(z.getImaginary()),
            Math.cosh(z.getReal()) * Math.sin(z.getImaginary()));
}

From source file:com.nextbreakpoint.nextfractal.mandelbrot.core.Expression.java

public static double funcCos(double x) {
    return Math.cos(x);
}

From source file:net.sf.dsp4j.octave.packages.signal_1_2_0.NCauer.java

public NCauer(double Rp, double Rs, int n) {

    // Cutoff frequency = 1:
    double wp = 1;

    // Stop band edge ws:
    double ws = __ellip_ws(n, Rp, Rs);

    double k = wp / ws;
    double k1 = Math.sqrt(1.0 - Math.pow(k, 2));
    double q0 = (1.0 / 2.0) * ((1.0 - Math.sqrt(k1)) / (1 + Math.sqrt(k1)));
    double q = q0 + 2.0 * Math.pow(q0, 5) + 15.0 * Math.pow(q0, 9) + 150.0 * Math.pow(q0, 13); //%(....)
    double D = (Math.pow(10, 0.1 * Rs) - 1.0) / (Math.pow(10, (0.1 * Rp)) - 1.0);

    //Filter order maybe this, but not used now:
    //n=ceil(log10(16*D)/log10(1/q))

    double l = (1.0 / (2.0 * n)) * Math.log((Math.pow(10, 0.05 * Rp) + 1.0) / (Math.pow(10, 0.05 * Rp) - 1.0));
    double sig01 = 0;
    double sig02 = 0;
    for (int m = 0; m <= 30; m++) {
        sig01 = sig01 + Math.pow((-1), m) * Math.pow(q, (m * (m + 1))) * Math.sinh((2 * m + 1) * l);
    }/*  ww  w  . j  a  va 2 s  .  c  o m*/
    for (int m = 1; m <= 30; m++) {
        sig02 = sig02 + Math.pow(-1.0, m) * Math.pow(q, (Math.pow(m, 2))) * Math.cosh(2 * m * l);
    }
    double sig0 = Math.abs((2.0 * Math.pow(q, (1.0 / 4.0)) * sig01) / (1.0 + 2.0 * sig02));

    double w = Math.sqrt((1.0 + k * Math.pow(sig0, 2)) * (1.0 + Math.pow(sig0, 2) / k));

    int r;
    if (n % 2 != 0) {
        r = (n - 1) / 2;
    } else {
        r = n / 2;
    }

    double[] wi = new double[r];
    for (int ii = 1; ii <= r; ii++) {
        double mu;
        if (n % 2 != 0) {
            mu = ii;
        } else {
            mu = (double) ii - 0.5;
        }
        double soma1 = 0;
        for (int m = 0; m <= 30; m++) {
            soma1 = soma1 + 2.0 * Math.pow(q, (1.0 / 4.0)) * (Math.pow(-1.0, m) * Math.pow(q, (m * (m + 1)))
                    * Math.sin(((2.0 * m + 1.0) * Math.PI * mu) / n));
        }
        double soma2 = 0;
        for (int m = 1; m <= 30; m++) {
            soma2 = soma2 + 2.0 * (Math.pow(-1.0, m) * Math.pow(q, (Math.pow(m, 2)))
                    * Math.cos((2.0 * m * Math.PI * mu) / n));
        }
        wi[ii - 1] = (soma1 / (1.0 + soma2));
    }

    double[] Vi = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        Vi[i] = Math.sqrt((1.0 - (k * (Math.pow(wi[i], 2)))) * (1.0 - (Math.pow(wi[i], 2)) / k));
    }
    double[] A0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        A0i[i] = 1.0 / (Math.pow(wi[i], 2));
    }
    double[] sqrA0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        sqrA0i[i] = 1.0 / wi[i];
    }

    double[] B0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        B0i[i] = (Math.pow((sig0 * Vi[i]), 2) + Math.pow((w * wi[i]), 2))
                / Math.pow((1.0 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)), 2);
    }

    double C01;
    if (wi.length == 0) {
        C01 = 1.0;
    } else {
        C01 = B0i[0] / A0i[0];
        for (int i = 1; i < wi.length; i++) {
            C01 *= B0i[i] / A0i[i];
        }
    }

    //Gain T0:
    if (n % 2 != 0) {
        T0 = sig0 * C01 * Math.sqrt(ws);
    } else {
        T0 = Math.pow(10, (-0.05 * Rp)) * C01;
    }

    //zeros:
    zer = new Complex[sqrA0i.length * 2];
    for (int i = 0; i < sqrA0i.length; i++) {
        zer[i] = Complex.valueOf(0.0, sqrA0i[i]);
        zer[i + sqrA0i.length] = Complex.valueOf(0.0, -sqrA0i[i]);
    }

    //poles:
    pol = new Complex[Vi.length * 2];
    for (int i = 0; i < Vi.length; i++) {
        pol[i] = new Complex(-2.0 * sig0 * Vi[i], 2.0 * wi[i] * w)
                .divide(2.0 * (1 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)));
        pol[i + Vi.length] = new Complex(-2.0 * sig0 * Vi[i], -2.0 * wi[i] * w)
                .divide(2.0 * (1 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)));
    }

    //If n odd, there is a real pole  -sig0:
    if (n % 2 != 0) {
        pol = Arrays.copyOf(pol, pol.length + 1);
        pol[pol.length - 1] = new Complex(-sig0);
    }

    for (int i = 0; i < pol.length; i++) {
        pol[i] = pol[i].multiply(Math.sqrt(ws));
    }
    for (int i = 0; i < zer.length; i++) {
        zer[i] = zer[i].multiply(Math.sqrt(ws));
    }

}

From source file:com.kentdisplays.synccardboarddemo.Page.java

/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 *
 * @param is InputStream to the page to load the path data from.
 *///from   ww w.j a  va2 s . c  om
public Page(InputStream is, int glProgram, int direction) {

    this.mModel = new float[16];
    this.mGlProgram = glProgram;

    // Calculate the coordinates from the given path.
    ArrayList<Path> paths = pathsFromSamplePageInputStream(is);
    float finalCoords[] = {};
    float finalNormals[] = {};
    float finalColors[] = {};
    mNumberOfPaths = paths.size();
    for (int i = 0; i < mNumberOfPaths; i++) {
        Path path = paths.get(i);
        float x1 = (path.x1 / 13942 * 2) - 1;
        float y1 = (path.y1 / 20280 * 2) - 1;
        float x2 = (path.x2 / 13942 * 2) - 1;
        float y2 = (path.y2 / 20280 * 2) - 1;
        float width = path.width / 3000;
        width = width < 0.013f ? 0.013f : width; // Width should be at least 0.013

        float distance = (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        float angle = (float) Math.PI / 2 - (float) Math.asin((x2 - x1) / distance);
        float xdiff = (width / 2) * (float) Math.sin(angle);
        float ydiff = (width / 2) * (float) Math.cos(angle);

        float coords[] = { x1 - xdiff, y1 - ydiff, 1.0f, // top left
                x2 - xdiff, y2 - ydiff, 1.0f, // bottom left
                x1 + xdiff, y1 + ydiff, 1.0f, // top right
                x2 - xdiff, y2 - ydiff, 1.0f, // bottom left
                x2 + xdiff, y2 + ydiff, 1.0f, // bottom right
                x1 + xdiff, y1 + ydiff, 1.0f, // top right
        };

        float normals[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
                1.0f, 0.0f, 0.0f, 1.0f, };

        float colors[] = { 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f,
                0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f,
                0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, };

        finalCoords = Floats.concat(finalCoords, coords);
        finalNormals = Floats.concat(finalNormals, normals);
        finalColors = Floats.concat(finalColors, colors);
    }

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(finalCoords.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    mPageVertices = bbVertices.asFloatBuffer();
    mPageVertices.put(finalCoords);
    mPageVertices.position(0);

    ByteBuffer bbNormals = ByteBuffer.allocateDirect(finalNormals.length * 4);
    bbNormals.order(ByteOrder.nativeOrder());
    mPageNormals = bbNormals.asFloatBuffer();
    mPageNormals.put(finalNormals);
    mPageNormals.position(0);

    ByteBuffer bbColors = ByteBuffer.allocateDirect(finalColors.length * 4);
    bbColors.order(ByteOrder.nativeOrder());
    mPageColors = bbColors.asFloatBuffer();
    mPageColors.put(finalColors);
    mPageColors.position(0);

    // Correctly place the page in the world.
    Matrix.setIdentityM(mModel, 0);
    switch (direction) {
    case 0:
        Matrix.translateM(mModel, 0, 0, 0, -mDistance); //Front.
        break;
    case 1:
        Matrix.translateM(mModel, 0, -mDistance, 0, 0); // Left.
        Matrix.rotateM(mModel, 0, 90, 0, 1f, 0);
        break;
    case 2:
        Matrix.translateM(mModel, 0, 0, 0, mDistance); // Behind.
        Matrix.rotateM(mModel, 0, 180, 0, 1f, 0);
        break;
    case 3:
        Matrix.translateM(mModel, 0, mDistance, 0, 0); // Right.
        Matrix.rotateM(mModel, 0, 270, 0, 1f, 0);
        break;
    }
}

From source file:Satellite.java

public static double[] Convert_To_Lat_Long(Vector3D posVec) {
    double Xcomp = posVec.getX();
    double Ycomp = posVec.getY();
    double Zcomp = posVec.getZ();

    double longitude;
    double latitude;
    double altitude;

    //Done so all cases of longitudes are right
    if (Ycomp > 0) {
        if (Xcomp > 0) {
            longitude = Math.toDegrees(Math.atan(Ycomp / Xcomp));
        } else {/* w w w .  j a  v  a  2s. c  o m*/
            longitude = 180 - Math.toDegrees(Math.atan(Math.abs(Ycomp / Xcomp)));
        }
    } else {
        if (Xcomp > 0) {
            longitude = -1 * Math.toDegrees(Math.atan(Math.abs(Ycomp / Xcomp)));
        } else {
            longitude = -1 * (180 - Math.toDegrees(Math.atan(Ycomp / Xcomp)));
        }
    }

    //Calculate latitude
    latitude = Math.toDegrees(Math.atan(Zcomp / Math.sqrt(Xcomp * Xcomp + Ycomp * Ycomp)));

    //Calculate radius and altitude
    double EER = Constants.WGS84_EARTH_EQUATORIAL_RADIUS; //Earth Equator Radius in meters
    double EPR = EER - EER * Constants.WGS84_EARTH_FLATTENING; //Earth Polar Radius in meters

    double earthRadius = Math
            .sqrt((Math.pow(EPR * EPR * Math.cos(latitude), 2) + Math.pow(EER * EER * Math.cos(latitude), 2))
                    / (Math.pow(EPR * Math.cos(latitude), 2) + Math.pow(EER * Math.cos(latitude), 2)));
    double orbitRadius = Math.sqrt(Xcomp * Xcomp + Ycomp * Ycomp + Zcomp * Zcomp);
    altitude = orbitRadius - earthRadius;

    return new double[] { latitude, longitude, altitude };
}

From source file:edu.umass.cs.iesl.pdf2meta.cli.extract.pdfbox.pagedrawer.Invoke.java

/**
 * process : Do : Paint the specified XObject (section 4.7).
 * @param operator The operator that is being executed.
 * @param arguments List/*from ww  w .ja va 2 s . c om*/
 * @throws java.io.IOException If there is an error invoking the sub object.
 */
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    GraphicsAwarePDFStreamEngine drawer = (GraphicsAwarePDFStreamEngine) context;
    PDPage page = drawer.getPage();
    COSName objectName = (COSName) arguments.get(0);
    Map xobjects = drawer.getResources().getXObjects();
    PDXObject xobject = (PDXObject) xobjects.get(objectName.getName());
    if (xobject == null) {
        log.warn("Can't find the XObject for '" + objectName.getName() + "'");
    } else if (xobject instanceof PDXObjectImage) {
        PDXObjectImage image = (PDXObjectImage) xobject;
        try {
            image.setGraphicsState(drawer.getGraphicsState());
            BufferedImage awtImage = image.getRGBImage();
            if (awtImage == null) {
                log.warn("getRGBImage returned NULL");
                return;//TODO PKOCH
            }
            int imageWidth = awtImage.getWidth();
            int imageHeight = awtImage.getHeight();
            double pageHeight = drawer.getPageSize().getHeight();

            log.debug("imageWidth: " + imageWidth + "\t\timageHeight: " + imageHeight);

            Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
            float yScaling = ctm.getYScale();
            float angle = (float) Math.acos(ctm.getValue(0, 0) / ctm.getXScale());
            if (ctm.getValue(0, 1) < 0 && ctm.getValue(1, 0) > 0)
                angle = (-1) * angle;
            ctm.setValue(2, 1, (float) (pageHeight - ctm.getYPosition() - Math.cos(angle) * yScaling));
            ctm.setValue(2, 0, (float) (ctm.getXPosition() - Math.sin(angle) * yScaling));
            // because of the moved 0,0-reference, we have to shear in the opposite direction
            ctm.setValue(0, 1, (-1) * ctm.getValue(0, 1));
            ctm.setValue(1, 0, (-1) * ctm.getValue(1, 0));
            AffineTransform ctmAT = ctm.createAffineTransform();
            ctmAT.scale(1f / imageWidth, 1f / imageHeight);
            drawer.drawImage(awtImage, ctmAT);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e, e);
        }
    } else if (xobject instanceof PDXObjectForm) {
        // save the graphics state
        context.getGraphicsStack().push((PDGraphicsState) context.getGraphicsState().clone());

        PDXObjectForm form = (PDXObjectForm) xobject;
        COSStream invoke = (COSStream) form.getCOSObject();
        PDResources pdResources = form.getResources();
        if (pdResources == null) {
            pdResources = page.findResources();
        }
        // if there is an optional form matrix, we have to
        // map the form space to the user space
        Matrix matrix = form.getMatrix();
        if (matrix != null) {
            Matrix xobjectCTM = matrix.multiply(context.getGraphicsState().getCurrentTransformationMatrix());
            context.getGraphicsState().setCurrentTransformationMatrix(xobjectCTM);
        }
        getContext().processSubStream(page, pdResources, invoke);

        // restore the graphics state
        context.setGraphicsState((PDGraphicsState) context.getGraphicsStack().pop());
    }
}

From source file:com.alvermont.terraj.planet.project.HeightfieldGenerator.java

/**
 * Generate the heightfield data//  ww  w.  j a  v  a 2  s  .  c  o m
 */
public void generate() {
    final int width = getParameters().getProjectionParameters().getWidth();

    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();

    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    heights = new int[width][height];

    final double sla = Math.sin(lat);
    final double cla = Math.cos(lat);
    final double slo = Math.sin(lon);
    final double clo = Math.cos(lon);

    cacheParameters();

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    double x;
    double y;
    double z;
    double x1;
    double y1;
    double z1;

    for (int j = 0; j < height; ++j) {
        for (int i = 0; i < width; ++i) {
            x = ((2.0 * i) - width) / height / scale;
            y = ((2.0 * j) - height) / height / scale;

            if (((x * x) + (y * y)) > 1.0) {
                heights[i][j] = 0;
            } else {
                z = Math.sqrt(1.0 - (x * x) - (y * y));

                x1 = (clo * x) + (slo * sla * y) + (slo * cla * z);
                y1 = (cla * y) - (sla * z);
                z1 = (-slo * x) + (clo * sla * y) + (clo * cla * z);

                final double heightValue = planet1(x1, y1, z1);

                heights[i][j] = (int) (10000000.0 * heightValue);
            }
        }
    }
}

From source file:Hexagon.java

public Hexagon(Point2D center, int size) {
    super();//w  ww .j av a  2s . c  om
    this.center = center;
    this.size = size;

    /**
      * MATH:
      *   With the hexagon points={TOP, UPPER-RIGHT, LOWER-RIGHT, BOTTOM, LOWER-LEFT, UPPER-RIGHT}
      *   size = length of each actual segment of the hexagon
      *   width = bounding rectangle width
      *   height = bounding rectangle height
      *   each inner angle is 120 degrees
      *   outside angles are 30-60-90 triangles with 30 near TOP and BOTTOM and 60 near sides
      *   hOffset = height difference between 'size' edge and bounding rectangle corners
      *   wOffset = width difference between TOP/BOTTOM points and bounding rectangle corners
      */

    double thirtyDegrees = Math.toRadians(30);
    hOffset = Math.sin(thirtyDegrees) * size;
    wOffset = Math.cos(thirtyDegrees) * size;

    height = (2 * hOffset) + size;
    width = (2 * wOffset);

    double left = center.getX() - (width / 2);
    double right = center.getX() + (width / 2);
    double top = center.getY() - (height / 2);
    double bottom = center.getY() + (height / 2);
    boundingBox = new Rectangle2D.Double(left, top, width, height);

    boundingCorners = new HashMap<BoundingCorner, Point2D>();
    boundingCorners.put(BoundingCorner.TopRight, new Point2D.Double(right, top));
    boundingCorners.put(BoundingCorner.TopLeft, new Point2D.Double(left, top));
    boundingCorners.put(BoundingCorner.BottomRight, new Point2D.Double(right, bottom));
    boundingCorners.put(BoundingCorner.BottomLeft, new Point2D.Double(left, bottom));

    corners = new HashMap<Corner, Point2D>();
    corners.put(Corner.Top, new Point2D.Double(center.getX(), top));
    corners.put(Corner.UpperRight, new Point2D.Double(right, (top + hOffset)));
    corners.put(Corner.LowerRight, new Point2D.Double(right, (bottom - hOffset)));
    corners.put(Corner.Bottom, new Point2D.Double(center.getX(), bottom));
    corners.put(Corner.LowerLeft, new Point2D.Double(left, (bottom - hOffset)));
    corners.put(Corner.UpperLeft, new Point2D.Double(left, (top + hOffset)));

    for (Corner corner : Corner.values()) {
        Point2D p2d = corners.get(corner);
        addPoint((int) p2d.getX(), (int) p2d.getY());
    }
}