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:Main.java

public static void main(String[] args) {

    double x = 0.5;
    double y = -1;

    // convert them in degrees
    x = Math.toDegrees(x);
    y = Math.toDegrees(y);/*from  www .j a va  2  s  .com*/

    System.out.println(x);
    System.out.println(y);

}

From source file:Main.java

public static void main(String[] args) {

    // get a variable x which is equal to PI/2
    double x = Math.PI / 2;

    // get a variable y which is equal to PI/3
    double y = Math.PI / 3;

    // convert x  and y to degrees
    x = Math.toDegrees(x);
    y = Math.toDegrees(y);//  w w w .j a  v a 2  s .  c  om

    // get the polar coordinates
    System.out.println("Math.atan2(" + x + "," + y + ")=" + Math.atan2(x, y));

}

From source file:MainClass.java

public static void main(String args[]) {
    double theta = 120.0;

    System.out.println(theta + " degrees is " + Math.toRadians(theta) + " radians.");

    theta = 1.312;/*from   w ww. ja v a 2  s .c  o  m*/
    System.out.println(theta + " radians is " + Math.toDegrees(theta) + " degrees.");
}

From source file:Main.java

public static void main(String[] args) {
    for (int i = 0; i <= 180; i++) {
        double angleRad = Math.toRadians(i);
        AffineTransform at = createRandomTransform(angleRad);
        double extractedAngleRad = extractAngle(at);
        System.out//  ww w. ja v  a 2s . c  o  m
                .println("In: " + Math.toDegrees(angleRad) + " " + "Out " + Math.toDegrees(extractedAngleRad));
    }
}

From source file:org.jlab.clas.clas.math.Test.java

public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
        double x = -100 + (double) (i + 1);
        for (int j = 0; j < 100; j++) {
            double y = -100 + (double) (j + 1);
            System.out.println("angle " + (float) Math.toDegrees(Math.atan2(y, x)) + "  " + "I1-D "
                    + (Icecore.atan2((float) y, (float) x) - org.apache.commons.math3.util.FastMath.atan2(y, x))
                    + "  " + "I2-D "
                    + (Icecore2.atan2((float) y, (float) x)
                            - org.apache.commons.math3.util.FastMath.atan2(y, x))
                    + "  " + "K-D "
                    + (Kappa.atan2((float) y, (float) x) - org.apache.commons.math3.util.FastMath.atan2(y, x)));
        }/*from w w  w .j  ava  2  s . c o m*/
    }
}

From source file:TrigFun.java

public static void main(String[] args) {
    double rads, degs, tanA, coTanA;

    // Obtain angle in degrees from user
    degs = 120d;//from   w  ww  . j a  v  a  2  s.co  m
    // Convert degrees to radian
    rads = Math.toRadians(degs);

    // Calculate tangent
    tanA = Math.tan(rads);
    System.out.println("Tangent = " + tanA);

    // Calculate cotangent
    coTanA = 1.0 / Math.tan(rads);
    System.out.println("Cotangent = " + coTanA);

    // Calculate arc-tangent
    rads = Math.atan(tanA);
    degs = Math.toDegrees(rads);
    System.out.println("Arc tangent: " + degs);

    // Calculate arc-cotangent
    rads = Math.atan(1 / coTanA);
    degs = Math.toDegrees(rads);
    System.out.println("Arc cotangent: " + degs);
}

From source file:HypFun.java

public static void main(String[] args) {
    double rads, degs, sinHA, cosHA, tanHA, asinHA;

    // Obtain angle in degrees from user
    degs = 20d;/* w ww.j  av  a 2  s  .co m*/
    // Convert degrees to radian
    rads = Math.toRadians(degs);

    // Calculate hyperbolic sine
    sinHA = (Math.exp(rads) - Math.exp(-rads)) / 2;
    System.out.println("Hyperbolic sine = " + sinHA);

    // Calculate Hyperbolic cosine
    cosHA = (Math.exp(rads) + Math.exp(-rads)) / 2;
    System.out.println("Hyperbolic cosine = " + cosHA);

    // Calculate hyperbolic tangent
    tanHA = sinHA / cosHA;
    System.out.println("Hyperbolic tangent = " + tanHA);

    // Calculate hyperbolic arc-sine
    asinHA = Math.log(sinHA + Math.sqrt((sinHA * sinHA) + 1.0));
    degs = Math.toDegrees(asinHA);
    System.out.println("Arc hyperbolic sine = " + degs);
}

From source file:TrigonometricDemo.java

public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);

    System.out.format("The value of pi is %.4f%n", Math.PI);
    System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians));
    System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians));
    System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians));
    System.out.format("The arcsine of %.4f is %.4f degrees %n", Math.sin(radians),
            Math.toDegrees(Math.asin(Math.sin(radians))));
    System.out.format("The arccosine of %.4f is %.4f degrees %n", Math.cos(radians),
            Math.toDegrees(Math.acos(Math.cos(radians))));
    System.out.format("The arctangent of %.4f is %.4f degrees %n", Math.tan(radians),
            Math.toDegrees(Math.atan(Math.tan(radians))));

}

From source file:TrigonometricDemo.java

public static void main(String[] args) {
    double degrees = 45.0;
    double radians = Math.toRadians(degrees);

    System.out.println("The value of pi is " + Math.PI);
    System.out.println("The sine of " + degrees + " is " + Math.sin(radians));
    System.out.println("The cosine of " + degrees + " is " + Math.cos(radians));
    System.out.println("The tangent of " + degrees + " is " + Math.tan(radians));
    System.out.println("The arc sine of " + Math.sin(radians) + " is "
            + Math.toDegrees(Math.asin(Math.sin(radians))) + " degrees");
    System.out.println("The arc cosine of " + Math.cos(radians) + " is "
            + Math.toDegrees(Math.acos(Math.cos(radians))) + " degrees");
    System.out.println("The arc tangent of " + Math.tan(radians) + " is "
            + Math.toDegrees(Math.atan(Math.tan(radians))) + " degrees");
}

From source file:graticules2wld.Main.java

/**
 * @param args/*from  w  ww  .j  a  v  a2  s  .  c om*/
 * @throws Exception
 */
public static void main(String[] args) throws Exception {

    /* parse the command line arguments */
    // create the command line parser
    CommandLineParser parser = new PosixParser();

    // create the Options
    Options options = new Options();
    options.addOption("x", "originx", true, "x component of projected coordinates of upper left pixel");
    options.addOption("y", "originy", true, "y component of projected coordinates of upper left pixel");
    options.addOption("u", "tometers", true, "multiplication factor to get source units into meters");
    options.addOption("h", "help", false, "prints this usage page");
    options.addOption("d", "debug", false, "prints debugging information to stdout");

    double originNorthing = 0;
    double originEasting = 0;

    String inputFileName = null;
    String outputFileName = null;

    try {
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        if (line.hasOption("help"))
            printUsage(0); // print usage then exit using a non error exit status

        if (line.hasOption("debug"))
            debug = true;

        // these arguments are required
        if (!line.hasOption("originy") || !line.hasOption("originx"))
            printUsage(1);

        originNorthing = Double.parseDouble(line.getOptionValue("originy"));
        originEasting = Double.parseDouble(line.getOptionValue("originx"));

        if (line.hasOption("tometers"))
            unitsToMeters = Double.parseDouble(line.getOptionValue("tometers"));

        // two args should be left. the input csv file name and the output wld file name.
        String[] iofiles = line.getArgs();
        if (iofiles.length < 2) {
            printUsage(1);
        }

        inputFileName = iofiles[0];
        outputFileName = iofiles[1];
    } catch (ParseException exp) {
        System.err.println("Unexpected exception:" + exp.getMessage());
        System.exit(1);
    }

    // try to open the input file for reading and the output file for writing
    File graticulesCsvFile;
    BufferedReader csvReader = null;

    File wldFile;
    BufferedWriter wldWriter = null;

    try {
        graticulesCsvFile = new File(inputFileName);
        csvReader = new BufferedReader(new FileReader(graticulesCsvFile));
    } catch (IOException exp) {
        System.err.println("Could not open input file for reading: " + inputFileName);
        System.exit(1);
    }

    try {
        wldFile = new File(outputFileName);
        wldWriter = new BufferedWriter(new FileWriter(wldFile));
    } catch (IOException exp) {
        System.err.println("Could not open output file for writing: " + outputFileName);
        System.exit(1);
    }

    // list of lon graticules and lat graticules
    ArrayList<Graticule> lonGrats = new ArrayList<Graticule>();
    ArrayList<Graticule> latGrats = new ArrayList<Graticule>();

    // read the source CSV and convert its information into the two ArrayList<Graticule> data structures
    readCSV(csvReader, lonGrats, latGrats);

    // we now need to start finding the world file paramaters
    DescriptiveStatistics stats = new DescriptiveStatistics();

    // find theta and phi
    for (Graticule g : latGrats) {
        stats.addValue(g.angle());
    }

    double theta = stats.getMean(); // we use the mean of the lat angles as theta
    if (debug)
        System.out.println("theta range = " + Math.toDegrees(stats.getMax() - stats.getMin()));
    stats.clear();

    for (Graticule g : lonGrats) {
        stats.addValue(g.angle());
    }

    double phi = stats.getMean(); // ... and the mean of the lon angles for phi
    if (debug)
        System.out.println("phi range = " + Math.toDegrees(stats.getMax() - stats.getMin()));
    stats.clear();

    // print these if in debug mode
    if (debug) {
        System.out.println("theta = " + Math.toDegrees(theta) + "deg");
        System.out.println("phi = " + Math.toDegrees(phi) + "deg");
    }

    // find x and y (distance beteen pixels in map units)
    Collections.sort(latGrats);
    Collections.sort(lonGrats);
    int prevMapValue = 0; //fixme: how to stop warning about not being initilised?
    Line2D prevGratPixelSys = new Line2D.Double();

    boolean first = true;
    for (Graticule g : latGrats) {
        if (!first) {
            int deltaMapValue = Math.abs(g.realValue() - prevMapValue);
            double deltaPixelValue = (g.l.ptLineDist(prevGratPixelSys.getP1())
                    + (g.l.ptLineDist(prevGratPixelSys.getP2()))) / 2;

            double delta = deltaMapValue / deltaPixelValue;
            stats.addValue(delta);
        } else {
            first = false;
            prevMapValue = g.realValue();
            prevGratPixelSys = (Line2D) g.l.clone();
        }
    }

    double y = stats.getMean();
    if (debug)
        System.out.println("y range = " + (stats.getMax() - stats.getMin()));
    stats.clear();

    first = true;
    for (Graticule g : lonGrats) {
        if (!first) {
            int deltaMapValue = g.realValue() - prevMapValue;
            double deltaPixelValue = (g.l.ptLineDist(prevGratPixelSys.getP1())
                    + (g.l.ptLineDist(prevGratPixelSys.getP2()))) / 2;

            double delta = deltaMapValue / deltaPixelValue;
            stats.addValue(delta);
        } else {
            first = false;
            prevMapValue = g.realValue();
            prevGratPixelSys = (Line2D) g.l.clone();
        }
    }

    double x = stats.getMean();
    if (debug)
        System.out.println("x range = " + (stats.getMax() - stats.getMin()));
    stats.clear();

    if (debug) {
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    }

    SimpleRegression regression = new SimpleRegression();

    // C, F are translation terms: x, y map coordinates of the center of the upper-left pixel
    for (Graticule g : latGrats) {
        // find perp dist to pixel space 0,0
        Double perpPixelDist = g.l.ptLineDist(new Point2D.Double(0, 0));

        // find the map space distance from this graticule to the center of the 0,0 pixel
        Double perpMapDist = perpPixelDist * y; // perpMapDist / perpPixelDist = y

        regression.addData(perpMapDist, g.realValue());
    }

    double F = regression.getIntercept();
    regression.clear();

    for (Graticule g : lonGrats) {
        // find perp dist to pixel space 0,0
        Double perpPixelDist = g.l.ptLineDist(new Point2D.Double(0, 0));

        // find the map space distance from this graticule to the center of the 0,0 pixel
        Double perpMapDist = perpPixelDist * x; // perpMapDist / perpPixelDist = x

        regression.addData(perpMapDist, g.realValue());
    }

    double C = regression.getIntercept();
    regression.clear();

    if (debug) {
        System.out.println("Upper Left pixel has coordinates " + C + ", " + F);
    }

    // convert to meters
    C *= unitsToMeters;
    F *= unitsToMeters;

    // C,F store the projected (in map units) coordinates of the upper left pixel.
    // originNorthing,originEasting is the offset we need to apply to 0,0 to push the offsets into our global coordinate system 
    C = originEasting + C;
    F = originNorthing + F;

    // calculate the affine transformation matrix elements
    double D = -1 * x * unitsToMeters * Math.sin(theta);
    double A = x * unitsToMeters * Math.cos(theta);
    double B = y * unitsToMeters * Math.sin(phi); // if should be negative, it'll formed by negative sin
    double E = -1 * y * unitsToMeters * Math.cos(phi);

    /*
     * Line 1: A: pixel size in the x-direction in map units/pixel
     * Line 2: D: rotation about y-axis
     * Line 3: B: rotation about x-axis
     * Line 4: E: pixel size in the y-direction in map units, almost always negative[3]
     * Line 5: C: x-coordinate of the center of the upper left pixel
     * Line 6: F: y-coordinate of the center of the upper left pixel
     */
    if (debug) {
        System.out.println("A = " + A);
        System.out.println("D = " + D);
        System.out.println("B = " + B);
        System.out.println("E = " + E);
        System.out.println("C = " + C);
        System.out.println("F = " + F);

        // write the world file
        System.out.println();
        System.out.println("World File:");
        System.out.println(A);
        System.out.println(D);
        System.out.println(B);
        System.out.println(E);
        System.out.println(C);
        System.out.println(F);
    }

    // write to the .wld file
    wldWriter.write(A + "\n");
    wldWriter.write(D + "\n");
    wldWriter.write(B + "\n");
    wldWriter.write(E + "\n");
    wldWriter.write(C + "\n");
    wldWriter.write(F + "\n");

    wldWriter.close();
}