`
阿尔萨斯
  • 浏览: 4209174 次
社区版块
存档分类
最新评论

UVA 10652 Board Wrapping(凸包求面积)

 
阅读更多

UVA 10652 Board Wrapping(凸包求面积)

题意:

有n块矩形木板,你的任务是用一个面积尽量小的凸多边形把它们包起来,并计算木板占整个包装面积的百分比.

分析: 刘汝佳<<训练指南>> P272例题6

给出了每个木板的中心和长,宽以及旋转角度,通过先旋转向量然后把中心点平移对应的向量可以求出矩形的4个顶点坐标.

然后我们根据矩形的所有顶点求出凸包,并求出凸包的面积.(即总面积)

最后用所有矩形的面积和/总面积就是百分比了.

AC代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const double PI=acos(-1);
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator<(const Point &B)const
    {
        return dcmp(x-B.x)<0 || (dcmp(x-B.x)==0 && dcmp(y-B.y)<0 );
    }
    bool operator==(const Point &B)const
    {
        return dcmp(x-B.x)==0 && dcmp(y-B.y)==0;
    }
};
typedef Point Vector;

Vector operator-(Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}
Vector operator+(Vector A,Vector B)
{
    return Vector(A.x+B.x,A.y+B.y);
}
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
Vector Rotate(Vector A,double rad)
{
    return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
double PolygonArea(Point *p,int n)
{
    double area=0;
    for(int i=1;i<n-1;++i)
        area += Cross(p[i]-p[0],p[i+1]-p[0]);
    return fabs(area)/2;
}
int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n);
    n=unique(p,p+n)-p;
    int m=0;
    for(int i=0;i<n;++i)
    {
        while(m>1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-1])<=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-1])<=0) --m;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}

const int maxn=600*4+10;
Point p[maxn];
int cnt;//点数目

int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        cnt=0;          //当前点数目
        double area1=0;//矩形面积和
        while(n--)
        {
            double x,y,w,h,j;
            scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j);
            area1 += w*h;
            Point o(x,y);           //矩形中心点
            double rad=-(j/180*PI); //角度转弧度
            p[cnt++]=o+Rotate(Vector(w/2,h/2), rad);
            p[cnt++]=o+Rotate(Vector(-w/2,h/2), rad);
            p[cnt++]=o+Rotate(Vector(w/2,-h/2), rad);
            p[cnt++]=o+Rotate(Vector(-w/2,-h/2), rad);
        }
        Point q[maxn];
        cnt=ConvexHull(p,cnt,q);
        double area2=PolygonArea(q,cnt);//凸包总面积
        printf("%.1lf %%\n",area1*100/area2);
    }
    return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics