进行相机坐标系相关公式推导时,经常碰到三角函数的使用。时间一长就生疏,碰到问题再查,很费时间。所以就总结一下,也希望能帮到更多的人。下面就通过简练的代码,把常用的cos、sin、tan、atan等通过代码及结果都说清楚。
注意弧度和角度的区别!!!
1、代码
#include
#include
using namespace std;
#define PI 3.1415926
int main()
{
//注意tan、atan等函数不能接受整数,tan(45)会报错“error C2668: 'tan' : ambiguous call to overloaded function”
float tanValue1 = tan(45.0f);
float tanValue2 = tan(45*PI/180.0f);
cout<
cout<
cout<
float atanValue1 = atan(1.0f);
float atanValue2 = atan(1.0f)*180.0f/PI;
cout<
cout<
cout<
cin.get();
return 0;
}
2、输出结果
3、结论
C++中sin、cos、tan、asin、acos、atan等三角函数的输入是弧度,而不是角度。
如果想对角度进行这些三角函数运算,需要乘以(PI/180)把角度转为弧度。