欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

直角坐标转极坐标

程序员文章站 2022-04-03 23:20:49
...
#include<iostream>
#include<cmath>
using namespace std;
struct rext		//直角坐标系
{
	double x;
	double y;
};
struct polar	//极坐标系
{
	double distance;
	double angle;
};
void rect_to_polar(const rext*pxy, polar *pda)
{
	pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
	pda->angle = atan2(pxy->y, pxy->x);
}
void show_polar(const polar * pda)
{
	const double Red_to_deg = 57.29577951;
	cout << "distance = " << pda->distance << endl;
	cout << "angle = " << pda->angle*Red_to_deg << endl;
}
int main()
{
	rext rplace;
	polar pplace;
	cout << "Enter the x and y :";
	while (cin >> rplace.x >> rplace.y)
	{
		rect_to_polar( &rplace, &pplace);
		show_polar(&pplace);
		cin.get();
	}
	cin.get();
	return 0;
}