atan2() Method in Java

Rumman Ansari   Software Engineer   2019-03-30   6365 Share
☰ Table of Contents

Table of Content:


Description

On this tutorial, we will be showing a java example on how to use the atan2(double a) method of Math Class

The method converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.

Syntax

double atan2(double y, double x)

Parameters

Here is the detail of parameters ?

  • X ? X co-ordinate in double data type.

  • Y ? Y co-ordinate in double data type.

Return Value

  • This method returns the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

Example

public class MathATan2 {

   public static void main(String args[]) {
      double x = 45.0;
      double y = 30.0;

      System.out.println( Math.atan2(x, y) );
   }
}

Output

0.982793723247329
Press any key to continue . . .

Example

The following example shows the usage of lang.Math.atan2() method.



import java.lang.*;

public class MathDemo {

   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);

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

Output

Math.atan2(90.0,59.99999999999999)=0.9827937232473292
Press any key to continue . . .