POJ 2007 极角排序

本文介绍了一种基于极角排序的凸包算法实现,该算法通过逆序Graham-scan来确定点集的凸包。代码示例中详细展示了如何通过比较向量极角大小来对点进行排序,并最终输出凸包顶点。

题目扯了那么多,又是算斜率, 又是不同象限的,最后就是一个极角排序
注意,这里的极角排序是一般grahm-scan算法的逆序即可

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
const int maxn=55;
const double eps=1e-8;
using namespace std;
int sgn(double x){
    if(fabs(x)<eps)
        return 0;
    if(x>0)
        return 1;
    if(x<0)
        return -1;
}
struct point{
    double x,y;
    //double slope=0;
    point(){}
    point(double _x,double _y){
        x=_x;
        y=_y;
    }
};
typedef point vector;
vector operator - (vector a,vector b){
    return point(a.x-b.x,a.y-b.y);
}
double operator * (vector a,vector b){
    return a.x*b.x+a.y*b.y;
}
double operator ^ (vector a,vector b){
    return a.x*b.y-a.y*b.x;
}
bool operator < (vector a,vector b){
    if(a.x==b.x &&a.y<b.y ||a.x<b.x)
        return true;
    return false;
}
double dis(point a,point b){
    return hypot(a.x-b.x,a.y-b.y);
}
struct convex{
    int n;
    point p[maxn];
};
point p[maxn];
bool cmp(point a,point b){
    int d=sgn((a-p[0])^(b-p[0]));
    if(d<0 || d==0 && sgn(dis(a,p[0])-dis(b,p[0]))<0 )//d<0 逆序
       return true;
    return false;
}
int main(){
    //freopen("input.txt","r",stdin);
    int i=0;
    while(~scanf("%lf%lf",&p[i].x,&p[i].y))
        i++;
    sort(p,p+i,cmp);
    printf("(%d,%d)\n",int(p[0].x),int(p[0].y));
    for(int j=i-1;j>0;j--)
        printf("(%d,%d)\n",int(p[j].x),int(p[j].y));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值