凸包&&旋转卡壳(andrew)

这篇博客介绍了计算几何中的上凸包和旋转卡壳的概念及求解方法。针对上凸包,文章阐述了如何通过点的坐标判断其是否属于上凸包,并给出了C++实现代码。对于旋转卡壳,强调了使用面积法的重要性,避免因点点间距离不连续导致错误,并提供了相应的优化算法和代码示例。
摘要由CSDN通过智能技术生成

题目链接
在这里插入图片描述
如图计算上凸包时判断 C 1 , C 2 , C 3 C_1,C_2,C_3 C1,C2,C3显然 C 3 C_3 C3是经过 A , B A,B A,B的上凸包
可以发现只需要通过 A , B A,B A,B即可判断,通过观察可以发现 C 3 y − A y C 3 x − A x < = B y − A y B x − A x \frac{C_{3y}-A_{y}}{C_{3x}-A_x}<=\frac{B_y-A_y}{B_x-A_x} C3xAxC3yAy<=BxAxByAy时为上凸包
( C 3 y − A y ) ∗ ( B x − A x ) − ( C 3 x − A x ) ∗ ( B y − A y ) < = 0 (C_{3y}-A_y)*(B_x-A_x)-(C_{3x}-A_x)*(B_y-A_y)<=0 (C3yAy)(BxAx)(C3xAx)(ByAy)<=0
但需注意有些数据需要去重之后相等的情况才可以算作弹出情况
计算下凸包时可利用上凸包的来算,因为可将q[t-1]作为相对原点的位置

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#define all(a) a.begin(),a.end()
using namespace std;
using ll = long long ;
struct node{
    double x,y;
    bool operator<(const node&p)const{
        return x==p.x?y<p.y:x<p.x;
    }
    node operator-(const node&p)const{
        return {x-p.x,y-p.y};
    }
    double operator*(const node&p)const{///cross
        return x*p.y-y*p.x;
    }
    bool operator==(const node&p)const{
        return x==p.x&&y==p.y;
    }
};
double dis(node &a,node &b){
    return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
bool vis[10010];
int q[10010];
signed main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vector<node>a(n);
    for(auto &[x,y]:a) cin>>x>>y;
    sort(all(a));
    //a.erase(unique(a.begin(),a.end()),a.end());
    int t=0;
    for(int i=0;i<a.size();i++){///up convex hull
        while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0) vis[q[t--]]=false;
        q[++t]=i;
        vis[i]=true;
    }
    vis[0]=false;
    for(int i=a.size()-1;i>=0;i--){
        if(vis[i])continue ;
        while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0)t--;
        q[++t]=i;
    }
    double ans=0;
    for(int i=1;i<t;i++)
        ans+=dis(a[q[i]],a[q[i+1]]);
    cout<<fixed<<setprecision(2)<<ans;
}

题目链接
计算旋转卡壳时必须采用面积法,不可采用点点间因为点点之间距离不一定是连续的
如一条平行线与圆相交有两个点时,距离可能会先减小后增大

while(abs(area(a[q[i]],a[q[i%t+1]],a[q[j]]))<abs(area(a[q[i]],a[q[i%t+1]],a[q[j%t+1]])))j=j%t+1;
        ans=max(ans,dis(a[q[i]],a[q[j]]));
        ans=max(ans,dis(a[q[i%t+1]],a[q[j]]));
        ans=max(ans,dis(a[q[i%t+1]],a[q[j%t+1]]));
        ans=max(ans,dis(a[q[i]],a[q[j%t+1]]));

此处需要四次取最大值前两次取最大值显而易见,后两次是当两个area相等时不代表dis(a[q[i]],a[q[j]])一定比dis(a[q[i%t+1]],a[j%t+1])大

#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
struct node{
    ll x,y;
    bool operator<(const node&p)const{
        return x==p.x?y<p.y:x<p.x;
    }
    node operator-(const node&p)const{
        return {x-p.x,y-p.y};
    }
    ll operator*(const node&p)const{
        return x*p.y-y*p.x;
    }
}a[100010];
ll dis(const node&a,const node&b){
    return pow(a.x-b.x,2)+pow(a.y-b.y,2);
}
int q[100010];
bool vis[100010];
ll area(const node&a,const node&b,const node&c){
    return (a-b)*(a-c);
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i].x>>a[i].y;
    sort(a,a+n);
    int t=0;
    for(int i=0;i<n;i++){
        while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0)vis[q[t--]]=false;
        q[++t]=i;
        vis[i]=true;
    }
    vis[0]=false;
    for(int i=n-1;i>=0;i--){
        if(vis[i])continue;
        while(t>1&&(a[q[t]]-a[q[t-1]])*(a[i]-a[q[t-1]])>0)t--;
        q[++t]=i;
    }
    ll ans=0;
    for(int i=1,j=1;i<=t;i++){
        while(abs(area(a[q[i]],a[q[i%t+1]],a[q[j]]))<abs(area(a[q[i]],a[q[i%t+1]],a[q[j%t+1]])))j=j%t+1;
        ans=max(ans,dis(a[q[i]],a[q[j]]));
        ans=max(ans,dis(a[q[i%t+1]],a[q[j]]));
        ans=max(ans,dis(a[q[i%t+1]],a[q[j%t+1]]));
        ans=max(ans,dis(a[q[i]],a[q[j%t+1]]));
    }
    cout<<ans;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倾海、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值