Java Math atan2()

The Java Math atan2() method converts the specified rectangular coordinates (x, y) into polar coordinates (r, θ) and returns the angle theta (θ).

The syntax of the atan2() method is:

Math.atan2(double y, double x)

Here, atan2() is a static method. Hence, we are accessing the method using the class name, Math.

atan2() Parameters

The atan2() method takes two parameters.

  • x/y - rectangular coordinates x and y

Note: The coordinates x and y represent a point in a two-dimensional plane.

atan2() Return Values

  • returns angle θ by converting coordinates (x, y) to coordinates (r, θ)

Example: Java Math.atan2()

class Main {
  public static void main(String[] args) {

    // two coordinates x and y
    double x = 3.7;
    double y = 6.45;

    // get angle θ
    double theta = Math.atan2(y, x);
    System.out.println(theta);                   // 1.0499821573815171

    // convert into the degree
    System.out.println(Math.toDegrees(theta));    // 60.15954618200191

Here, the atan2() method converts the coordinates (x, y) to coordinates (r, θ) and returns the angle theta (θ).

We have used the Math.toDegrees() method to convert the angle θ into the degree.

Did you find this article helpful?