Close

Java - Math.atan2() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Method:

public static double atan2(double y,
                           double x)

Returns the angle theta from the conversion of rectangular coordinates ( xy ) 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.


Examples


package com.logicbig.example.math;

public class Atan2Example {

public static void main(String... args) {
double[] xNumbers = {1, 1.732, 1, 1, 0};
double[] yNumbers = {0, 1, 1, 1.732, 1};
for (int i = 0; i < xNumbers.length; ++i) {
double tan2Values = Math.atan2(yNumbers[i], xNumbers[i]);
double degree = Math.toDegrees(tan2Values);
System.out.printf("AngleTheta of rectangular coordinates (%s, %s) = %.2f degrees%n",
xNumbers[i], yNumbers[i], degree);
}
}
}

Output

AngleTheta of rectangular coordinates (1.0, 0.0) = 0.00 degrees
AngleTheta of rectangular coordinates (1.732, 1.0) = 30.00 degrees
AngleTheta of rectangular coordinates (1.0, 1.0) = 45.00 degrees
AngleTheta of rectangular coordinates (1.0, 1.732) = 60.00 degrees
AngleTheta of rectangular coordinates (0.0, 1.0) = 90.00 degrees




See Also