POJ 3348 求凸包面积

题意 : 就是让你求出凸包面积然后除以50

题解 :

  1. 凸包的求法
  2. 面积的求法
    说一下面积的求法 : 和凸包的排序方式一样然后逆时针扫一遍凸包把所有的面积全部加起来就可以得到答案了。 (一定要先固定一个极点然后按照极角排序就可以了)
    ps : 计算几何输入一定要用scanf;
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>

using namespace std;
const double eps = 1e-8;
const int maxn = 1e4 + 10;
const double INF = 1e15 + 7;

struct point {
    double x,y;
}p[maxn],tu[maxn];

point init;
int n;
double dist (point a,point b) {
    return sqrt ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double cross (double x1,double y1,double x2,double y2) {
    return x1 * y2 - x2 * y1;
}

bool cmp (point a,point b) {
    if (fabs(cross(a.x - init.x, a.y - init.y, b.x - init.x, b.y - init.y)) < eps) {
        return dist(a, init) < dist(b, init);
    }
    else
        return cross(a.x - init.x, a.y - init.y, b.x - init.x, b.y - init.y) > 0;
}


int main () {
    ios_base :: sync_with_stdio(false);
    while (scanf ("%d",&n) != EOF) {
        double mix = 0,miy = INF;
        int pos = 1;
        for (int i = 1;i <= n; ++ i) {
            scanf ("%lf%lf",&p[i].x,&p[i].y);
            if (miy > p[i].y) {
                miy = p[i].y;
                pos = i;
                mix = p[i].x;
            }
            else if (fabs (miy - p[i].y) < eps) {
                if (mix > p[i].x) {
                    mix = p[i].x;
                    pos = i;
                }
            }
        }
        swap (p[1],p[pos]);
        init = p[1];
        sort (p + 2,p + n + 1,cmp);
        tu[1] = p[1],tu[2] = p[2],tu[3] = p[3];
        int tot = 3;
        for (int i = 4;i <= n; ++ i) {
            while (tot >= 3) {
                point p1,p2,p3;
                p1 = tu[tot];
                p2 = tu[tot - 1];
                p3 = p[i];
                if (cross(p1.x - p2.x,p1.y - p2.y , p3.x-p1.x, p3.y - p1.y) < 0) tot --;
                else break;
            }
            tu[++tot] = p[i];
        }
        init = tu[1];
        sort (tu + 2,tu + tot + 1,cmp);
        double res = 0;
        for (int i = 2;i < tot; ++ i) {
            if (i != tot) {
                res += cross(tu[i].x - p[1].x, tu[i].y - p[1].y, tu[i + 1].x - p[1].x, tu[i + 1].y - p[1].y);
            }
        }
        int ans = floor(fabs (res) / 100);
        printf ("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值