andrew算法

#include <cstdio>
#include <algorithm>
#define maxn 120005
#define eps 1e-8
using namespace std;
int n;
struct point{
    double x, y;
    point(){}
    point(double a, double b):x(a),y(b){}
    point operator - (const point &b) const
    {
        return  point(x-b.x, y-b.y);
    }
    bool operator < (const point &b) const
    {
        return x<b.x||(x == b.x && y < b.y);
    }

}p[maxn], res[maxn], pb[maxn];
int dcmp(double x){
    return (x>eps) - (x < -eps);
}
double cross(point a, point b){
    return a.x*b.y-b.x*a.y;
}
int andrew(){
    sort(p, p+n);
    int m = 0, i;
    for(i = 0; i < n; i++){
        while(m>1 && cross(res[m-1]-res[m-2], p[i]-res[m-2]) < 0)
            m--;
        res[m++] = p[i];
    }
    int k = m;
    for(i = n-2 ; i >= 0; i--){
        while(m>k && cross(res[m-1]-res[m-2], p[i]-res[m-2]) < 0)
            m--;
        res[m++] = p[i];
    }
    if(m>1) m--;
    return m;
}
int main(){
    int na, nb, i;
    scanf("%d", &na, &nb);
    for(i = 0; i < na; i++){
        scanf("%lf %lf", &p[i].x, &p[i].y);
    }
    scanf("%d", &nb);
    n = na+nb;
    for(i = na; i < n; i++){
        scanf("%lf %lf", &p[i].x, &p[i].y);
        pb[i-na] = p[i];
    }
    int m = andrew();
    sort(pb, pb+nb);
    int tmp;
    int flag = 1;
    for(i = 0; i < m; i++){
        tmp = lower_bound(pb, pb+nb, res[i]) - pb;
        if(dcmp(pb[tmp].x - res[i].x) == 0 && dcmp(pb[tmp].y - res[i].y)== 0)
        {
            flag = 0;
            break;
        }
    }
    if(flag) printf("YES");
    else printf("NO");
    return 0;
}

求凸包面积

double area(int m){
    double ans = 0;
    int i;
    for(i = 1; i < m-1; i++)
        ans += cross(res[i]-res[0], res[i+1]-res[0]);
    return fabs(ans/2);
}

求凸包周长

double length(int m){
    double ans = 0;
    int i;
    for(i = 0; i < m; i++)
        ans += dist(res[i], res[(i+1)%m]);
    return ans;
}
发布了52 篇原创文章 · 获赞 2 · 访问量 893

猜你喜欢

转载自blog.csdn.net/qq_44714572/article/details/97919275