Scrambled Polygon POJ - 2007 极角排序

题意:

给你n个点,这n个点可以构成一个多边形(但是不是按顺序给你的)。原点(0,0)为起点,让你按顺序逆序输出所有点

 

题解:

就是凸包问题的极角排序

用double一直Wa,改了int就可以了

 

//原点(0,0)为起点,逆序输出多边形的点
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<math.h> 
using namespace std;
const int maxn=1e3+10;
const double PI=acos(-1.0);  //180度的弧度制
const double eps=1e-6;
struct Cpoint
{
    int x,y;
    Cpoint(){}
    Cpoint(int xx,int yy):x(xx),y(yy){}
    Cpoint friend operator -(Cpoint a,Cpoint b)
    {
        return Cpoint(a.x-b.x,a.y-b.y);
    }
    int friend operator ^(Cpoint a,Cpoint b)
    {
        return a.x*b.y-b.x*a.y;
    }
    bool friend operator <(Cpoint a,Cpoint b)
    {
        if(a.y==b.y) return a.x<b.x;
        return a.y<b.y;
    }
}point[maxn],init_point;
int dist(Cpoint a,Cpoint b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int Sign(double x)
{
    if(x>=-eps && x<=eps) return 0;
    if(x>eps) return 1;
    else return -1;
}
bool cmp(Cpoint a,Cpoint b)  //积角排序
{
    int s=(a-init_point)^(b-init_point); //若s>0,那么b在a点的左侧,所以a点先遍历到
    if(s>0 || (s==0 && dist(a,init_point)<dist(b,init_point))) return 1;
    else return 0;
}
int n;
int main()
{
    int x,y;
    scanf("%lf%lf",&x,&y);
    init_point.x=0;
    init_point.y=0;
    while(~scanf("%d%d",&x,&y))
    {
        point[n].x=x;
        point[n].y=y;
        n++;
    }
    sort(point,point+n,cmp);
    printf("(0,0)\n");
    for(int i=0;i<n;++i)
    {
        printf("(%d,%d)\n",point[i].x,point[i].y);
    }
    return 0;
}

 

posted @ 2020-12-01 21:36  kongbursi  阅读(67)  评论(0编辑  收藏  举报