P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

本文详细介绍了二维凸包算法的实现原理与代码实现,通过一个具体示例展示了如何使用C++进行快速排序和计算几何中关键步骤的处理,如极角排序、交叉积判断等。该算法在计算机图形学、地理信息系统等领域有广泛应用。

传送门

二维凸包的板子,抄yyb大佬的

//minamoto
#include<bits/stdc++.h>
#define rint register int
#define inf 0x3f3f3f3f
using namespace std;
const int N=10005;
struct node{double x,y;}p[N],st[N];
int n,k,top;double ans;
inline bool cmp(node a,node b){
    double A=atan2((a.y-p[1].y),(a.x-p[1].x));
    double B=atan2((b.y-p[1].y),(b.x-p[1].x));
    return A!=B?A<B:a.x<b.x;
}
inline double cross(node a,node b,node c){return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}
inline double dis(node a,node b){return sqrt((b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x));}
int main(){
//  freopen("testdata.in","r",stdin);
    scanf("%d",&n),p[0]={inf,inf};
    for(rint i=1;i<=n;++i){
        scanf("%lf%lf",&p[i].x,&p[i].y);
        if(p[0].y>p[i].y||(p[0].y==p[i].y&&p[i].x<p[0].x))p[0]=p[i],k=i;
    }
    swap(p[k],p[1]),sort(p+2,p+n+1,cmp);
    st[0]=p[1],st[1]=p[2],top=1;
    for(rint i=3;i<=n;++i){
        while(top&&cross(st[top-1],p[i],st[top])>=0)--top;
        st[++top]=p[i];
    }
    st[top+1]=p[1];for(rint i=0;i<=top;++i)ans+=dis(st[i],st[i+1]);
    printf("%.2lf\n",ans);return 0;
}

转载于:https://www.cnblogs.com/bztMinamoto/p/9989228.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值