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

直角坐标转极坐标

程序员文章站 2022-04-03 23:17:31
...

1 atan2的用法 :atna2(y,x)求出原点与x轴的夹角(-π,π]

atan : atan(y/x) 取值范围为(-1/2π,1/2π)

atan2比 atan稳定
2 Rad_to_deg = 57.29577951 弧度转角度

3example:

// coordin.h
# ifndef COORDIN_H_
# define COORDIN_H_

struct polar 
{
	double distance ;
	double angle;
};

struct rect
{
	double x;
	double y;
};

//prototype
polar rect_to_polar (rect xypos);
void show_polar(polar dapos);

#endif

// file1.cpp
# include <iostream>
# include "coordin.h"   //structure templates,function prototypes

using namespace std;

int main()
{
	rect rplace;
	polar pplace;

	cout<<"Enter the x and y values:";
	while (cin>>rplace.x>>rplace.y)   //slick use of cin
	{
		pplace = rect_to_polar(rplace);
		show_polar(pplace);
		cout<<"Text two numbers (q to quit ): ";
	}
	cout<<"Bye!\n";
	return 0;
}
// file2.cpp
# include <iostream>
# include <cmath>
# include "coordin.h"   //structure template ,function prototypes

//convert rectangular to polar coordinates 直角坐标转换成极坐标

polar rect_to_polar(rect xypos)
{
	using namespace std ;
	polar answer;
	answer.distance =
		sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
	answer.angle = atan2(xypos.y,xypos.x);
	return answer; //return a polar strucutre

}

//show polar coordinates,convert angle to degress

void show_polar(polar dapos)
{
	 using namespace  std;
	 const double Rad_to_deg = 57.29577951;

	 cout<<"distance = "<<dapos.distance;
	 cout<<", angle = "<<dapos.angle * Rad_to_deg;
	 cout<<" degrees\n";
}