java.lang.StrictMath atan2​(double y, double x)

Description

The atan2​(double y, double x) method of StrictMath class returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi.

Special cases:

  • If either argument is NaN, then the result is NaN.
  • If the first argument is positive zero and the second argument is positive, or the first argument is positive and finite and the second argument is positive infinity, then the result is positive zero.
  • If the first argument is negative zero and the second argument is positive, or the first argument is negative and finite and the second argument is positive infinity, then the result is negative zero.
  • If the first argument is positive zero and the second argument is negative, or the first argument is positive and finite and the second argument is negative infinity, then the result is the double value closest to pi.
  • If the first argument is negative zero and the second argument is negative, or the first argument is negative and finite and the second argument is negative infinity, then the result is the double value closest to -pi.
  • If the first argument is positive and the second argument is positive zero or negative zero, or the first argument is positive infinity and the second argument is finite, then the result is the double value closest to pi/2.
  • If the first argument is negative and the second argument is positive zero or negative zero, or the first argument is negative infinity and the second argument is finite, then the result is the double value closest to -pi/2.
  • If both arguments are positive infinity, then the result is the double value closest to pi/4.
  • If the first argument is positive infinity and the second argument is negative infinity, then the result is the double value closest to 3*pi/4.
  • If the first argument is negative infinity and the second argument is positive infinity, then the result is the double value closest to -pi/4.
  • If both arguments are negative infinity, then the result is the double value closest to -3*pi/4.

Notes:

The atan2​(double y, double x) method of StrictMath class is static thus it should be accessed statically which means the we would be calling this method in this format:

StrictMath.atan2​(double y, double x)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java compare method non statically.

Method Syntax

public static double atan2​(double y, double x)

Method Argument

Data Type Parameter Description
double y the ordinate coordinate
double x the abscissa coordinate

Method Returns

The atan2​(double y, double x) method returns the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

Compatibility

Requires Java 1.3 and up

Java StrictMath atan2​(double y, double x) Example

Below is a java code demonstrates the use of atan2​(double y, double x) method of StrictMath class. Basically the program below ask for two user input, one for the ordinate coordinate and one for abscissa coordinate. The result of ata2 method would be the theta of the point.

package com.javatutorialhq.java.examples;


import java.util.Scanner;

/*
 * A java example source code to demonstrate
 * the use of atan2(double y, double x)
 * method of StrictMath class
 */

public class StrictMathAtan2Example {

	public static void main(String[] args) {
		
		/* assume a coordinates
		 * Determine the theta component of the point
		 * given x and y coordinate
		 */
		
		// Ask user input for the ordinate
		System.out.print("Enter y:");
		
		// declare the scanner object
		Scanner scan = new Scanner(System.in);		
		
		// use scanner to get the user input and store it to a variable
		double y = scan.nextDouble();		
		
		
		// Ask another user input
		System.out.print("Enter x:");
		
		// store it to a variable
		double x = scan.nextDouble();	
		
		// close the scanner object
		scan.close();		
		
		// get the result	
		double theta = StrictMath.atan2(y,x);

		
		// print the result
		System.out.println("theta: "+theta);


	}

}

Sample Output

Below is the sample output when you run the above example.

Java StrictMath atan2 method example