Java Math atan2() 方法

定义和用法

atan2() 方法返回从直角坐标 (x, y) 转换为极坐标 (r, theta) 时的角度 theta(以弧度为单位)。

这与调用 atan(y/x) 相同,只是它考虑了 x 的负值,因此可以返回超出范围 -PI/2 到 PI/2 的角度。

注意:atan2() 方法中,y 坐标在前,然后是 x 坐标。这是因为它正在执行 y / x 的除法。

实例

给定直角坐标,返回极坐标的角度(以弧度为单位):

System.out.println(Math.atan2(0.5, 0.5));
System.out.println(Math.atan2(-0.5, -0.5));
System.out.println(Math.atan2(5, 5));
System.out.println(Math.atan2(10, 20));
System.out.println(Math.atan2(-5, -5));
System.out.println(Math.atan2(-10, 10)); 

亲自试一试

语法

public static double atan2(double y, double x)

参数

参数 描述
y 必需。要查找角度的点的 y 坐标。
x 必需。要查找角度的点的 x 坐标。

技术细节

返回: double 值,表示点 (x, y) 围绕原点 (0, 0) 形成的角度(以弧度为单位)。
Java 版本: 任意版本。