Example usage for java.lang Math toRadians

List of usage examples for java.lang Math toRadians

Introduction

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

Prototype

public static double toRadians(double angdeg) 

Source Link

Document

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Usage

From source file:org.kalypsodeegree_impl.graphics.displayelements.RasterDisplayElement_Impl.java

private double calculateSlope(final int i, final int j, final double[][] values, final double xres,
        final double yres, final double z) {
    final double scale = 1.0;
    final double az = 315.0;
    final double alt = 75.0;

    if (i == 0 || i > values.length - 2)
        return Double.NaN;

    if (j == 0 || j > values[0].length - 2)
        return Double.NaN;

    // First Slope ...
    final double x = z * //
            (values[i - 1][j - 1] + values[i - 1][j] + values[i - 1][j] + values[i - 1][j + 1]
                    - values[i + 1][j - 1] - values[i + 1][j] - values[i + 1][j] - values[i + 1][j + 1]) //
            / (8.0 * xres * scale);//from  w  w  w .  j  a  v a 2 s.  c  o  m

    final double y = z * //
            (values[i - 1][j + 1] + values[i][j + 1] + values[i][j + 1] + values[i + 1][j + 1]
                    - values[i - 1][j - 1] - values[i][j - 1] - values[i][j - 1] - values[i + 1][j - 1]) //
            / (8.0 * yres * scale);

    final double slope = 90.0 - Math.toDegrees(Math.atan(Math.sqrt(x * x + y * y)));

    // ... then aspect...
    final double aspect = Math.atan2(x, y);

    // ... then the shade value
    final double cang = Math.sin(Math.toRadians(alt)) * Math.sin(Math.toRadians(slope)) + //
            Math.cos(Math.toRadians(alt)) * Math.cos(Math.toRadians(slope)) //
                    * Math.cos(Math.toRadians(az - 90.0) - aspect);

    if (cang <= 0.0)
        return 1.0;

    return cang;
}

From source file:Matrix.java

public static float[] matrixPerspective(float fovy, float aspect, float znear, float zfar) {
    // this code is adapted from Mesa :)
    float xmax, ymax;
    ymax = znear * (float) Math.tan(Math.toRadians(fovy / 2f));
    xmax = aspect * ymax;/* w w  w .j a  va2 s.c om*/
    return matrixFrustum(-xmax, xmax, -ymax, ymax, znear, zfar);
}

From source file:com.jillesvangurp.geo.GeoGeometry.java

/**
 * Compute the Haversine distance between the two coordinates. Haversine is
 * one of several distance calculation algorithms that exist. It is not very
 * precise in the sense that it assumes the earth is a perfect sphere, which
 * it is not. This means precision drops over larger distances. According to
 * http://en.wikipedia.org/wiki/Haversine_formula there is a 0.5% error
 * margin given the 1% difference in curvature between the equator and the
 * poles./*w w w.j a va 2s .  c  om*/
 *
 * @param lat1
 *            the latitude in decimal degrees
 * @param long1
 *            the longitude in decimal degrees
 * @param lat2
 *            the latitude in decimal degrees
 * @param long2
 *            the longitude in decimal degrees
 * @return the distance in meters
 */
public static double distance(final double lat1, final double long1, final double lat2, final double long2) {
    validate(lat1, long1, false);
    validate(lat2, long2, false);

    final double deltaLat = toRadians(lat2 - lat1);
    final double deltaLon = toRadians(long2 - long1);

    final double a = sin(deltaLat / 2) * sin(deltaLat / 2)
            + cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) * sin(deltaLon / 2) * sin(deltaLon / 2);

    final double c = 2 * asin(Math.sqrt(a));

    return EARTH_RADIUS * c;
}

From source file:org.kalypso.ogc.sensor.diagview.jfreechart.ObservationPlot.java

/**
 * Draw alarmlevels (horizontal line and text annotation)
 *///  ww w. ja  v  a  2  s.co  m
private void drawAlarmLevels(final Graphics2D g2, final Rectangle2D dataArea) {
    for (final Object element : m_yConsts.keySet()) {
        final AlarmLevelPlotElement vac = m_yConsts.get(element);

        final ValueAxis axis = m_diag2chartAxis.get(vac.getAxis());
        if (axis == null)
            continue;

        if (axis.getRange().contains(vac.getAlarm().value)) {
            final double yy = axis.valueToJava2D(vac.getAlarm().value, dataArea, RectangleEdge.LEFT);
            final Line2D line = new Line2D.Double(dataArea.getMinX(), yy, dataArea.getMaxX(), yy);
            // always set stroke, else we got the stroke from the last drawn line
            g2.setStroke(AlarmLevelPlotElement.STROKE_ALARM);
            g2.setPaint(vac.getAlarm().color);
            g2.draw(line);

            // and draw the text annotation: if annotation is outside (on top); label it below the line
            if (yy < dataArea.getMinY() + 20)
                vac.getAnnotation().setAngle(Math.toRadians(20));
            else
                vac.getAnnotation().setAngle(Math.toRadians(340));

            vac.getAnnotation().draw(g2, this, dataArea, getDomainAxis(), axis);
        }
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

private void rotate(int rotation, PDRectangle viewBox, AffineTransform atdoc) {
    float x = viewBox.getWidth() + viewBox.getLowerLeftX();
    float y = viewBox.getHeight() + viewBox.getLowerLeftY();
    switch (rotation) {
    case 90:/*from  www  .  ja v a2  s  .c  om*/
        atdoc.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
        atdoc.translate(0, viewBox.getWidth());
        atdoc.rotate(-Math.PI / 2.0);
        atdoc.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
        break;
    case 180:
        atdoc.translate(x, y);
        atdoc.rotate(-Math.PI);
        atdoc.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
        break;
    case 270:
        atdoc.translate(viewBox.getLowerLeftX(), y);
        atdoc.rotate(Math.toRadians(270 + 180));
        atdoc.translate(-x, -y);
        break;
    default:
        //no additional transformations necessary
        break;
    }
}

From source file:edu.csun.ecs.cs.multitouchj.application.touchpong.TouchPong.java

private boolean bounceBall(BounceableControl control) {
    /*// ww  w .  j  a  v a 2  s . c o  m
    log.debug("Cheking...");
    log.debug("\tcontrol: "+control.getPosition().toString()+", ball: "+ball.getPosition().toString());
    log.debug("\tvector: x="+ballUnitVector.getX()+", y="+ballUnitVector.getY());
    */

    boolean isHit = false;
    if ((control.isVisible()) && (control.isHit(ball.getPosition()))) {
        log.debug("bounced!!!!!!!!!!!!!!");

        float angleDelta = 90.0f - control.getRotation();

        // get current angle of ball vector
        Point previousBallPosition = new Point(0.0f, 0.0f);
        Point currentBallPosition = new Point(ballUnitVector.getX(), ballUnitVector.getY());
        //float ballVectorMagnitude = PointUtility.getDistance(previousBallPosition, currentBallPosition);
        float currentBallAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition);
        log.debug("\t\tcurrentBallAngle=" + currentBallAngle);

        // get new vector
        float targetBallAngle = (currentBallAngle + angleDelta);
        float newX = (float) Math.cos(Math.toRadians(targetBallAngle));
        float newY = (float) Math.sin(Math.toRadians(targetBallAngle));
        log.debug("\t\tnew vector: x=" + newX + ", y=" + newY);
        newX = (-1 * newX);

        // back to original angle
        previousBallPosition.set(0.0f, 0.0f);
        currentBallPosition.set(newX, newY);
        log.debug("\t\tnewX=" + newX + ", newY=" + newY);
        float newAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition);
        targetBallAngle = (newAngle - angleDelta);
        log.debug("\t\tnew angle=" + newAngle + ", targetBallAngle=" + targetBallAngle);

        newX = (float) Math.cos(Math.toRadians(targetBallAngle));
        newY = (float) Math.sin(Math.toRadians(targetBallAngle));
        ballUnitVector.set(newX, newY);
        ballUnitVector = ballUnitVector.normalise(null);
        log.debug("\t\tfinal: x=" + ballUnitVector.getX() + ", y=" + ballUnitVector.getY());

        isHit = true;
    }

    return isHit;
}

From source file:net.konyan.yangonbusonthemap.HomeActivity.java

public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
    double earthRadius = 6371000; //meters
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    float dist = (float) (earthRadius * c);

    return dist;//from   ww  w . j  a  v a 2 s  . c o m
}

From source file:ViewProj.java

public BranchGroup createVWorldViewSG() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();
    objRoot.setCapability(BranchGroup.ALLOW_DETACH);

    // setup a transform group to hold the scaled scene
    TransformGroup objTrans = new TransformGroup();
    objRoot.addChild(objTrans);/*  w ww .jav a2  s .c  o m*/

    // get the eye point, field of view and clip distances
    float fov = (float) view.getFieldOfView();

    // figure out the angle factors to find points along the edges
    // of the FOV
    // X = fovSpreadX * (Y - eyeVW.y) + eyeVW.x;
    float fovSpreadX = (float) Math.tan(fov / 2);
    // Z = fovSpreadZ * (X - eyeVW.x) + eyeVW.z;
    float fovSpreadZ = 1.0f / fovSpreadX;
    //System.out.println("fovSpreadX = " + fovSpreadX);
    //System.out.println("fovSpreadZ = " + fovSpreadZ);

    Transform3D vpTransform = new Transform3D();
    viewingPlatform.getViewPlatformTransform().getTransform(vpTransform);
    Vector3f vpTranslation = new Vector3f();
    vpTransform.get(vpTranslation);
    eyePtVW.set(vpTranslation);
    eyePtVW.negate();
    // get the eye point in our 2D coord system.
    Point3f eyePt = new Point3f(0.0f, eyePtVW.z, 0.1f);
    float frontClipDist = (float) view.getFrontClipDistance();
    float backClipDist = (float) view.getBackClipDistance();

    // set up the clip plane lines
    Point3f[] cpPoints = new Point3f[5];
    cpPoints[0] = new Point3f(frontClipDist * fovSpreadX, eyePtVW.z + frontClipDist, 0.1f);
    cpPoints[1] = new Point3f(cpPoints[0]);
    cpPoints[1].x *= -1;
    Point3f backLeft = new Point3f(-backClipDist * fovSpreadX, eyePtVW.z + backClipDist, 0.1f);
    cpPoints[2] = backLeft;
    Point3f backRight = new Point3f(backLeft);
    backRight.x *= -1;
    cpPoints[3] = backRight;
    cpPoints[4] = cpPoints[0];
    //for (int i = 0; i < 4; i++) {
    //    System.out.println("cpPoints[" + i + "] = " + cpPoints[i]);
    //}
    int[] cpLength = new int[1];
    cpLength[0] = 5;
    LineStripArray cpLines = new LineStripArray(5, LineArray.COORDINATES, cpLength);
    cpLines.setCoordinates(0, cpPoints);
    Appearance cpApp = new Appearance();
    ColoringAttributes cpCa = new ColoringAttributes(blue, ColoringAttributes.SHADE_FLAT);
    cpApp.setColoringAttributes(cpCa);
    Shape3D cpShape = new Shape3D(cpLines, cpApp);
    objTrans.addChild(cpShape);

    // get the limits of the space
    float minY = eyePt.y;
    float maxY = backLeft.y;
    float minX = backLeft.x;
    float maxX = backRight.x;

    // figure out the X and Y extents and offsets
    float deltaX = maxX - minX;
    float deltaY = maxY - minY;
    float offsetX = -(maxX + minX) / 2.0f;
    float offsetY = -(maxY + minY) / 2.0f;
    float gridSize = Math.max(deltaX, deltaY);

    // scale the grid slightly to give a border around the edge
    gridSize *= 1.1f;

    //System.out.println("offsetX = " + offsetX);
    //System.out.println("offsetY = " + offsetY);

    // Scale the view to fit -1 to 1
    Transform3D trans = new Transform3D();
    trans.set(new Vector3f(offsetX, offsetY, 0.0f), 2.0f / gridSize);
    objTrans.setTransform(trans);

    // figure out a grid step that is a multiple of 10 which keeps the
    // number of steps less than 30.
    float gridStep = 1.0f;
    while ((gridSize / gridStep) > 30.0) {
        gridStep *= 10;
    }
    int gridNumSteps = (int) Math.ceil(gridSize / gridStep) + 1;

    // allocate the grid points array, four points for each step (x and y)
    // with a couple extra points for the extra grid points added
    // below
    int gridNumPoints = 4 * (gridNumSteps + 4);
    Point3f[] gridPts = new Point3f[gridNumPoints];
    for (int i = 0; i < gridNumPoints; i++) {
        gridPts[i] = new Point3f();
    }

    // find the grid limits. Add a step on each side to make sure
    // the grid is larger than the view
    float gridMinY = gridStepFloor(minY, gridStep) - gridStep;
    float gridMaxY = gridStepCeil(maxY, gridStep) + gridStep;
    float gridMinX = gridStepFloor(minX, gridStep) - gridStep;
    float gridMaxX = gridStepCeil(maxX, gridStep) + gridStep;
    //System.out.println("gridMinY = " + gridMinY);
    //System.out.println("gridMaxY = " + gridMaxY);
    //System.out.println("gridMinX = " + gridMinX);
    //System.out.println("gridMaxX = " + gridMaxX);

    // set up the background grid
    Appearance bgApp = new Appearance();
    ColoringAttributes bgCa = new ColoringAttributes();
    bgCa.setColor(grey);
    LineAttributes bgLa = new LineAttributes();
    bgApp.setColoringAttributes(bgCa);

    // clear out the clip grid point list
    numClipGridPts = 0;

    // set up the vertical lines
    int numPts = 0;
    for (float x = gridMinX; x <= gridMaxX; x += gridStep) {
        gridPts[numPts].x = x;
        gridPts[numPts].y = gridMinY;
        gridPts[numPts].z = -0.2f;
        gridPts[numPts + 1].x = x;
        gridPts[numPts + 1].y = gridMaxY;
        gridPts[numPts + 1].z = -0.2f;
        numPts += 2;

        // try to add a line to the clipped grid
        // find the intersection of the clipped line with the FOV sides
        // this is a distance relative to the eye
        float clipZ = fovSpreadZ * Math.abs(x - eyePtVW.x);
        if (clipZ < frontClipDist) { // clip to front clip plane
            clipZ = frontClipDist;
        }
        if (clipZ < backClipDist) { // clip to back clip plane
            // line is not clipped
            clipGridPtsVW[numClipGridPts].x = x;
            clipGridPtsVW[numClipGridPts].y = clipZ + eyePtVW.z;
            clipGridPtsVW[numClipGridPts].z = -0.1f;
            clipGridPtsVW[numClipGridPts + 1].x = x;
            clipGridPtsVW[numClipGridPts + 1].y = backClipDist + eyePtVW.z;
            clipGridPtsVW[numClipGridPts + 1].z = -0.1f;
            numClipGridPts += 2;
        }
    }
    LineArray vertLa = new LineArray(numPts, LineArray.COORDINATES);
    vertLa.setCoordinates(0, gridPts, 0, numPts);
    Shape3D vertShape = new Shape3D(vertLa, bgApp);
    objTrans.addChild(vertShape);

    // set up the horizontal lines
    numPts = 0;
    for (float y = gridMinY; y <= gridMaxY; y += gridStep) {
        gridPts[numPts].x = gridMinX;
        gridPts[numPts].y = y;
        gridPts[numPts++].z = -0.2f;
        gridPts[numPts].x = gridMaxX;
        gridPts[numPts].y = y;
        gridPts[numPts++].z = -0.2f;

        // try to add a line to the clipped grid
        // find the intersection of the clipped line with the FOV sides
        // this is a distance relative to the eye
        float clipDist = (y - eyePtVW.z);
        if ((clipDist > frontClipDist) && (clipDist < backClipDist)) {

            float clipX = fovSpreadX * clipDist;
            clipGridPtsVW[numClipGridPts].x = -clipX;
            clipGridPtsVW[numClipGridPts].y = y;
            clipGridPtsVW[numClipGridPts].z = -0.1f;
            clipGridPtsVW[numClipGridPts + 1].x = clipX;
            clipGridPtsVW[numClipGridPts + 1].y = y;
            clipGridPtsVW[numClipGridPts + 1].z = -0.1f;
            numClipGridPts += 2;
        }
    }
    LineArray horizLa = new LineArray(numPts, LineArray.COORDINATES);
    horizLa.setCoordinates(0, gridPts, 0, numPts);
    Shape3D horizShape = new Shape3D(horizLa, bgApp);
    objTrans.addChild(horizShape);

    // draw the clipped grid.
    if (numClipGridPts > 0) {
        LineArray clipLa = new LineArray(numClipGridPts, LineArray.COORDINATES);
        clipLa.setCoordinates(0, clipGridPtsVW, 0, numClipGridPts);
        Appearance clipGridApp = new Appearance();
        ColoringAttributes clipCa = new ColoringAttributes(black, ColoringAttributes.SHADE_FLAT);
        clipGridApp.setColoringAttributes(clipCa);
        LineAttributes clipGridLa = new LineAttributes();
        Shape3D clipShape = new Shape3D(clipLa, clipGridApp);
        objTrans.addChild(clipShape);
    }

    // set up the coordinate system
    Appearance coordSysApp = new Appearance();
    LineAttributes coordSysLa = new LineAttributes();
    coordSysLa.setLineWidth(3.0f);
    coordSysApp.setLineAttributes(coordSysLa);
    ColoringAttributes coordSysCa = new ColoringAttributes(grey, ColoringAttributes.SHADE_FLAT);
    coordSysApp.setColoringAttributes(coordSysCa);
    Point3f[] coordSysPts = new Point3f[4];
    coordSysPts[0] = new Point3f(gridMinX, 0, -0.5f);
    coordSysPts[1] = new Point3f(gridMaxX, 0, -0.5f);
    coordSysPts[2] = new Point3f(0, gridMinY, -0.5f);
    coordSysPts[3] = new Point3f(0, gridMaxY, -0.5f);
    LineArray coordSysLines = new LineArray(4, LineArray.COORDINATES);
    coordSysLines.setCoordinates(0, coordSysPts);
    Shape3D coordSysShape = new Shape3D(coordSysLines, coordSysApp);
    objTrans.addChild(coordSysShape);

    // set up the circle
    Appearance circleApp = new Appearance();
    ColoringAttributes circleCa = new ColoringAttributes();
    circleCa.setColor(red);
    circleApp.setColoringAttributes(circleCa);
    PolygonAttributes pa = new PolygonAttributes();
    pa.setCullFace(PolygonAttributes.CULL_NONE);
    circleApp.setPolygonAttributes(pa);
    int step = 360 / (numCirclePts - 1);
    for (int deg = 0; deg < 360; deg += step) {
        double angle = Math.toRadians(deg);
        circlePtsVW[deg / 10].x = sphereRadius * (float) Math.sin(angle);
        circlePtsVW[deg / 10].y = sphereRadius * (float) Math.cos(angle);
        circlePtsVW[deg / 10].z = -0.3f;
    }
    circlePtsVW[numCirclePts - 1].set(circlePtsVW[0]);
    int[] lineStripLength = new int[1];
    lineStripLength[0] = numCirclePts;
    //LineStripArray circleLineStrip = new LineStripArray(numCirclePts,
    //        LineArray.COORDINATES, lineStripLength);
    TriangleFanArray circleLineStrip = new TriangleFanArray(numCirclePts, LineArray.COORDINATES,
            lineStripLength);
    circleLineStrip.setCoordinates(0, circlePtsVW);
    Shape3D circleShape = new Shape3D(circleLineStrip, circleApp);
    objTrans.addChild(circleShape);

    return objRoot;
}

From source file:uk.ac.diamond.scisoft.analysis.io.HDF5LoaderTest.java

@Test
public void testLoadingNexusDetector() throws ScanFileHolderException {
    String n = TestFileFolder + "../NexusDiffractionTest/336502.nxs";
    NexusHDF5Loader l = new NexusHDF5Loader();
    l.setFile(n);/*from   ww  w  .  j a va  2s .c  o m*/
    DataHolder dh = l.loadFile();
    Tree t = dh.getTree();
    DetectorProperties dp = NexusTreeUtils.parseDetector("/entry/instrument/detector", t, 0)[0];

    ILazyDataset ld = dh.getLazyDataset(0);
    assertArrayEquals(new int[] { 10, 195, 487 }, ld.getShape());

    System.err.println(dp);

    AxisAngle4d ad = new AxisAngle4d(-1, 0, 0, Math.toRadians(9 + 36.53819));

    Matrix4d mo = new Matrix4d();
    mo.setIdentity();
    mo.setColumn(3, 571 + 210, 200, 0, 1);
    Matrix4d m = new Matrix4d();
    m.set(ad);
    m.mul(mo);

    Vector3d fast = new Vector3d(0, -Math.sqrt(0.5), Math.sqrt(0.5));
    Vector3d slow = new Vector3d(1, 0, 0);
    m.transform(fast);
    m.transform(slow);

    Vector4d o4 = new Vector4d();
    o4.setW(1);
    m.transform(o4);
    Vector3d origin = new Vector3d(o4.x, o4.y, o4.z);
    Matrix3d ori = MatrixUtils.computeFSOrientation(fast, slow);
    ori.transpose();
    DetectorProperties edp = new DetectorProperties(origin, 195, 487, 0.172, 0.172, ori);
    Vector3d bv = new Vector3d(origin);
    bv.normalize();

    assertEquals(edp.getPx(), dp.getPy()); // XXX test file wrong!!!
    assertEquals(edp.getPy(), dp.getPx());
    assertEquals(edp.getStartX(), dp.getStartX());
    assertEquals(edp.getStartY(), dp.getStartY());
    assertTrue(MatrixUtils.isClose(edp.getBeamVector(), dp.getBeamVector(), 1e-8, 1e-8));
    assertTrue(MatrixUtils.isClose(edp.getHPxSize(), dp.getHPxSize(), 1e-8, 1e-8));
    assertTrue(MatrixUtils.isClose(edp.getVPxSize(), dp.getVPxSize(), 1e-8, 1e-8));
    assertTrue(MatrixUtils.isClose(edp.getOrigin(), dp.getOrigin(), 1e-8, 1e-8));
    assertTrue(MatrixUtils.isClose(edp.getOrientation(), dp.getOrientation(), 1e-8, 1e-8));
}

From source file:com.skumar.flexibleciruclarseekbar.CircularSeekBar.java

/**
 * Update thumb positon//ww w  . j av  a  2 s  .  co m
 */
private void updateThumbPosition() {
    int thumbAngle = (int) (mStartAngle + mProgressSweep + mRotation + 90);
    mThumbXPos = (int) (mArcRadius * Math.cos(Math.toRadians(thumbAngle)));
    mThumbYPos = (int) (mArcRadius * Math.sin(Math.toRadians(thumbAngle)));
}