凸包模板

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
#define eps 1e-8
#define PI acos(0.0)*2.0
const int maxn=200005;

struct point{
	double x,y;
}p[maxn],res[maxn];
double dis(point a,point b)
{
	return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
bool xmult(point p1,point p2,point p0)//cross product(p1-p0)X(p2-p0)
{
	if(((p1.x-p0.x)*(p2.y-p0.y) >= (p2.x-p0.x)*(p1.y-p0.y)))
		return false;
	return true;
}
bool operator < (const point &l,const point &r)
{
	return l.y<r.y || (l.y==r.y && l.x<r.x);
}
int graham(point pnt[],int n)//求凸包及凸包内点的数目
{
	int i,len,top=1;
	sort(pnt,pnt+n);
	if(n==0) return 0;res[0] = pnt[0];
	if(n==1) return 1;res[1] = pnt[1];
	if(n==2) return 2;res[2] = pnt[2];
	for (i = 2; i < n; i++)	{
		while(top &&xmult(pnt[i],res[top],res[top-1]))
			top--;
		res[++top] = pnt[i];
	}
	len = top;
	res[++top] = pnt[n-2];
	for (int i = n-3; i >= 0 ; i--)
	{
		while(top!=len &&xmult(pnt[i],res[top],res[top-1]))
			top--;
		res[++top] = pnt[i];
	}
	return top;
}
double cir_polygon(int n,point *p)//circumference
{
	double cir = 0;
	int i;
	for (i = 0; i < n; i++)
		cir += dis(p[i],p[(i+1)%n]);
	return cir;
}
double area_polygon(int n,point *p)//area
{
	double s1=0,s2=0;
	int i;
	for (i = 0; i < n; i++)
	{
		s1+=p[(i+1)%n].y*p[i].x;
		s2+=p[(i+1)%n].y*p[(i+2)%n].x;
	}
	return fabs(s1-s2)/2;
}
int main()
{
	int n,top;
	double r;
	while(cin>>n){
		double ans = 0;
		for (int i = 0; i < n; i++)
			cin>>p[i].x>>p[i].y;
		top = graham(p,n);
		ans += cir_polygon(top,res);
		cout<<setiosflags(ios::fixed)<<setprecision(2)<<ans<<endl;
	}
	return 0;
}

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;

const double eps = 1e-8;
const double inf = 10000000000.0;
int stk[100001], top;

struct Point {
    double x, y;
} p[100001];

int dblcmp(double k) {
    if (fabs(k) < eps) return 0;
    return k > 0 ? 1 : -1;
}

double multi(Point p0, Point p1, Point p2) {
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}

double getDis(Point a, Point b) {
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

bool cmp(const Point& a, const Point& b) {
    int d = dblcmp(multi(p[0], a, b));
    if (!d) return getDis(p[0], a) < getDis(p[0], b);
    return d > 0;
}

int main()
{
    //freopen("凸边形外壳.in","r",stdin);
    int n, i, k, t;
    double tx, ty;
    cin >> t;
    while (t --) {
        memset(stk,0,sizeof(stk));
        scanf("%d", &n);
        tx = ty = inf;
        for (i = 0; i < n; i++) {
            scanf ("%lf%lf", &p[i].x, &p[i].y);
            int d = dblcmp(ty-p[i].y);
            if (!d && dblcmp(tx-p[i].x) > 0) {
               k = i; tx = p[i].x;
            } else if (d > 0) {
               k = i;
               ty = p[i].y;
               tx = p[i].x;
            }
        }
        if (n <= 2) {
            printf ("0.0\n");
            continue;
        }
        p[k].x = p[0].x;
        p[k].y = p[0].y;
        p[0].x = tx;
        p[0].y = ty;

        sort(p+1, p+n, cmp);
        stk[0] = 0;
        stk[1] = 1;
        top = 1;
        for (i = 2; i < n; i++) {
            while (top >= 1 && dblcmp(multi(p[stk[top-1]], p[i], p[stk[top]])) >= 0) top--;
            stk[++top] = i;
        }
        double area = 0;
        for (i = 1; i < top; i++)
            area += fabs(multi(p[stk[0]], p[stk[i]], p[stk[i+1]]));
        printf ("%.1f\n", area/2);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值