直角坐标转极坐标

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 
# 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 
# include 
# 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 = "<

你可能感兴趣的:(c++)