UVA 10652 Board Wrapping (凸包面积)



\


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, j where 0 <= x, y, w, h <=10000 and –90° < j <=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. j is the angle between the height axis of the board to the y-axis in degrees, positive clockwise. That is, if j = 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 (%).


Sample Input                                           Output for 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

                                      64.3 %



题意:有n个木板,用面积尽量小的凸多边形把他们包起来,并计算出木板占整个包装面积的百分比。第一行一个数据组数T,每组数据第一行一个木板个数n,以下n行每行5个实数 x, y,  w, h, j  (0<=x,y,w,h<=10000     -90<=j<=90) ,其中(x,y)是木板中心的坐标,w是宽,h是高,j是顺时针旋转的角度(j=0表示不旋转,此时长度为w的那条边应该是水平方向)。木板保证互不相交。对于每组数据输出木板总面积占包装面积的百分比,保留小数点后1位


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
using namespace std;
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
} ;
bool operator < (const Point&a,const Point&b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
Point operator+(Point A,Point B)
{
    return Point(A.x+B.x,A.y+B.y);
}
Point operator-(Point A,Point B)
{
    return Point(A.x-B.x,A.y-B.y);
}
double torad(double deg)//角度转换为弧度
{
    return deg/180*acos(-1);
}
Point Rotate(Point A,double rad)//向量旋转
{
    return Point(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
double Cross(Point A,Point B)
{
    return A.x*B.y-A.y*B.x;
}
double PolygonArea(Point *p,int n)
{
    double area=0;
    for(int i=1;i<n-1;i++)
        area=area+Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2;
}
int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n);
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=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-2])<=0)m--;
        ch[m++]=p[i];
    }
    if(n>1)m--;
    return m;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int N,t=0;
        Point P[2500],ch[2500];
        cin>>N;
        double area=0;
        for(int i=0;i<N;i++)
        {
            double x,y,w,h,j;
            cin>>x>>y>>w>>h>>j;
            Point o(x,y);
            double ang=-torad(j);//顺时针旋转
            P[t++]=o+Rotate(Point(-w/2,-h/2),ang); //求所给的长方形木板的四个顶点
            P[t++]=o+Rotate(Point(w/2,-h/2),ang);
            P[t++]=o+Rotate(Point(-w/2,h/2),ang);
            P[t++]=o+Rotate(Point(w/2,h/2),ang);
            area=area+w*h;//累加木板总面积
        }
        int m=ConvexHull(P,t,ch);
        double area2=PolygonArea(ch,m);
        cout<<setiosflags(ios::fixed)<<setprecision(1);
        cout<<area*100/area2<<" %"<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/MisdomTianYa/p/6581759.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值