Skip to content

Commit e966f24

Browse files
authored
Merge pull request #196 from pedroulissespu/master
Radial Sort Code in C+
2 parents 4abf46a + 3b54a92 commit e966f24

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

C++/radial_sort.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define type int
4+
#define point pair<type, type>
5+
#define X first
6+
#define Y second
7+
8+
point operator-(point a, point b)
9+
{
10+
return {a.X - b.X, a.Y - b.Y};
11+
}
12+
13+
type operator*(point a, point b)
14+
{
15+
return a.X * b.Y - a.Y * b.X;
16+
}
17+
18+
int n;
19+
vector<point> P;
20+
point R;
21+
22+
int dist(point a, point b)
23+
{
24+
return (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y);
25+
}
26+
27+
bool cmp(point a, point b)
28+
{
29+
if((a - R).Y * (b - R).Y <= 0) return a.Y > R.Y;
30+
int c = (a - R) * (b - R);
31+
if(c == 0) return dist(R, a) <= dist(R, b);
32+
return c > 0;
33+
}
34+
35+
int main()
36+
{
37+
cin >> n >> R.X >> R.Y;
38+
for(int i = 0; i < n; i++)
39+
{
40+
type x, y;
41+
cin >> x >> y;
42+
P.push_back({x, y});
43+
}
44+
sort(P.begin(), P.end(), cmp);
45+
for(point p : P) cout << p.X << ' ' << p.Y << '\n';
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)