POJ 2007 Scrambled Polygon 极角排序

极角排序即可

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;

const int MAXN=1000;
const double PI = acos(-1);

struct Point
{
    int x,y;
};

Point pot[MAXN];
int stck[MAXN],top;

int cross(Point p0,Point p1,Point p2) //计算叉积  p0p1 X p0p2
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}

double dis(Point p1,Point p2)  //计算 p1p2的 距离
{
    return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}

bool cmp(Point p1,Point p2) //极角排序函数,角度相同则距离小的在前面
{
    int tmp=cross(pot[0],p1,p2);
    if(tmp>0) return true;
    else if(tmp==0&&dis(pot[0],p1)<dis(pot[0],p2)) return true;
    else return false;
}

void init(int n) //输入,并把最左下方的点放在 pot[0]。并且进行极角排序
{
    int i,k;
    Point p0;
    p0 = pot[0];
    k=0;
    for(i=1; i<n; i++)
    {
        if( (p0.y>pot[i].y) || ((p0.y==pot[i].y)&&(p0.x>pot[i].x)) )
        {
            p0 = pot[i];
            k=i;
        }
    }
    pot[k]=pot[0];
    pot[0]=p0;
    sort(pot+1,pot+n,cmp);
}

void graham(int n)
{
    int i;
    if(n==1)
    {
        top=0;
        stck[0]=0;
    }
    if(n==2)
    {
        top=1;
        stck[0]=0;
        stck[1]=1;
    }
    if(n>2)
    {
        for(i=0; i<=1; i++) stck[i]=i;
        top=1;

        for(i=2; i<n; i++)
        {
            while(top>0&&cross(pot[stck[top-1]],pot[stck[top]],pot[i])<=0) top--;
            top++;
            stck[top]=i;
        }
    }
}

int main()
{
//    freopen("in.txt","r",stdin);
    int n = 0;
    while(~scanf("%d%d", &pot[n].x, &pot[n].y)) n++;
    init(n);
    int p;
    for(int i=0; i<n; i++)
        if(pot[i].x == 0 && pot[i].y == 0)
        {
            p = i;
            break;
        }
    for(int i=0;i<n;i++)
        printf("(%d,%d)
", pot[(p+i)%n].x, pot[(p+i)%n].y);
    return 0;
}
原文地址:https://www.cnblogs.com/pach/p/7289491.html