UVA 10652 Board Wrapping(凸包)

时间:2024-06-08 11:05:56

题目链接: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/ * PI; } //角度化弧度 struct Pt {
double x,y;
Pt(double x=,double y=):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=;
for(int i=;i<n;i++) {
while(m> && cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--; //维护凸包
ch[m++]=p[i];
}
int k=m;
for(int i=n-;i>=;i--) {
while(m>k && cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
if(n>) m--;
return m;
} double PolygonArea(Pt* p,int n) { //多边形面积
double S=;
for(int i=;i<n-;i++)
S += cross(p[i]-p[],p[i+]-p[]);
return S/;
} const int N = +;
Pt P[N],ch[N];
int n; int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
int pc=; double S1=;
double x,y,w,h,j;
for(int i=;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/,-h/),ang);
P[pc++]= o + rotate(vec(w/,-h/),ang);
P[pc++]= o + rotate(vec(-w/,h/),ang);
P[pc++]= o + rotate(vec(w/,h/),ang);
S1 += w*h;
}
int m=ConvexHull(P,pc,ch);
double S2=PolygonArea(ch,m);
printf("%.1lf %%\n",S1*/S2);
}
return ;
}