[笔记] 计算几何-凸包 POJ-3348 Cows

题目:http://poj.org/problem?id=3348

求凸包面积

算法:先对点的横坐标排序,从左到右先计算下凸边,再从右到左计算上凸边.复杂度比Graham Scan法稍稍要高(两次遍历点集),但实现较容易

#include <stdio.h>
#include <algorithm>
using namespace std;

struct point{
    double x,y;
};

point a[10005];
point res[10005];
bool cmp(point x,point y);
int clockwise(point pre,point now,point nxt);
double area(point o,point x,point y);

int main(){
    
    int n;
    while (scanf("%d",&n)!=EOF){
        
        for (int i=1;i<=n;i++){
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        sort(a+1,a+n+1,cmp);
        
        int num = 0;
        for (int i=1;i<=n;i++){
            while (num>=2&&!clockwise(res[num-1],res[num],a[i])) num--;
            res[++num] = a[i];
        }
        
        int k = num;
        for (int i=n-1;i>=1;i--){
            while (k+1<=num&&!clockwise(res[num-1],res[num],a[i])) num--;
            res[++num] = a[i];
        }
        num--;


        double sum = 0;

        for (int i=3;i<=num;i++){
            sum += area(res[1],res[i],res[i-1]);
        }
        
        printf("%d\n",(int)(sum/100));
    }
    
    return 0;
}

bool cmp(point x,point y){
    if (x.x<y.x) return true;
    else return false;
}

int clockwise(point pre,point now,point nxt){
    
    double x1 = now.x-pre.x;
    double y1 = now.y-pre.y;
    double x2 = nxt.x-now.x;
    double y2 = nxt.y-now.y;
    if (x1*y2-x2*y1>0) return 1;
    else return 0;
}

double area(point o,point x,point y){
    
    double x1 = x.x-o.x;
    double y1 = x.y-o.y;
    double x2 = y.x-o.x;
    double y2 = y.y-o.y;
    
    if (x1*y2-x2*y1>0) return x1*y2-x2*y1;
    else return ((-1.0)*(x1*y2-x2*y1));
}

 

转载于:https://www.cnblogs.com/phynotape/p/3911581.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值