poj-1113-Wall

求凸包的问题。

做法:

先求出所有点构成的凸包的周长。

再加上半径为l的圆的周长~

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = 3.14159265358979;
struct point
{
    double x,y;
    double thera;
}a[1005],chs[1005];

bool cmp1(point a,point b)
{
    if( a.y == b.y ) return a.x < b.x;
    return a.y < b.y;
}
bool cmp2(point a,point b)
{
    if( a.thera == b.thera )
        return a.x < b.x;
    else
        return a.thera < b.thera;
}
double get_thera(point a0,point a1)
{
    return atan2((a1.y-a0.y),(a1.x-a0.x));
}
double dist_2point(point a1,point a2)
{
    return sqrt((a1.x-a2.x)*(a1.x-a2.x)+(a1.y-a2.y)*(a1.y-a2.y));
}

bool line_3point(point a1,point a2,point a3)
{
    double P,Q,M;
    P=(a2.x - a1.x) * (a3.y - a1.y);
    Q=(a3.x - a1.x) * (a2.y - a1.y);
    M=P-Q;
    if(M>0)  return false;
    if(M<0)  return true;
    if(M==0) return true;
	return false;
}

int main()
{
    int i,n,top;
	double r,len,shan,sum;
    while(scanf("%d%lf",&n,&r)!=EOF)
    {
        len=0;
        for(i=0;i<n;i++)
            scanf("%lf%lf",&a[i].x,&a[i].y);
		sort(a,a+n,cmp1);
		for(i=0;i<n;i++)
		    a[i].thera=get_thera(a[0],a[i]);
		sort(a+1,a+n,cmp2);
        chs[0]=a[0]; chs[1]=a[1]; chs[2]=a[2];
        top=2;
        for(i=3;i<n;i++)
        {
            while(top>=1 && line_3point(chs[top-1],chs[top],a[i]))
            {
                top--;
            }
            chs[++top]=a[i];
        }
        top++;
        for(i=0;i<top;i++)
            len+=dist_2point(chs[i%top],chs[(i+1)%top]);

        shan=2.0*PI*r;
        sum=shan+len;
		printf("%.0lf\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值