6-1 直角坐标转极坐标
请编写函数,将直角坐标 ( x,y ) 转换为极坐标 ( r,θ )。
函数原型
void ToPolar(double *r, double *theta, double x, double y);
说明:r 和 theta 分别为指示极径和极角(弧度)的指针,x 和 y 分别为横坐标和纵坐标。
裁判程序
#include <stdio.h> #include <math.h> void ToPolar(double *r, double *theta, double x, double y); int main() { double x, y, r, theta; scanf("( %lg, %lg )", &x, &y); ToPolar(&r, &theta, x, y); printf("( %.10g, %.10g )\n", r, theta); return 0; } /* 你提交的程序将被嵌在这里 */
输入样例1
( -1.29903810567665797, 0.75 )
输出样例1
( 1.5, 2.617993878 )
输入样例2
( -2, -3.4641016151377546 )
输出样例2
( 4, -2.094395102 )
void ToPolar(double *r, double *theta, double x, double y){
*r=sqrt(x*x+y*y);
*theta=atan2(y,x);
}