您的位置:首页 > 编程语言 > C语言/C++

实例讲解C语言atan和atan2函数

2012-09-02 10:01 357 查看
http://anony3721.blog.163.com/blog/static/51197420114962425307/

使用时需要预包含#include <math.h>

(1)atan(x) 不包括角度的象限信息,返回值在[-pi/2,pi/2]

The atan function computes the principal value of the arc tangent of x, returning a value in the range [-pi/2, pi/2]. 由于tan函数是以pi为周期的tanθ=tan(θ+pi),所以radAngle=atan(slope)实际上返回的是数学上atan(slope)的在[-pi/2, pi/2]之间的主值部分,注意radAngle以弧度为单位。例如pi/4的正切值为1,pi/4
+ pi = 5pi/4的正切值也是1,此时C语言中计算的atan(1)=pi/4仅仅是位于tan函数在[-pi/2,pi/2]的部分,也就是说atan丢失了角度的象限信息,不能通过radAngle=atan(slope)来判断radAngle位于哪个象限。

#include <stdio.h>

#include <math.h>

#define PI 3.14159

int main(int argc, char *argv[])

{

float radAngle,degAngle;

radAngle = atan(1); //atan(x) returns a value in the range [-pi/2, pi/2]

degAngle = radAngle*180/PI;

printf("arctangent in radians %f\n",radAngle); // 仅仅给出了第一象限的结果,第三象限的角度值不可能通过atan求出

printf("arctangent in degrees %f\n",degAngle);

radAngle = atan(-1);

degAngle = radAngle*180/PI;

printf("arctangent in radians %f\n",radAngle); //仅仅给出了第四象限的结果,不可能给出第二象限的结果

printf("arctangent in degrees %f\n",degAngle);

return 0;

}

(2)atan2(y,x) 包含角度的象限信息,返回值在[-pi,pi]

The atan2 function computes the principal value of the arc tangent of y / x, using the signs of both arguments to determine the quadrant of the return value. 由于atan2包含角度的象限信息所以用的比atan多。

1)一个比较实用的功能就是将直角坐标系下的点(x,y)转换为极坐标系下的点(rho,theta)。x = rho * cos(theta); y = rho * sin(theta),rho = sqrt( x^2 + y^2); theta = atan2(y,x)。

2)atan2计算两点间连线的倾斜角,这种方法非常的有用。在OpengL中视角fovy的计算就用到了atan2如下所示

// Calculating Field of View

#define PI 3.1415926535

double calculateAngle(double size, double distance)

{

double radtheta, degtheta;

radtheta = 2.0 * atan2 (size/2.0, distance);

degtheta = (180.0 * radtheta) / PI;

return degtheta;

}

atan2(y,x)函数返回点(x,y)和原点(0,0)之间直线的倾斜角.那么如何计算任意两点间直线的倾斜角呢?只需要将两点x,y坐标分别相减得到一个新的点(x2-x1,y2-y1).然后利用他求出角度就可以了.使用下面的一个转换可以实现计算出两点间连线的夹角.

atan2(y2-y1,x2-x1)不过这样我们得到的是一个弧度值,在一般情况下我们需要把它转换为一个角度.

下面我们用一段代码来测试一下这样的转换.

//测试,计算点(3,3)和(5,5)构成的连线的夹角

x=atan2(5-3,5-3)//输出0.785398

x=x*180/pi//转换为角度,输出45.000038

#include <stdio.h>

#include <math.h>

#define PI 3.14159

int main(int argc, char *argv[])

{

float radAngle,degAngle;

radAngle = atan2(7,7); //atan(x) returns a value in the range [-pi/2, pi/2]

degAngle = radAngle*180/PI;

printf("in the fist quadrant,arctangent in radians %f\n",radAngle);

printf("in the fist quadrant,arctangent in degrees %f\n",degAngle);

radAngle = atan2(-7,7);

degAngle = radAngle*180/PI;

printf("in the second quadrant,arctangent in radians %f\n",radAngle);

printf("in the second quadrant,arctangent in degrees %f\n",degAngle);

radAngle = atan2(-7,-7);

degAngle = radAngle*180/PI;

printf("in the third quadrant,arctangent in radians %f\n",radAngle);

printf("in the third quadrant,arctangent in degrees %f\n",degAngle);

radAngle = atan2(7,-7);

degAngle = radAngle*180/PI;

printf("in the fourth quadrant,arctangent in radians %f\n",radAngle);

printf("in the fourth quadrant,arctangent in degrees %f\n",degAngle);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: