因此,我一直在研究将此笛卡尔坐标转换为极坐标的函数以及if语句if x == 0 and y>0 then theta == pi/2
。
但是,如果要计算theta,则只需遵循以下形式:
theta = atan(y/x).
令我困惑的是,如果x == 0
此函数立即未定义?既然你被0
这个除法应该趋向于无穷大吧?
那么,在该函数中如何声明x ==0 and y>0
它始终等于pi / 2?
它可能是如此基本,我只是在将方法复杂化了很多……
提前致谢。
void carttopolar(float x, float y, double *radptr, double *thetaptr){
float theta;
*radptr = sqrt(x * x + y * y);
if(x==0){
if(y==0){
theta = 0.0;
}
else if(y>0){
theta = M_PI_2;
}
else{
theta = -M_PI_2;
}
}
else{
theta = atan(y/x);
}
*thetaptr = theta;
}
关键是atan是tan的倒数,而tan实际上在某些点上会生成无限值,这是一张图片:
该代码捕获了将无穷大的参数输入atan并返回产生无穷大的角度的情况(+/- pi / 2分别给出了正无穷大或负无穷大)。
普遍接受的方法是仅使用该atan2
函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句