2129. Polar angle of a point

 

Find the polar angle of the point.

 

Input. Two integers are given – the decart coordinates of the point that does not coincide with the origin. Input values do not exceed 10000 by the absolute value.

 

Output. Print one real number – the value of its polar angle in radians in the interval [0; 2π). The answer must be rounded to 6 digits after the decimal point.

 

Sample input

Sample output

2 3

0.982794

 

 

SOLUTION

geometry

 

Algorithm analysis

The polar coordinate system is a two-dimensional coordinate system in which each point on a plane is uniquely determined by two numbers: a polar angle φ (which is also called an azimuth or phase angle) and a polar radius r. The r coordinate is the distance from the point to the center, or pole of the coordinate system, and the φ coordinate is the angle measured counterclockwise from the ray at 0 ° (sometimes called the polar axis of the coordinate system).

                         

Let point P have cartesian coordinates (x, y) and polar coordinates (r, φ). Then the transformation formulas from the polar coordinate system to the cartesian one have the form:

Function atan2(double y, double x) computes the arctangent of y / x in the range (-π; π]. If x = 0, or both parameters are zero, then the function returns 0. In this problem the polar angle should be printed in the interval [0; 2π). To do this, in the case of a negative value of atan2 function, add 2π to the result.

 

Algorithm realization

Compute the polar angle of a point using function atan2. If the result of atan2 function is negative, then 2π should be added to it, since the answer must be in the interval [0; 2π).

 

scanf("%lf %lf",&a,&b);

res = atan2(b,a);

if (res < 0) res += 2*PI;

printf("%.6lf\n",res);

 

Algorithm realization – 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 realization

 

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 realization

 

import math

a, b = map(float,input().split())

res = math.atan2(b,a)

if (res < 0) :

  res += 2 * math.pi

print("%.6f" %res)