atan2()
方法的语法是:
Math.atan2(double y, double x)
这里,
atan2()
是一个静态方法。因此,我们使用类名访问该方法,
Math
。
atan2() 参数
atan2()
方法接受两个参数。
注意: 坐标 x 和 y 表示二维平面中的一个点。
atan2() 返回值
通过将坐标(x, y)转换为坐标(r, θ)来返回角度θ示例: 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 } }
这里,
atan2()
方法将坐标 (x, y) 转换为坐标 (r, θ) 并返回角度θ(θ)。
我们使用了Math.toDegrees() 方法来转换角度
θ
成度数。