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:com.bolatu.gezkoncsvlogger.GyroOrientation.GyroscopeOrientation.java

/**
 * Create an angle-axis vector, in this case a unit quaternion, from the
 * provided Euler angle's (presumably from SensorManager.getOrientation()).
 * // www  .  ja  va2  s.  c o m
 * Equation from
 * http://www.euclideanspace.com/maths/geometry/rotations/conversions
 * /eulerToQuaternion/
 * 
 * @param orientation
 */
private void getRotationVectorFromAccelMag() {
    // Assuming the angles are in radians.

    // getOrientation() values:
    // values[0]: azimuth, rotation around the Z axis.
    // values[1]: pitch, rotation around the X axis.
    // values[2]: roll, rotation around the Y axis.

    // Heading, Azimuth, Yaw
    double c1 = Math.cos(vOrientationAccelMag[0] / 2);
    double s1 = Math.sin(vOrientationAccelMag[0] / 2);

    // Pitch, Attitude
    // The equation assumes the pitch is pointed in the opposite direction
    // of the orientation vector provided by Android, so we invert it.
    double c2 = Math.cos(-vOrientationAccelMag[1] / 2);
    double s2 = Math.sin(-vOrientationAccelMag[1] / 2);

    // Roll, Bank
    double c3 = Math.cos(vOrientationAccelMag[2] / 2);
    double s3 = Math.sin(vOrientationAccelMag[2] / 2);

    double c1c2 = c1 * c2;
    double s1s2 = s1 * s2;

    double w = c1c2 * c3 - s1s2 * s3;
    double x = c1c2 * s3 + s1s2 * c3;
    double y = s1 * c2 * c3 + c1 * s2 * s3;
    double z = c1 * s2 * c3 - s1 * c2 * s3;

    // The quaternion in the equation does not share the same coordinate
    // system as the Android gyroscope quaternion we are using. We reorder
    // it here.

    // Android X (pitch) = Equation Z (pitch)
    // Android Y (roll) = Equation X (roll)
    // Android Z (azimuth) = Equation Y (azimuth)

    qGyroscope = new Quaternion(w, z, x, y);

}

From source file:chiliad.parser.pdf.extractor.vectorgraphics.operator.Invoke.java

/**
 * process : Do : Paint the specified XObject (section 4.7).
 *
 * @param operator The operator that is being executed.
 * @param arguments List/*from   w w w  . java  2 s.c  o  m*/
 * @throws IOException If there is an error invoking the sub object.
 */
@Override
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    VectorGraphicsExtractor extractor = (VectorGraphicsExtractor) context;

    PDPage page = extractor.getPage();
    COSName objectName = (COSName) arguments.get(0);
    Map<String, PDXObject> xobjects = extractor.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 {
            if (image.getImageMask()) {
                // set the current non stroking colorstate, so that it can
                // be used to create a stencil masked image
                image.setStencilColor(extractor.getGraphicsState().getNonStrokingColor());
            }
            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 = extractor.getPageSize().getHeight();

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

            Matrix ctm = extractor.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);
            extractor.drawImage(awtImage, ctmAT);
        } catch (Exception e) {
            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 formContentstream = form.getCOSStream();
        // find some optional resources, instead of using the current resources
        PDResources pdResources = form.getResources();
        // 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);
        }
        if (form.getBBox() != null) {
            PDGraphicsState graphicsState = context.getGraphicsState();
            PDRectangle bBox = form.getBBox();

            float x1 = bBox.getLowerLeftX();
            float y1 = bBox.getLowerLeftY();
            float x2 = bBox.getUpperRightX();
            float y2 = bBox.getUpperRightY();

            Point2D p0 = extractor.transformedPoint(x1, y1);
            Point2D p1 = extractor.transformedPoint(x2, y1);
            Point2D p2 = extractor.transformedPoint(x2, y2);
            Point2D p3 = extractor.transformedPoint(x1, y2);

            GeneralPath bboxPath = new GeneralPath();
            bboxPath.moveTo((float) p0.getX(), (float) p0.getY());
            bboxPath.lineTo((float) p1.getX(), (float) p1.getY());
            bboxPath.lineTo((float) p2.getX(), (float) p2.getY());
            bboxPath.lineTo((float) p3.getX(), (float) p3.getY());
            bboxPath.closePath();

            Area resultClippingArea = new Area(graphicsState.getCurrentClippingPath());
            Area newArea = new Area(bboxPath);
            resultClippingArea.intersect(newArea);

            graphicsState.setCurrentClippingPath(resultClippingArea);
        }
        getContext().processSubStream(page, pdResources, formContentstream);

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

From source file:StatFunctions.java

public static double qt(double p, double ndf, boolean lower_tail) {
    // Algorithm 396: Student's t-quantiles by
    // G.W. Hill CACM 13(10), 619-620, October 1970
    if (p <= 0 || p >= 1 || ndf < 1)
        throw new IllegalArgumentException("Invalid p or df in call to qt(double,double,boolean).");
    double eps = 1e-12;
    double M_PI_2 = 1.570796326794896619231321691640; // pi/2
    boolean neg;/*w  ww.  java  2s . com*/
    double P, q, prob, a, b, c, d, y, x;
    if ((lower_tail && p > 0.5) || (!lower_tail && p < 0.5)) {
        neg = false;
        P = 2 * (lower_tail ? (1 - p) : p);
    } else {
        neg = true;
        P = 2 * (lower_tail ? p : (1 - p));
    }

    if (Math.abs(ndf - 2) < eps) { /* df ~= 2 */
        q = Math.sqrt(2 / (P * (2 - P)) - 2);
    } else if (ndf < 1 + eps) { /* df ~= 1 */
        prob = P * M_PI_2;
        q = Math.cos(prob) / Math.sin(prob);
    } else { /*-- usual case;  including, e.g.,  df = 1.1 */
        a = 1 / (ndf - 0.5);
        b = 48 / (a * a);
        c = ((20700 * a / b - 98) * a - 16) * a + 96.36;
        d = ((94.5 / (b + c) - 3) / b + 1) * Math.sqrt(a * M_PI_2) * ndf;
        y = Math.pow(d * P, 2 / ndf);
        if (y > 0.05 + a) {
            /* Asymptotic inverse expansion about normal */
            x = qnorm(0.5 * P, false);
            y = x * x;
            if (ndf < 5)
                c += 0.3 * (ndf - 4.5) * (x + 0.6);
            c = (((0.05 * d * x - 5) * x - 7) * x - 2) * x + b + c;
            y = (((((0.4 * y + 6.3) * y + 36) * y + 94.5) / c - y - 3) / b + 1) * x;
            y = a * y * y;
            if (y > 0.002)/* FIXME: This cutoff is machine-precision dependent */
                y = Math.exp(y) - 1;
            else { /* Taylor of e^y -1 : */
                y = (0.5 * y + 1) * y;
            }
        } else {
            y = ((1 / (((ndf + 6) / (ndf * y) - 0.089 * d - 0.822) * (ndf + 2) * 3) + 0.5 / (ndf + 4)) * y - 1)
                    * (ndf + 1) / (ndf + 2) + 1 / y;
        }
        q = Math.sqrt(ndf * y);
    }
    if (neg)
        q = -q;
    return q;
}

From source file:magma.agent.worldmodel.impl.VisibleObjectTest.java

/**
 * Test method for//  www.  j av a 2 s.  c  o  m
 * {@link magma.agent.worldmodel.impl.VisibleObject#update(magma.agent.perception.impl.VisibleObjectPerceptor, float, IThisPlayer)}
 * .
 */
@Test
public void testUpdateRotation45() {
    VisibleObjectPerceptor vision = new VisibleObjectPerceptor("test", new Vector3D(2.0, 2.0, 0.0));
    thisPlayer.setPosition(new Vector3D(0.0, 0.0, 0.0));
    thisPlayer.setHorizontalAngle(Angle.deg(-45.0));
    double x = 2.0 / Math.cos(Math.toRadians(-45.0));

    testee.update(vision, 1.0f, thisPlayer);
    Vector3D position = testee.getPosition();
    testVector(new Vector3D(x, 0.0, 0.0), position);
}

From source file:ch.algotrader.option.SABR.java

private static double getSmallestRoot(double cubic, double quadratic, double linear, double constant) {

    double a = quadratic / cubic;
    double b = linear / cubic;
    double C = constant / cubic;
    double Q = (Math.pow(a, 2) - 3 * b) / 9.0;
    double r = (2 * Math.pow(a, 3) - 9 * a * b + 27 * C) / 54.0;

    double root = 0;

    if (Math.pow(r, 2) - Math.pow(Q, 3) >= 0) {

        double capA = -Math.signum(r)
                * Math.pow(Math.abs(r) + Math.sqrt(Math.pow(r, 2) - Math.pow(Q, 3)), 1.0 / 3.0);
        double capB = 0;
        if (capA != 0) {
            capB = Q / capA;//from w ww . j  a  va  2  s. c  om
        }
        root = capA + capB - a / 3.0;

    } else {

        double theta = Math.acos(r / Math.pow(Q, 1.5));
        double root1 = -2 * Math.sqrt(Q) * Math.cos(theta / 3.0) - a / 3.0;
        double root2 = -2 * Math.sqrt(Q) * Math.cos(theta / 3.0 + 2.0943951023932) - a / 3.0;
        double root3 = -2 * Math.sqrt(Q) * Math.cos(theta / 3.0 - 2.0943951023932) - a / 3.0;

        // find the smallest positive one
        if (root1 > 0) {
            root = root1;
        } else if (root2 > 0) {
            root = root2;
        } else if (root3 > 0) {
            root = root3;
        }

        if (root2 > 0 && root2 < root) {
            root = root2;
        }
        if (root3 > 0 && root3 < root) {
            root = root3;
        }
    }

    return root;
}

From source file:dz.alkhwarizmix.winrak.java.model.vo.WinrakPosition.java

/**
 * http://www.movable-type.co.uk/scripts/latlong.html
 *//*from   ww w  .  j a  va  2s . co m*/
@Override
public int distanceTo(final IWinrakPosition pos2) {
    final WinrakPosition pos1 = this;
    final Double R = 6371000.0; // metres
    final Double radLat1 = Math.toRadians(pos1.getLat());
    final Double radLat2 = Math.toRadians(pos2.getLat());
    final Double deltaLat = Math.toRadians(pos2.getLat() - pos1.getLat());
    final Double deltaLng = Math.toRadians(pos2.getLng() - pos1.getLng());
    final Double a = (Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2))
            + (Math.cos(radLat1) * Math.cos(radLat2) * Math.sin(deltaLng / 2) * Math.sin(deltaLng / 2));
    final Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    final Double d = R * c;
    return (d.intValue());
}

From source file:com.github.mfpdev.marketing.StoreCategorySegmentResolverResource.java

private double distance(double lon1, double lat1, double lon2, double lat2) {
    double dLat = Math.toRadians(lat2 - lat1); // Javascript functions in radians
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return EARTH_RADIUS * c; // Distance in km
}

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

/**
 * Carry out the projection/*w ww . java 2 s . c  o m*/
 */
public void project() {
    setcolours();

    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();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

    double y;
    double scale1;
    double cos2;
    double theta1;
    int i;
    int j;
    int k;

    y = Math.sin(lat);
    y = (1.0 + y) / (1.0 - y);
    y = 0.5 * Math.log(y);
    k = (int) ((0.5 * y * width * scale) / Math.PI);

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        //            if (debug && ((j % (Height/25)) == 0))
        //            {fprintf(stderr, "%c", view); fflush(stderr);}
        progress.progressStep(j);

        y = (Math.PI * ((2.0 * (j - k)) - height)) / width / scale;
        y = Math.exp(2. * y);
        y = (y - 1.) / (y + 1.);

        scale1 = (scale * width) / height / Math.sqrt(1.0 - (y * y)) / Math.PI;

        cos2 = Math.sqrt(1.0 - (y * y));
        depth = (3 * ((int) (log2(scale1 * height)))) + 3;

        for (i = 0; i < width; ++i) {
            theta1 = lon - (0.5 * Math.PI) + ((Math.PI * ((2.0 * i) - width)) / width / scale);
            colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, y, -Math.sin(theta1) * cos2);

            if (doShade) {
                shades[i][j] = shade;
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = Math.sin(Math.toRadians(theta1));
            y = (1.0 + y) / (1.0 - y);
            y = 0.5 * Math.log(y);
            j = (height / 2) + (int) ((0.5 * y * width * scale) / Math.PI) + k;

            if ((j >= 0) && (j < height)) {
                for (i = 0; i < width; ++i)
                    colours[i][j] = BLACK;
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta1 = 0.0; theta1 > -360.0; theta1 -= vgrid)
            ;

        for (theta1 = theta1; theta1 < 360.0; theta1 += vgrid) {
            i = (int) (0.5 * width * (1.0 + ((scale * (Math.toRadians(theta1) - lon)) / Math.PI)));

            if ((i >= 0) && (i < width)) {
                for (j = 0; j < height; ++j)
                    colours[i][j] = BLACK;
            }
        }
    }

    if (doShade) {
        smoothshades();
    }
}

From source file:ActualizadorLocal.Clientes.ClienteNodos.java

public void procesarDatos(String datos) throws SQLException {
    //Preprocesamos los datos, para darle un nombre al array:

    datos = "{\"nodos\":" + datos + "}";

    JSONParser parser = new JSONParser();

    try {//from   ww w  .  j a va 2s .  co m
        JSONObject obj = (JSONObject) parser.parse(datos);
        JSONArray lista = (JSONArray) obj.get("nodos");

        for (int i = 0; i < lista.size(); i++) {
            long a0 = (long) ((JSONObject) lista.get(i)).get("idNodo");
            String a1 = (String) ((JSONObject) lista.get(i)).get("nombre");
            double a2 = (double) ((JSONObject) lista.get(i)).get("latitud");
            double a3 = (double) ((JSONObject) lista.get(i)).get("longitud");

            nodos.add(a0);

            //Tenemos que calcular el polgono para la visualizacin de los datos mediante GOOGLE FUSION TABLES:

            double lat = Math.toRadians(new Double(a2));
            double lon = Math.toRadians(new Double(a3));

            double radio = 50.0 / 6378137.0;

            String cadena_poligono = "<Polygon><outerBoundaryIs><LinearRing><coordinates>";

            for (int j = 0; j <= 360; j = j + 15) {
                double r = Math.toRadians(j);
                double lat_rad = Math
                        .asin(Math.sin(lat) * Math.cos(radio) + Math.cos(lat) * Math.sin(radio) * Math.cos(r));
                double lon_rad = Math.atan2(Math.sin(r) * Math.sin(radio) * Math.cos(lat),
                        Math.cos(radio) - Math.sin(lat) * Math.sin(lat_rad));
                double lon_rad_f = ((lon + lon_rad + Math.PI) % (2 * Math.PI)) - Math.PI;

                cadena_poligono = cadena_poligono + Math.toDegrees(lon_rad_f) + "," + Math.toDegrees(lat_rad)
                        + ",0.0 ";
            }

            cadena_poligono = cadena_poligono + "</coordinates></LinearRing></outerBoundaryIs></Polygon>";

            this.InsertarDatos("\"" + (String) Long.toString(a0) + "\",\"" + a1 + "\",\"" + Double.toString(a2)
                    + "\",\"" + Double.toString(a3) + "\",\"" + cadena_poligono + "\"");

        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

    syncDB();

}

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

/**
 * Carry out the projection/*from   w w  w .j a v a 2 s. c  o m*/
 */
public void project() {
    setcolours();

    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();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

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

    double y;
    double cos2;
    double theta1;
    double scale1;
    int k;
    int i;
    int j;
    int water;
    int land;

    y = 2.0 * Math.sin(lat);
    k = (int) ((0.5 * y * width * scale) / Math.PI);

    water = 0;
    land = 0;

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        progress.progressStep(j);

        y = (0.5 * Math.PI * ((2.0 * (j - k)) - height)) / width / scale;

        if (Math.abs(y) > 1.0) {
            for (i = 0; i < width; ++i) {
                colours[i][j] = backgroundColour;

                if (doShade) {
                    shades[i][j] = 255;
                }
            }
        } else {
            cos2 = Math.sqrt(1.0 - (y * y));

            if (cos2 > 0.0) {
                scale1 = (scale * width) / height / cos2 / Math.PI;

                depth = (3 * ((int) (log2(scale1 * height)))) + 3;

                for (i = 0; i < width; ++i) {
                    theta1 = lon - (0.5 * Math.PI) + ((Math.PI * ((2.0 * i) - width)) / width / scale);

                    colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, y, -Math.sin(theta1) * cos2);

                    if (doShade) {
                        shades[i][j] = shade;
                    }

                    if (colours[i][j] < colourLand0) {
                        ++water;
                    } else {
                        ++land;
                    }
                }
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    log.debug("Water percentage: " + ((100 * water) / (water + land)));

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = 2.0 * Math.sin(Math.toRadians(theta1));
            j = (height / 2) + (int) ((0.5 * y * width * scale) / Math.PI) + k;

            if ((j >= 0) && (j < height)) {
                for (i = 0; i < width; ++i)
                    colours[i][j] = BLACK;
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta1 = 0.0; theta1 > -360.0; theta1 -= vgrid)
            ;

        for (theta1 = theta1; theta1 < 360.0; theta1 += vgrid) {
            i = (int) (0.5 * width * (1.0 + ((scale * (Math.toRadians(theta1) - lon)) / Math.PI)));

            if ((i >= 0) && (i < width)) {
                for (j = Math.max(0, (height / 2) - (int) ((width * scale) / Math.PI) + k); j < Math.min(height,
                        (height / 2) + (int) ((width * scale) / Math.PI) + k); ++j)
                    colours[i][j] = BLACK;
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}