C++ cmath atan2() 函数
示例
根据直角坐标,返回极坐标的角度(弧度制):
cout << atan2(0.5, 0.5);
cout << atan2(-0.5, -0.5);
cout << atan2(5, 5);
cout << atan2(10, 20);
cout << atan2(5, -5);
cout << atan2(-10, 10);
亲自试一试 »
定义和用法
atan2() 函数返回由直角坐标 (x, y) 转换为极坐标 (r, theta) 得到的角度 theta(以弧度为单位)。
这与调用 atan(y/x) 相同,区别在于 atan2() 函数会考虑 x 的负值,因此它可以返回 -π/2 到 π/2 范围之外的角度。
atan2() 函数定义在 <cmath> 头文件中。
注意:在atan2()方法中,y坐标在前,x坐标在后。这是因为它等价于y/x的反正切值。
语法
以下选项之一:
atan2(double y, double x);
atan2(float y, float x);
参数值
| 参数 | 描述 |
|---|---|
| y | 必填。要计算角度的点的 y 坐标。 如果此值为整数,则会被视为 double 类型。 |
| x | 必填。要计算角度的点的 x 坐标。 如果此值为整数,则会被视为 double 类型。 |
技术细节
| 返回值: | 一个float值(如果所有参数均为浮点数)或double值(在其他情况下),表示点 (x, y) 绕原点 (0, 0) 所成的角度(以弧度为单位)。 |
|---|

