# Sorting points (x,y) according to the angle made with +ve x-axis

I am unable to identify error in my code
Question-Programming Problems and Competitions :: HackerRank
my code:

``````#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define PI 3.14

int main()
{
lli n;
cin>>n;
pair <  pair <float,float> , pair <lli,lli> > a[n];
float theta,dist;
for(lli i=0;i<n;i++)
{
cin>>a[i].second.first>>a[i].second.second;
if(a[i].second.second==0){
if(a[i].second.first>0) theta=0;
else theta=PI;
}
else if(a[i].second.first==0){
if(a[i].second.second>0) theta=(float)((float)(90*PI)/(float)180);
else theta=(float)((float)(270*PI)/(float)180);
}
else{
float temp=(float)((float)a[i].second.second/(float)a[i].second.first);
//cout<<(float)atan(temp)<<endl;
if(a[i].second.first>0){
if(a[i].second.second>0){
theta=(float)atan(temp);
}else{
theta=(float)((float)(2*PI)+(float)atan(temp));
}
}else{
if(a[i].second.second>0){
theta=(float)((float)PI+(float)atan(temp));
}else{
theta=(float)((float)PI+(float)atan(temp));
}
}
}

dist=(float)((float)(a[i].first.first*a[i].first.first)+(float)(a[i].first.second*a[i].first.second));
a[i].first.first=(float)theta;
a[i].first.second=(float)dist;
cout<<theta<<endl;
}
sort(a,a+n);
for(lli i=0;i<n;i++){
cout<<a[i].second.first<<" "<<a[i].second.second<<endl;
}
return 0;
}
``````

I think it might be the result of smaller precisionâ€¦ Use double insteadâ€¦By the way if I had that question I would have had solvedd it on the basis of co ordiantes

Hi, this part of code will work :

```  int N; // Number of points.
double dx, dy, temp;  //dx, dy are coordinate points.
vector < pair < double, pair < double, double > > > angles; // A vector which holds the angles, points.

for(LL n = 0; n < N; ++n){
scanf("%lf%lf", &dx, &dy);
temp = atan2(dy, dx);
angles.pb(make_pair(temp, make_pair(dx, dy));
}
sort(angles.begin(), angles.end()); // Now sorting the angles. Based on angle.
```

As a whole atan2(y, x) will take care of all the conversions. i.e it returns the angle made by the required point (x, y) with +X axis.

What is the Problem you are facing ? Is it a Wrong Answer ? Is it a compilation error ? Is it a Runtime Error ? Please specify that

Thanx a lot! Precision does matter.

Thank you.atan2() is a something new to learn for me.Better than the long code i wrote!
BTW my mistake was in calculating the distance.Thanks a lot!