UVA 11096 || Nails (计算凸包周长

来源:互联网 发布:电子教室软件价格 编辑:程序博客网 时间:2024/06/05 00:23

发现写计算几何写久了真的非常烦躁,明明就是小错误一直找很久。

先算出凸包,就裸题了,然后求一下周长就好了。

#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;#define FOR(i,m,n) for( int i=m; i<n; ++i)double const epx = 1e-8;int dcmp( double x){    if( fabs(x) < epx)        return 0;    return x < 0 ?-1:1;}struct node{    double x,y,d;    node(double a,double b):x(a),y(b){}    node(){}};typedef node vec;vec operator - ( node a,node b){    return vec( a.x-b.x,a.y-b.y);}node point[ 110 ];double length( node a,node b){    vec A = a - b;    return sqrt(A.x*A.x + A.y*A.y);}//叉乘ab*acdouble crosspruduct(node a,node b,node c){    vec AB = b - a;    vec AC = c - a;    return AB.x * AC.y - AB.y * AC.x ;}//坐标排序bool idcmp(node a ,node b){    return ( a.x == b.x )? ( a.y < b.y ): ( a.x < b.x );}//以选定的第一点与剩下的点连线进行级角排序bool anglecmp( node a,node b){    double cp = crosspruduct( point[0],a,b);    if( dcmp(cp) == 0 )       return  a.d < b.d;//级角相等距离由近到远    return cp > 0;//顺时针排序}double graham( int n){    if( n > 1 )    {        sort( point,point+n,idcmp);//坐标排序        FOR(i,1,n)            point[i].d = length(point[0],point[i]);//距离        sort(point+1,point+n,anglecmp);//级角排序    }    int top = n-1;//栈顶    if( n > 2 )//graham 最少三个顶点    {        top = 1;        for( int i = 2;i <n ;++i)        {            while( top > 0                  &&crosspruduct( point[ top-1],point[top],point[i])<0)                    --top;            //栈顶两点与第i点不构成向左拐关系 栈顶回溯至满足条件            point[++top] = point[i];        }    }    point[++top] = point[0];//封闭    double sum = 0.0;    FOR(i,0,top)    {        sum += length( point[i],point[i+1]);    }    return sum ;}int main(){    int cas,len,num;    cin>>cas;    while( cas-- )    {        cin>>len>>num;        FOR(i,0,num)        {            scanf("%lf %lf",&point[i].x,&point[i].y);        }        double ans = graham(num);        if( ans < len )            ans = (double)len ;        printf("%.5lf\n",ans);    }    return 0;}


0 0
原创粉丝点击