poj2007

极坐标排序

注意atan2(y,x)的使用方法,y在前,x在后。返回X轴正方向到原点到(x,y)点的射线的到角。

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
#include
<cmath>
#include
<algorithm>
using namespace std;

#define maxn 55
#define pi acos(-1)

struct Point
{
int x, y;
} point[maxn];

bool operator <(const Point &a, const Point &b)
{
return atan2(a.y, a.x) < atan2(b.y, b.x);
}

double cal(double a)
{
if (a < 0)
return a + 2 * pi;
return a;
}

int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d%d", &point[0].x, &point[0].y);
int n = 0;
while (scanf("%d%d", &point[n].x, &point[n].y) != EOF)
n
++;
sort(point, point
+ n);
double temp = 0;
point[n]
= point[0];
int s;
for (int i = 0; i < n; i++)
{
double a = cal(atan2(point[i + 1].y, point[i + 1].x) - atan2(point[i].y, point[i].x));
if (a > temp)
{
temp
= a;
s
= (i + 1) % n;
}
}
printf(
"(0,0)\n");
for (int i = 0; i < n; i++)
printf(
"(%d,%d)\n", point[(s + i) % n].x, point[(s + i) % n].y);
return 0;
}

 

posted @ 2011-09-02 09:12  金海峰  阅读(717)  评论(0编辑  收藏  举报