Languages
[Edit]
EN

Java - Math.atan2() method example

3 points
Created by:
Sylvie-Justice
490

The Math.atan2() function returns the angle in radians in the range -Math.PI/2 to +Math.PI/2 between the positive x-axis and the ray to the point (x, y) ≠ (0, 0).

public class MathExample {

    public static void main(String[] args) {
        //                              y   x         angle in radians
        System.out.println( Math.atan2( 2,  4) ); //  0.4636476090008061 <-   ~26.6 degrees
        System.out.println( Math.atan2( 4, -2) ); //  2.0344439357957027 <-  ~116.6 degrees
        System.out.println( Math.atan2(-2, -4) ); // -2.677945044588987  <- ~-153.4 degrees
        System.out.println( Math.atan2(-4,  2) ); // -1.1071487177940904 <-  ~-63.4 degrees
    }
}

atan2() method has been visualized on below image:

atan2(y, x) function visualization - Java Math Object.
atan2(y, x) function visualization - Java Math Object.

1. Documentation

Syntax
package java.lang;

public final class Math {

    public static double atan2(double y, double x) { ... }

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.atan2() call.

Parametersy, x - double values that are coordinates of the point (primitive value).
Result

number value of angle between two lines OP and OX in radians in the range -Math.PI/2 to +Math.PI/2 (primitive value).

Where:

  • O=(0, 0) - intersection point of coordinate system axes,
  • P=(x, y) - out point,
  • OX - positive part of x-axis.

If point is in the 1st (I) or 2nd (II) quadrant angle is measured in a counterclockwise direction.

If point is in the 3rd (III) or 4th (IV) quadrant angle is measured in a clockwise direction.

Note: angle arrows show measured angles for points located in different quadrants.

Description

atan2 is a static method that takes two parameters and returns an approximation of the arctangent(y/x) function taking into account quadrants of a point location (P=(x, y)).

2. Working with degrees

public class CustomMath {

    static double calculateAngle(double y, double x) {
        double angle = Math.atan2(y, x);
        return (180 / Math.PI) * angle; // rad to deg conversion
    }
    
    public static void main(String[] args) {
        //                                  y   x           degrees
        System.out.println( calculateAngle( 2,  4) ); //  26.56505117707799 
        System.out.println( calculateAngle( 4, -2) ); // 116.56505117707799 
        System.out.println( calculateAngle(-2, -4) ); // -153.434948822922  
        System.out.println( calculateAngle(-4,  2) ); // -63.43494882292201
    }
}

3.  Conversion to only clockwise angles in degrees

This section shows how to convert angles to clockwise angles (from 0 to 360 degrees).

public class CustomMath {

    static double calculateAngle(double y, double x) {
        double angle = Math.atan2(y, x);
        if (angle < 0.0) {
            angle += 2.0 * Math.PI;
        }
        return (180 / Math.PI) * angle; // rad to deg conversion
    }
    
    public static void main(String[] args) {
        //                                  y   x           degrees
        System.out.println( calculateAngle( 2,  4) ); //  26.56505117707799
        System.out.println( calculateAngle( 4, -2) ); // 116.56505117707799
        System.out.println( calculateAngle(-2, -4) ); // 206.565051177078
        System.out.println( calculateAngle(-4,  2) ); // 296.565051177078
    }
}

4.  Conversion to only counterclockwise angles in degrees

This section shows how to convert angles to counterclockwise angles (from -360 to 0 degrees).

public class CustomMath {

    static double calculateAngle(double y, double x) {
        double angle = Math.atan2(y, x);
        if (angle > 0.0) {
            angle -= 2.0 * Math.PI;
        }
        return (180 / Math.PI) * angle; // rad to deg conversion
    }
    
    public static void main(String[] args) {
        //                                  y   x            degrees
        System.out.println( calculateAngle( 2,  4) ); // -333.434948822922 
        System.out.println( calculateAngle( 4, -2) ); // -243.434948822922 
        System.out.println( calculateAngle(-2, -4) ); // -153.434948822922 
        System.out.println( calculateAngle(-4,  2) ); // -63.43494882292201
    }
}

References

  1. atan2 - Wikipedia
  2. Inverse trigonometric functions - Wikipedia

Alternative titles

  1. Java - Math.atan2() documentation with examples
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - Math object

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join