-
Notifications
You must be signed in to change notification settings - Fork 0
/
polar-angles.cpp
81 lines (59 loc) · 1.18 KB
/
polar-angles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <limits.h>
#include <math.h>
#define PI 3.14159265
using namespace std;
class Point
{
public:
int x, y;
float degree, distance;
Point(int x, int y, float degree, float distance)
{
this->x = x;
this->y = y;
this->degree = degree;
this->distance = distance;
}
};
void polarAngels(vector<Point*> points)
{
int N = points.size();
for(int i=0; i<N-1; i++)
{
float min = i;
for(int j=i; j<N; j++)
{
if(points[j]->degree < points[min]->degree )
min = j;
else if(points[j]->degree == points[min]->degree && points[j]->distance < points[min]->distance)
min = j;
}
swap(points[i], points[min]);
}
for(int i=0; i<N; i++)
cout<<points[i]->x<<" "<<points[i]->y<<endl;
}
int main() {
int N;
cin>>N;
vector<Point*> allPoints;
for(int i=0; i<N; i++)
{
int x, y;
cin>> x >>y;
float degree, distance;
degree = atan2(y, x)*180/PI;
if(degree < 0)
degree += 360;
distance = sqrt(x*x + y*y);
Point* p = new Point(x, y, degree, distance);
allPoints.push_back(p);
}
polarAngels(allPoints);
return 0;
}