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 %%\n",S1*100/S2);
    }
    return 0;
}

 

你可能感兴趣的:(UVA 10652 Board Wrapping(凸包))