【发布时间】:2012-08-22 10:35:11
【问题描述】:
以下是将笛卡尔坐标转换为极坐标的代码。 else if 语句 (y>0) = pi/2 else -pi/2 ... 这两行的相关性是什么?当然,您只需要 theta = atan(y/x) 和 r = sqrt(x^2 + y^2) 来确定正确的 theta 和 r? 当我进入调试并放置检查点以查看代码如何运行时,似乎这部分也从未使用过......
有人可以阐明这些行的相关性吗?
谢谢。
这是应用程序的代码;
void cartesianToPolar (float x, float y, double *rPtr, double *thetaPtr)
{
//store radius in supplied address - calc for r
*rPtr = sqrt(x * x + y * y);
//calc theta
float theta;
if (x == 0.0) {
if (y== 0.0) {
theta = 0.0;
} else if ( y > 0){
theta = M_PI_2;
} else {
theta = -M_PI_2;
}
}else{
theta = atan(y/x);
}
//store theta in address
*thetaPtr = theta;
}
int main (int argc, const char * argv[])
{
double pi = 3.14;
double integerPart;
double fractionPart;
// Pass add of integerPart as argument
fractionPart = modf(pi, &integerPart);
// Find value stored in intpart
printf("integerPart = %.0f, fractionPart = %.2f\n", integerPart, fractionPart);
double x = 3.0;
double y = -4.0;
double radius;
double angle;
cartesianToPolar(x,y,&angle,&radius);
printf("(%.2f, %.2f) becomes (%.2f radiants, %.2f)\n", x, y, radius, angle);
return 0;
}
【问题讨论】:
-
请考虑更改您的标签。其中没有太多objective-c,而是标准c,您可能希望用“极坐标”标记它。
-
对不起,你说它的c与objective-c相反是正确的。
标签: c polar-coordinates