POJ 2007 Scrambled Polygon 极角排序

题意:

给你一些点,这些点里面一定有(0,0)原点,并且没有第二象限中的点,这些点是一个凸包,让你按极角序输出这些点。

就排序,比较水,没凸包真扫兴

View Code
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

#define eps 1e-8
#define inf 1<<29

struct point
{
    int x, y;
}p[52];

int det(int x1, int y1, int x2, int y2)
{
    return x1 * y2 - x2 * y1;
}

int cross(point o, point a, point b)
{
    return det(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}

double f(int x)
{
    return x * x * 1.0;
}

double dis(point a, point b)
{
    return sqrt( f(a.x - b.x) + f(a.y - b.y) );
}

bool cmp(point a, point b)
{
    int tmp = cross(p[0], a, b);
    if(tmp > 0) return 1;
    if(tmp == 0 && dis(p[0], a) < dis(p[0], b) )return 1;
    return 0;
}

int main()
{
    int i, j;
    int n = 0;
    //freopen("int.txt", "r", stdin);
    while( ~scanf("%d%d", &p[n].x, &p[n].y) )
    {
        if(p[n].y < p[0].y) swap(p[0], p[n]);
        else if(p[n].y == p[0].y && p[n].x < p[0].x) swap(p[0], p[n]);
        n++;
    }
    sort(p + 1, p + n, cmp);
    for(i = 0; i < n; i++)
        if(p[i].x == 0.0 && p[i].y == 0.0){ j = i; break; }
    for(i = j; i < n; i++)
        printf("(%d,%d)\n", p[i].x, p[i].y);
    for(i = 0; i < j; i++)
        printf("(%d,%d)\n", p[i].x, p[i].y);
}

 

posted @ 2012-09-01 15:37  To be an ACMan  Views(341)  Comments(0)    收藏  举报