UVA 10652 Board Wrapping(凸包求面积)

本文介绍了一种解决UVA10652问题的方法,该问题要求使用最小的凸多边形包裹一系列旋转矩形,并计算矩形区域占总面积的比例。文章详细介绍了如何通过旋转和平移操作确定矩形顶点坐标,进而求得凸包及面积。

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值