Example usage for java.lang Math toDegrees

List of usage examples for java.lang Math toDegrees

Introduction

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

Prototype

public static double toDegrees(double angrad) 

Source Link

Document

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

Usage

From source file:spinworld.gui.RadarPlot.java

private void drawTickLabel(Graphics2D g2, Rectangle2D radarArea, Point2D middlePoint, double _axisAngle,
        int cat, double tick) {
    double axisAngle = normalize(_axisAngle);
    double _origin = getOrigin(cat).doubleValue();
    double max = getMaxValue(cat).doubleValue();
    double tickValue = ((max - _origin) * tick) + _origin;
    String label = "" + Math.round(tickValue * 1000) / 1000d;
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    int labelW = (int) labelBounds.getWidth();
    int labelH = (int) labelBounds.getHeight();

    double centerX = radarArea.getCenterX();
    double centerY = radarArea.getCenterY();

    double adj = middlePoint.distance(centerX, centerY);
    double opp = TICK_MARK_LENGTH / 2 + 4;
    double hyp = Math.sqrt(Math.pow(opp, 2) + Math.pow(adj, 2));
    double angle = Math.toDegrees(Math.atan(opp / adj));
    int charHeight = g2.getFontMetrics().getHeight();
    int charWidth = g2.getFontMetrics().charWidth('M');

    double alphaRad = Math.toRadians(axisAngle - angle);
    double labelX = centerX + (hyp * Math.cos(alphaRad));
    double labelY = centerY - (hyp * Math.sin(alphaRad)) + labelH;

    //        g2.draw(new Line2D.Double(centerX, centerY, labelX, labelY - labelH)); // test line

    double sinGap = Math.pow(Math.sin(Math.toRadians(axisAngle)), 2);
    if (axisAngle > 90 && axisAngle < 270) {
        labelY -= labelH;//from w  w  w.j a  v a2s . c  om
        labelY += (charHeight * sinGap / 2);
    } else {
        labelY -= (charHeight * sinGap / 2);
    }
    double cosGap = Math.pow(Math.cos(Math.toRadians(axisAngle)), 2);
    if (axisAngle > 180) {
        labelX -= labelW;
        labelX += (charWidth * cosGap / 2);
    } else {
        labelX -= (charWidth * cosGap / 2);
    }
    //        g2.drawRect((int) labelX, (int) labelY - labelH, labelW, labelH); // test rectangle
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelX, (float) labelY);
}

From source file:civilisation.individu.Humain.java

/**
 * Return the angle towards the specified patch
 *//*www . j a v  a2s. co m*/
public double towards(Patch p) {
    double dx = p.x - xcor();
    double dy = -(p.y - ycor());
    double inRads = Math.atan2(dy, dx);

    if (inRads < 0)
        inRads = Math.abs(inRads);
    else
        inRads = 2 * Math.PI - inRads;

    return Math.toDegrees(inRads);
}

From source file:civilisation.individu.Humain.java

/**
 * Return the angle towards the specified patch coordinates
 *//* w w  w . jav  a2 s  .c o m*/
public double towards(int x, int y) {
    double dx = x - xcor();
    double dy = -(y - ycor());
    double inRads = Math.atan2(dy, dx);

    if (inRads < 0)
        inRads = Math.abs(inRads);
    else
        inRads = 2 * Math.PI - inRads;

    return Math.toDegrees(inRads);
}

From source file:io.doist.datetimepicker.time.RadialTimePickerView.java

private int getDegreesFromXY(float x, float y) {
    final double hypotenuse = Math.sqrt((y - mYCenter) * (y - mYCenter) + (x - mXCenter) * (x - mXCenter));

    // Basic check if we're outside the range of the disk
    if (hypotenuse > mCircleRadius[HOURS]) {
        return -1;
    }//w w w . ja  v a2  s .co  m
    // Check
    if (mIs24HourMode && mShowHours) {
        if (hypotenuse >= mMinHypotenuseForInnerNumber && hypotenuse <= mHalfwayHypotenusePoint) {
            mIsOnInnerCircle = true;
        } else if (hypotenuse <= mMaxHypotenuseForOuterNumber && hypotenuse >= mHalfwayHypotenusePoint) {
            mIsOnInnerCircle = false;
        } else {
            return -1;
        }
    } else {
        final int index = (mShowHours) ? HOURS : MINUTES;
        final float length = (mCircleRadius[index] * mNumbersRadiusMultiplier[index]);
        final int distanceToNumber = (int) Math.abs(hypotenuse - length);
        final int maxAllowedDistance = (int) (mCircleRadius[index] * (1 - mNumbersRadiusMultiplier[index]));
        if (distanceToNumber > maxAllowedDistance) {
            return -1;
        }
    }

    final float opposite = Math.abs(y - mYCenter);
    int degrees = (int) (Math.toDegrees(Math.asin(opposite / hypotenuse)) + 0.5);

    // Now we have to translate to the correct quadrant.
    final boolean rightSide = (x > mXCenter);
    final boolean topSide = (y < mYCenter);
    if (rightSide) {
        if (topSide) {
            degrees = 90 - degrees;
        } else {
            degrees = 90 + degrees;
        }
    } else {
        if (topSide) {
            degrees = 270 + degrees;
        } else {
            degrees = 270 - degrees;
        }
    }
    return degrees;
}

From source file:com.example.SmartBoard.DrawingView.java

public double gradient(int startX, int startY, int stopX, int stopY) {

    double dy = stopY - startY;
    double dx = stopX - startX;
    System.out.println("Gradient of temp line: " + Math.round((dx / dy) * 100.0) / 100.0);
    double slopeAngle = Math.toDegrees(Math.tan(Math.round((dx / dy) * 100.0) / 100.0));

    System.out.println("Degrees:" + slopeAngle);
    return Math.round((dx / dy) * 100.0) / 100.0;

}

From source file:com.planetmayo.debrief.satc_rcp.views.MaintainContributionsView.java

protected void redoOwnshipStates() {
    if (legPlot == null)
        return;//from  w w  w .  ja v  a2 s  . c  om

    boolean showCourses = true;
    if (showOSCourse != null)
        showCourses = showOSCourse.getSelection();

    java.awt.Color courseCol = java.awt.Color.blue.darker().darker();
    java.awt.Color speedCol = java.awt.Color.blue.brighter().brighter();

    // ok, now loop through and set them
    long startTime = Long.MAX_VALUE;
    long endTime = Long.MIN_VALUE;

    // clear any datasets
    legPlot.setDataset(0, null);
    legPlot.setDataset(1, null);

    // hmm, actually we have to remove any target leg markers
    @SuppressWarnings("unchecked")
    Collection<IntervalMarker> markers = legPlot.getDomainMarkers(Layer.BACKGROUND);
    if (markers != null) {
        ArrayList<IntervalMarker> markersToDelete = new ArrayList<IntervalMarker>(markers);
        Iterator<IntervalMarker> mIter = markersToDelete.iterator();
        while (mIter.hasNext()) {
            IntervalMarker im = mIter.next();
            legPlot.removeDomainMarker(im);
        }
    }

    // hey, does it have any ownship legs?
    TimeSeriesCollection tscC = new TimeSeriesCollection();
    TimeSeriesCollection tscS = new TimeSeriesCollection();
    TimeSeriesCollection tscCLegs = new TimeSeriesCollection();
    TimeSeriesCollection tscSLegs = new TimeSeriesCollection();
    TimeSeries courses = new TimeSeries("Course");
    TimeSeries bearings = new TimeSeries("Bearings");
    TimeSeries speeds = new TimeSeries("Speed");
    TimeSeries courseLegs = new TimeSeries("Course (leg)");
    TimeSeries speedLegs = new TimeSeries("Speed (leg)");

    Iterator<BaseContribution> conts = activeSolver.getContributions().iterator();
    while (conts.hasNext()) {
        BaseContribution baseC = conts.next();
        if (baseC.isActive())
            if (baseC instanceof BearingMeasurementContribution) {
                BearingMeasurementContribution bmc = (BearingMeasurementContribution) baseC;

                Iterator<LegOfData> lIter = null;
                LegOfData thisLeg = null;

                if (bmc.getOwnshipLegs() != null) {
                    lIter = bmc.getOwnshipLegs().iterator();
                    thisLeg = lIter.next();
                }

                List<HostState> hostStates = bmc.getHostState();
                if (hostStates != null) {
                    Iterator<HostState> stateIter = hostStates.iterator();
                    while (stateIter.hasNext()) {
                        BearingMeasurementContribution.HostState hostState = stateIter.next();
                        long thisTime = hostState.time;
                        double thisCourse = hostState.courseDegs;
                        if (showCourses)
                            courses.add(new FixedMillisecond(thisTime), thisCourse);
                        double thisSpeed = hostState.speedKts;
                        speeds.add(new FixedMillisecond(thisTime), thisSpeed);
                        startTime = Math.min(thisTime, startTime);
                        endTime = Math.max(thisTime, endTime);

                        // sort out if this is in a leg or not
                        if (thisLeg != null) {
                            if (thisTime > thisLeg.getEnd() && lIter.hasNext()) {
                                thisLeg = lIter.next();
                            } else {
                                if (thisTime >= thisLeg.getStart()) {
                                    speedLegs.add(new FixedMillisecond(thisTime), thisSpeed);
                                    if (showCourses)
                                        courseLegs.add(new FixedMillisecond(thisTime), thisCourse);
                                }
                            }
                        }
                    }
                }

                // also, we wish to show the bearings from the BMC
                Iterator<BMeasurement> cuts = bmc.getMeasurements().iterator();
                while (cuts.hasNext()) {
                    BearingMeasurementContribution.BMeasurement measurement = cuts.next();
                    if (measurement.isActive()) {
                        long thisT = measurement.getDate().getTime();
                        bearings.add(new FixedMillisecond(thisT),
                                Math.toDegrees(Math.abs(measurement.getBearingRads())));
                    }
                }

            }
    }

    // HEY, also shade the ownship legs
    conts = activeSolver.getContributions().iterator();
    while (conts.hasNext()) {
        BaseContribution baseC = conts.next();
        if (baseC.isActive()) {
            if (baseC instanceof BearingMeasurementContribution) {
                BearingMeasurementContribution bmc = (BearingMeasurementContribution) baseC;

                Iterator<LegOfData> lIter = null;
                if (bmc.getOwnshipLegs() != null) {
                    int ctr = 1;
                    lIter = bmc.getOwnshipLegs().iterator();
                    while (lIter.hasNext()) {
                        LegOfData thisL = lIter.next();
                        long thisStart = thisL.getStart();
                        long thisFinish = thisL.getEnd();

                        java.awt.Color transCol = new java.awt.Color(0, 0, 255, 12);

                        final Marker bst = new IntervalMarker(thisStart, thisFinish, transCol,
                                new BasicStroke(2.0f), null, null, 1.0f);
                        bst.setLabel("O/S-" + ctr++);
                        bst.setLabelAnchor(RectangleAnchor.TOP_LEFT);
                        bst.setLabelFont(new Font("SansSerif", Font.ITALIC + Font.BOLD, 10));
                        bst.setLabelTextAnchor(TextAnchor.TOP_LEFT);
                        legPlot.addDomainMarker(bst, Layer.BACKGROUND);
                    }
                }
            }
        }
    }

    tscS.addSeries(speeds);
    tscSLegs.addSeries(speedLegs);
    tscC.addSeries(bearings);

    if (showCourses) {
        tscC.addSeries(courses);
        tscCLegs.addSeries(courseLegs);
    }

    legPlot.setDataset(0, null);
    legPlot.setDataset(1, null);
    legPlot.setDataset(2, null);
    legPlot.setDataset(3, null);
    legPlot.setDataset(0, tscC);
    legPlot.setDataset(1, tscS);
    legPlot.setDataset(2, tscCLegs);
    legPlot.setDataset(3, tscSLegs);

    final NumberAxis axis2 = new NumberAxis("Speed (Kts)");
    legPlot.setRangeAxis(1, axis2);
    legPlot.mapDatasetToRangeAxis(1, 1);
    legPlot.mapDatasetToRangeAxis(3, 1);

    legPlot.getRangeAxis(0).setLabel("Crse/Brg (Degs)");
    legPlot.mapDatasetToRangeAxis(0, 0);
    legPlot.mapDatasetToRangeAxis(2, 0);

    final XYLineAndShapeRenderer lineRenderer1 = new XYLineAndShapeRenderer(true, true);
    lineRenderer1.setSeriesPaint(1, courseCol);
    lineRenderer1.setSeriesShape(1, ShapeUtilities.createDiamond(0.1f));
    lineRenderer1.setSeriesPaint(0, java.awt.Color.RED);
    lineRenderer1.setSeriesShape(0, ShapeUtilities.createDiamond(2f));

    final XYLineAndShapeRenderer lineRenderer2 = new XYLineAndShapeRenderer(true, false);
    lineRenderer2.setSeriesPaint(0, speedCol);

    final XYLineAndShapeRenderer lineRenderer3 = new XYLineAndShapeRenderer(false, true);
    lineRenderer3.setSeriesPaint(0, courseCol);
    lineRenderer3.setSeriesShape(0, ShapeUtilities.createUpTriangle(2f));

    final XYLineAndShapeRenderer lineRenderer4 = new XYLineAndShapeRenderer(false, true);
    lineRenderer4.setSeriesPaint(0, speedCol);
    lineRenderer4.setSeriesShape(0, ShapeUtilities.createDownTriangle(2f));

    // ok, and store them
    legPlot.setRenderer(0, lineRenderer1);
    legPlot.setRenderer(1, lineRenderer2);
    legPlot.setRenderer(2, lineRenderer3);
    legPlot.setRenderer(3, lineRenderer4);

    if (startTime != Long.MAX_VALUE)
        legPlot.getDomainAxis().setRange(startTime, endTime);

    // ok - get the straight legs to sort themselves out
    // redoStraightLegs();
}

From source file:jp.furplag.util.commons.NumberUtilsTest.java

/**
 * {@link jp.furplag.util.commons.NumberUtils#toDegrees(java.lang.Number)}.
 *///ww  w.  j  a va 2 s.  c o m
@Test
public void testToDegrees() {
    assertEquals("null", (Object) Math.toDegrees(0d), toDegrees(null));
    for (Class<?> type : NUMBERS) {
        short s = -1080;
        while (s <= 1080) {
            assertEquals("deg: " + s + ": " + type.getSimpleName(), (Object) Math.toDegrees(Math.toRadians(s)),
                    toDegrees(toRadians(s)));
            s++;
        }
    }
}

From source file:com.planetmayo.debrief.satc_rcp.views.MaintainContributionsView.java

/**
 * copy the SATC scenario to the clipboard
 * //from ww w  .jav a2  s  .  c  o m
 */
protected void exportSATC() {
    // - ok, really we just export the state & bearing data
    if (activeSolver != null) {
        StringBuffer res = new StringBuffer();
        final String newLine = System.getProperty("line.separator");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        @SuppressWarnings("deprecation")
        Date dateLead = new Date(100, 7, 7);

        Iterator<BaseContribution> conts = activeSolver.getContributions().iterator();
        while (conts.hasNext()) {
            BaseContribution baseC = conts.next();
            if (baseC instanceof BearingMeasurementContribution) {
                BearingMeasurementContribution bmc = (BearingMeasurementContribution) baseC;

                // ok - sort out the date offset
                Date startDate = bmc.getStartDate();
                long offset = startDate.getTime() - dateLead.getTime();

                // get ready for the offset
                Point2D origin = null;

                // get ready to calculate offsetes
                GeodeticCalculator calc = GeoSupport.createCalculator();

                // ok, first the states
                res.append("//X, Y, Time, Course Degs, Speed Kts" + newLine);
                Iterator<HostState> states = bmc.getHostState().iterator();
                while (states.hasNext()) {
                    BearingMeasurementContribution.HostState hostState = states.next();

                    // sort out the X,Y offset
                    double x, y;
                    if (origin == null) {
                        x = 0;
                        y = 0;
                        origin = new Point2D.Double(hostState.dLong, hostState.dLat);
                    } else {
                        // ok, calc a new XY, from the origin
                        java.awt.geom.Point2D.Double thisP = new Point2D.Double(hostState.dLong,
                                hostState.dLat);
                        calc.setStartingGeographicPoint(origin);
                        calc.setDestinationGeographicPoint(thisP);
                        double angle = calc.getAzimuth();
                        double dist = calc.getOrthodromicDistance();

                        // and the new x,y coords
                        x = Math.sin(Math.toRadians(angle)) * dist;
                        y = Math.cos(Math.toRadians(angle)) * dist;

                    }

                    res.append(x + ", " + y + ", " + sdf.format(new Date(hostState.time - offset)) + ","
                            + hostState.courseDegs + "," + hostState.speedKts + newLine);
                }

                // now the cuts
                res.append("//Time, Bearing Degs" + newLine);
                Iterator<BMeasurement> cuts = bmc.getMeasurements().iterator();
                while (cuts.hasNext()) {
                    BearingMeasurementContribution.BMeasurement cut = cuts.next();
                    res.append(sdf.format(new Date(cut.getDate().getTime() - offset)) + ","
                            + Math.toDegrees(cut.getBearingRads()) + newLine);
                }
            }
        }

        // hmm, did we find anything
        if (res.length() > 0) {
            // ok, put it on the clipboard.
            new TextTransfer().setClipboardContents(res.toString());
        }
    }
}

From source file:us.bojie.rawgyroscope.camera.Camera2RawFragment.java

private void reset() {
    orientation = new GyroscopeOrientation(getActivity());

    handler = new Handler();

    runnable = new Runnable() {
        @Override//from ww w.j av a  2  s.  c o m
        public void run() {
            vOrientation = orientation.getOrientation();
            dataReady = true;

            XAxis = String.format("%.2f", Math.toDegrees(vOrientation[0]));
            YAxis = String.format("%.2f", Math.toDegrees(vOrientation[1]));
            ZAxis = String.format("%.2f", Math.toDegrees(vOrientation[2]));

        }
    };
}

From source file:com.google.appinventor.components.runtime.GoogleMap.java

@SimpleFunction
public String getBoundingBox(double latitudeInDegrees, double longitudeInDegrees, double halfSideInKm) {
    // Semi-axes of WGS-84 geoidal reference
    double WGS84_a = 6378137.0; // Major semiaxis [m]
    double WGS84_b = 6356752.3; // Minor semiaxis [m]

    // Bounding box surrounding the point at given coordinates,
    // assuming local approximation of Earth surface as a sphere
    // of radius given by WGS84
    double lat = Math.toRadians(latitudeInDegrees);
    double lon = Math.toRadians(longitudeInDegrees);
    double halfSide = 1000 * halfSideInKm;

    // Radius of Earth at given latitude
    // Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
    // http://en.wikipedia.org/wiki/Earth_radius
    double An = WGS84_a * WGS84_a * Math.cos(lat);
    double Bn = WGS84_b * WGS84_b * Math.sin(lat);
    double Ad = WGS84_a * Math.cos(lat);
    double Bd = WGS84_b * Math.sin(lat);
    double radius = Math.sqrt((An * An + Bn * Bn) / (Ad * Ad + Bd * Bd));

    // Radius of the parallel at given latitude
    double pradius = radius * Math.cos(lat);

    double latMin = lat - halfSide / radius;
    double latMax = lat + halfSide / radius;
    double lonMin = lon - halfSide / pradius;
    double lonMax = lon + halfSide / pradius;

    String coordinates = Math.toDegrees(latMin) + "," + Math.toDegrees(lonMin) + "," + Math.toDegrees(latMax)
            + "," + Math.toDegrees(lonMax);
    return coordinates;
}