Java atan2() 方法
atan2() 方法用于将矩形坐标 (x, y) 转换成极坐标 (r, theta),返回所得角 theta。该方法通过计算 y/x 的反正切值来计算相角 theta,范围为从 -pi 到 pi。
语法
double atan2(double y, double x)
参数
y -- 纵坐标。
x -- 横坐标。
返回值
与笛卡儿坐标中点 (x, y) 对应的极坐标中点 (r, theta) 的 theta 组件。
实例
public class Test{
public static void main(String args[]){
double x = 45.0;
double y = 30.0;
System.out.println( Math.atan2(x, y) );
}
}
编译以上程序,输出结果为:
0.982793723247329
Java Number类



LuncyTB
xhy***er@126.com
关于 Java 的 Math.atan2(double x,double y) 的笔记:
Math.atan2(double x,double y) 方法返回选 (x,y) 坐标的方向,以弧度记。
众所周知,Math.cos(double a) , Math.sin(double a) 返回a的余弦/正弦值,那么,这个 a 和 atan2 返回的 angle 有什么区别?
做程序测试:
public class Test{ public static void main(String args[]){ double x=1.73205081*4; double y=4; // 60度方向的一个点。 double a=Math.atan2(x,y); System.out.println("The angle is "+(a*180/Math.PI)); // 转换为角度制输出 } }对应输出:
所以,这个60度是点到原点连线与y轴的夹角。
再看:
public class Test{ public static void main(String args[]){ double direction=74.0; double a=direction*Math.PI/180.0; double x=Math.cos(a)*20.0; double y=Math.sin(a)*20.0; System.out.println("The angle is "+(Math.atan2(x,y)*180/Math.PI)); } }对应输出结果:
与 angle(a)互余。
public class Test{ public static void main(String args[]){ double direction=170.0; double a=direction*Math.PI/180.0; double x=Math.cos(a)*20.0; double y=Math.sin(a)*20.0; System.out.println("The angle is "+(Math.atan2(x,y)*180/Math.PI)); } }对应输出结果:
可见,它是顺时针方向,0弧度在正y轴;而正规三角函数是逆时针方向,0弧度在正x轴。
LuncyTB
xhy***er@126.com