题目大意:给出平面上许多矩形的中心点和倾斜角度,计算这些矩形面积占这个矩形点形成的最大凸包的面积比。
算法:GRAHAM,ANDREW。
题目非常的简单,就是裸的凸包 + 点旋转。这题自己不会的地方就是点旋转,另外,Andrew 算法还没有调试出来 。有一个非常细节的地方:给出的矩形有N个,但是点有4*N个。
直接上代码:GRAHAM
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath> using namespace std; int N,pc,top;
double Tot_s = ,area = ; struct data{
double x,y; data(double x=,double y=):x(x),y(y) {}
};
data pt[],st[];
double cross(data a,data b,data td){
return (a.x-td.x)*(b.y-td.y) - (a.y-td.y)*(b.x-td.x);
}
double dis(data a,data b){
return sqrt(pow(a.x-b.x,)+pow(a.y-b.y,));
}
bool cmp(data a,data b){
if(cross(a,b,pt[]) == )
return dis(a,pt[]) < dis(b,pt[]);
return cross(a,b,pt[]) > ;
}
data Rotate(data a,double rad){
return data(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
data operator + (data a,data b){
return data(a.x+b.x,a.y+b.y);
}
double torad(double deg){
return deg/*acos(-);//acos(-1) = PI
}
void Graham(){
int k = ;data tp;
top = ;
for(int i = ;i < *N;++ i)
if(pt[k].x>pt[i].x || (pt[k].x==pt[i].x&&pt[k].y>pt[i].y))
k = i;
tp = pt[k];pt[k] = pt[];pt[] = tp;
sort(pt+,pt+*N,cmp);
st[] = pt[];st[] = pt[];st[] = pt[];
for(int i = ;i < *N;++ i){
while(top && cross(pt[i],st[top],st[top-]) >= )
top --;
st[++ top] = pt[i];
}
st[++ top] = pt[];
for(int i = ;i < top;++ i)
Tot_s += cross(st[],st[i],st[i+]);
Tot_s /= ;
}
void init(){
scanf("%d",&N);
for(int i = ;i < N;++ i){
double x,y,w,h,j,ang; scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j); data cen(x,y);
ang = -torad(j);
pt[pc ++] = cen + Rotate(data(-w/,-h/),ang);
pt[pc ++] = cen + Rotate(data(w/,h/),ang);
pt[pc ++] = cen + Rotate(data(-w/,h/),ang);
pt[pc ++] = cen + Rotate(data(w/,-h/),ang);
area += w*h;
}
Graham();
pc = ; } int main(){
int tcase;
scanf("%d",&tcase);
while(tcase --){
init();
printf("%.1lf ",area*/Tot_s);
cout << "%" << endl;
area = ;Tot_s = ;
} return ;
}