• UVA 10652 Board Wrapping(凸包)


    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32286

    【思路】

           凸包      

           根据角度与中心点求出长方形所有点来,然后就可以应用凸包算法了。

    【代码】

    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    const double PI = acos(-1.0);
    double torad(double deg) { return deg/180 * PI; }                    //角度化弧度 
    
    struct Pt {
        double x,y;
        Pt(double x=0,double y=0):x(x),y(y) {};
    };
    typedef Pt vec;
    vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
    vec operator + (vec A,vec B) { return vec(A.x+B.x,A.y+B.y); }
    bool operator < (const Pt& a,const Pt& b) {
        return a.x<b.x || (a.x==b.x && a.y<b.y);
    }
    
    double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; }
    vec rotate(vec A,double rad) {
        return vec(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
    }
    
    int ConvexHull(Pt* p,int n,Pt* ch) {
        sort(p,p+n);
        int m=0;
        for(int i=0;i<n;i++) {
            while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;    //维护凸包 
            ch[m++]=p[i]; 
        }
        int k=m;
        for(int i=n-2;i>=0;i--) {
            while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
            ch[m++]=p[i]; 
        }
        if(n>1) m--;
        return m;
    }
    
    double PolygonArea(Pt* p,int n) {                                    //多边形面积 
        double S=0;
        for(int i=1;i<n-1;i++) 
            S += cross(p[i]-p[0],p[i+1]-p[0]);
        return S/2;
    }
    
    const int N = 2500+10;
    Pt P[N],ch[N];
    int n;
    
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            scanf("%d",&n);
            int pc=0; double S1=0;
            double x,y,w,h,j;
            for(int i=0;i<n;i++) {
                scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j);
                double ang=-torad(j);
                Pt o(x,y);
                P[pc++]= o + rotate(vec(-w/2,-h/2),ang);
                P[pc++]= o + rotate(vec(w/2,-h/2),ang);
                P[pc++]= o + rotate(vec(-w/2,h/2),ang);
                P[pc++]= o + rotate(vec(w/2,h/2),ang);
                S1 += w*h;
            }
            int m=ConvexHull(P,pc,ch);
            double S2=PolygonArea(ch,m);
            printf("%.1lf %%
    ",S1*100/S2);
        }
        return 0;
    }
  • 相关阅读:
    CentOS 7下搭建配置SVN服务器
    centos7 安装字体库
    redis 开机自启动
    Firewalls
    当安装某个扩展提示错误,显示版本冲突的时候,
    防盗链
    Telnet ping不通443的解决办法
    R处理xml文件
    解决load 函数无法赋予变量名的问题
    用Rprofile文件配置打开时R的设置
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5175550.html
Copyright © 2020-2023  润新知