UVA 10652 Board Wrapping(凸包+向量旋转)

来源:互联网 发布:鞑靼牛肉 知乎 编辑:程序博客网 时间:2024/06/04 20:08

UVA 10652 Board Wrapping(凸包)

The small sawmill in Mission, British Columbia, has
developed a brand new way of packaging boards for
drying. By fixating the boards in special moulds, the
board can dry efficiently in a drying room.
Space is an issue though. The boards cannot be
too close, because then the drying will be too slow.
On the other hand, one wants to use the drying room
efficiently.
Looking at it from a 2-D perspective, your task is
to calculate the fraction between the space occupied by
the boards to the total space occupied by the mould.
Now, the mould is surrounded by an aluminium frame
of negligible thickness, following the hull of the boards’
corners tightly. The space occupied by the mould
would thus be the interior of the frame.
Input
On the first line of input there is one integer, N ≤ 50,
giving the number of test cases (moulds) in the input. After this line, N test cases follow. Each test case
starts with a line containing one integer n, 1 < n ≤ 600, which is the number of boards in the mould.
Then n lines follow, each with five floating point numbers x, y, w, h, ϕ where 0 ≤ x, y, w, h ≤ 10000
and −90◦ < ϕ ≤ 90◦
. The x and y are the coordinates of the center of the board and w and h are the
width and height of the board, respectively. ϕ is the angle between the height axis of the board to the
y-axis in degrees, positive clockwise. That is, if ϕ = 0, the projection of the board on the x-axis would
be w. Of course, the boards cannot intersect.
Output
For every test case, output one line containing the fraction of the space occupied by the boards to the
total space in percent. Your output should have one decimal digit and be followed by a space and a
percent sign (‘%’).
Note: The Sample Input and Sample Output corresponds to the given picture
Sample Input
1
4
4 7.5 6 3 0
8 11.5 6 3 0
9.5 6 6 3 90
4.5 3 4.4721 2.2361 26.565
Sample Output
64.3 %

题意:

给定N个矩形的长和宽,给定中心坐标,求其总矩形面积和凸包面积之比
先把矩形的4个点找出来,用向量旋转来求,具体可以参考复平面的两点转换公式,实部看作x,虚部去掉i看成y即可(x+iy)*(cos(zeta)+isin(zeta))
然后就是先求上凸包再求下凸包
再求一次(凸包)多边形面积

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespace std;const double eps=1e-10,pi=acos(-1.0);const int maxn=100+5;struct point{  double x,y;  point(double x=0,double y=0):x(x),y(y){};  void IN(){scanf("%lf%lf",&x,&y);}};int dcmp(double x){if(fabs(x)<eps)return 0;return x>0?1:-1;}point operator + (const point &a,const point &b){return point(a.x+b.x,a.y+b.y);}point operator - (const point &a,const point &b){return point(a.x-b.x,a.y-b.y);}point operator * (const point &a,double t){return point(a.x*t,a.y*t);}point operator / (const point &a,double t){return point(a.x/t,a.y/t);}double operator * (const point &a,const point &b){return a.x*b.x+a.y*b.y;}double operator ^ (const point &a,const point &b){return a.x*b.y-b.x*a.y;} double Len(const point &a){return sqrt(a*a);}double Angle(const point &a,const point &b){return acos(a*b/Len(a)/Len(b));}point rotate(const point &a,double rad){return point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}bool operator <(const point &a,const point &b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}bool operator ==(const point &a,const point &b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}struct Line{  point st,ed;  Line(){};  Line(point a,point b):st(a),ed(b){}  void IN(){st.IN();ed.IN();}};bool isIntersected(Line a,Line b){  return (min(a.st.x,a.ed.x)<=max(b.st.x,b.ed.x))&&         (min(b.st.x,b.ed.x)<=max(a.st.x,a.ed.x))&&         (min(a.st.y,a.ed.y)<=max(b.st.y,b.ed.y))&&         (min(b.st.y,b.ed.y)<=max(a.st.y,a.ed.y))&&         (dcmp((a.st-a.ed)^(b.st-a.st))*dcmp((a.st-a.ed)^(b.ed-a.st))<=0)&&         (dcmp((b.st-b.ed)^(a.st-b.st))*dcmp((b.st-b.ed)^(a.ed-b.st))<=0);}double getS(point *a,int n){  double ans=0;  for(int i=2;i<=n;++i)ans+=(a[i]-a[0])^(a[i-1]-a[0]);  return fabs(ans)/2.0;}point Q[maxn*25];int convex_hull(point *a,int n){  sort(a+1,a+1+n);  int m=0;  for(int i=1;i<=n;++i)  {    while(m>1&&dcmp((Q[m]-Q[m-1])^(a[i]-Q[m-1]))<=0)m--;    Q[++m]=a[i];  }  int k=m;  for(int i=n-1;i>0;--i)  {    while(m>k&&dcmp((Q[m]-Q[m-1])^(a[i]-Q[m-1]))<=0)m--;    Q[++m]=a[i];  }  if(n>1)m--;    return m;}int n;double x,y,w,h,j;point p[25*maxn];int main(){  int T;  scanf("%d",&T);   while(T--)  {    scanf("%d",&n);    int cnt=0;    double ans1=0;    for(int i=1;i<=n;++i)    {      scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j);      double rad=-j*pi/180;      p[++cnt]=point(x,y)+rotate(point( w/2, h/2),rad);      p[++cnt]=point(x,y)+rotate(point(-w/2, h/2),rad);      p[++cnt]=point(x,y)+rotate(point( w/2,-h/2),rad);      p[++cnt]=point(x,y)+rotate(point(-w/2,-h/2),rad);      ans1+=w*h;    }    int m=convex_hull(p,cnt);    double ans2=getS(Q,m+1);    printf("%.1f %%\n",ans1*100.0/ans2);  }  return 0;}
原创粉丝点击