The polar coordinate system is a two-dimensional system in which every point on the plane is uniquely determined by two numbers: the polar radius and the polar angle (also called the azimuth or the phase angle).
The radius is the distance from the point to the center, or pole, of the coordinate system. The angle is the angle between the polar axis and the ray connecting the pole with the point; it is measured counterclockwise from the ray corresponding to the direction of (this ray is called the polar axis).
Let point have Cartesian coordinates and polar coordinates . Then the formulas for converting from polar coordinates to Cartesian coordinates are as follows:
The function atan2(double , double ) computes the arctangent of and returns an angle in the interval (-\pi; \pi]. If x = 0 or both arguments are zero, the function returns .
In this problem, the polar angle must be given in the interval [0; 2\pi). Therefore, if the result of atan2 is negative, you should add to it.
Algorithm implementation
Read the input data.
scanf("%lf %lf",&a,&b);The polar angle of a point is computed using the atan2 function.
res = atan2(b,a);If the result of atan2 is negative, you should add to it, since the angle must lie within the interval [0; 2\pi).
if (res < 0) res += 2*PI;Print the answer.
printf("%.6lf\n",res);Algorithm implementation – class
#include <stdio.h>
#include <math.h>
#define PI acos(-1.0)
class Point
{
private:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y) {}
void ReadPoint(void)
{
scanf("%lf %lf",&x,&y);
}
double GetPolarAngle()
{
double res = atan2(y,x);
if (res < 0) res += 2*PI;
return res;
}
};
int main(void)
{
Point p;
p.ReadPoint();
printf("%.6lf\n",p.GetPolarAngle());
return 0;
}Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
double a = con.nextDouble();
double b = con.nextDouble();
double res = Math.atan2(b,a);
if (res < 0) res += 2 * Math.PI;
System.out.println(res);
con.close();
}
} Python implementation
Read the input data.
import math
a, b = map(float,input().split())The polar angle of a point is computed using the atan2 function.
res = math.atan2(b,a)If the result of atan2 is negative, you should add to it, since the angle must lie within the interval [0; 2\pi).
if (res < 0) :
res += 2 * math.piPrint the answer.
print("%.6f" %res)