converts cartesian coordinates (positive Y is down, positive X is right) into an angle in degrees (0 degrees is up, clockwise is positive). - Java java.lang

Java examples for java.lang:Math Geometry

Description

converts cartesian coordinates (positive Y is down, positive X is right) into an angle in degrees (0 degrees is up, clockwise is positive).

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int x = 2;
        int y = 2;
        System.out.println(toAngle(x, y));
    }/*from w  w w.j  a va 2s  . c o m*/

    /**
     * converts cartesian coordinates (positive Y is down, positive X is right)
     * into an angle in degrees (0 degrees is up, clockwise is positive). The
     * angle is between 0 and 359, inclusive.
     * 
     * @param x
     * @param y
     * @return
     */
    public static int toAngle(int x, int y) {
        return (int) ((Math.toDegrees(Math.atan2(y, x)) + 90) % 360);
    }
}

Related Tutorials