Board Wrapping UVA - 10652 凸包+向量旋转模板

题目链接:https://vjudge.net/problem/UVA-10652
白书P272
题意:给多个木板,每块木板用x,y,w,h,j描述。(x,y)为中心点,w,h为宽度和高度。j为顺时针旋转绕中心旋转的角度。用一个面积最小得凸多边形将所有木板包起来,求木板占多边形得面积。
思路:先处理木板的顶点,先用角度计算弧度,因为是顺时针需要加负号。向量旋转时因为是绕中心旋转,因此用一个连接木板的顶点与中心的向量来旋转,再加上中心的坐标就可以得到旋转后的顶点坐标,然后求凸包并计算其面积。

#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
#include <cmath>
#include <list>
using namespace std;
struct point
{
    double x,y;
    point(double a=0,double b=0):x(a),y(b)
    {

    }
};
struct line
{
    point a,b;
    int id;
    line()
    {

    }
    line(point c,point d):a(c),b(d)
    {

    }
};
const double eps=1e-10;
bool operator ==(point a,point b)
{
    return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;
}
bool operator <(point a,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);
}
int dcmp(double x)
{
    if(fabs(x)<eps)
        return 0;
    else
        return x<0?-1:1;
}
double cross(point a,point b)
{
    return a.x*b.y-a.y*b.x;
}
double dot(point a,point b)
{
    return a.x*b.x+a.y*b.y;
}
const double PI = acos(-1.0);//计算圆周率
double torad(double deg)//计算弧度
{
    return deg/180 * PI;
}
point Rotate(point a,double rad)//向量旋转 rad用弧度
{
    return point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
point p[2500],ch[2500];
double polyarea(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)//凸包模板 不能有重复点 需要先去重 但是此题保证没有重复点
{                                     //输入点数组为p 个数为n 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;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        double area=0;
        int pc=0;
        for(int i=0; i<n; i++)
        {
            double x,y,w,h,j,ang;
            scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j);
            ang=-torad(j);
            point o(x,y);
            p[pc++]=o+Rotate(point(-w/2,-h/2),ang);//旋转顶点并计算旋转后的顶点坐标
            p[pc++]=o+Rotate(point(w/2,-h/2),ang);
            p[pc++]=o+Rotate(point(-w/2,h/2),ang);
            p[pc++]=o+Rotate(point(w/2,h/2),ang);
            area+=w*h;
        }
        int m=convexhull(p,pc,ch);
        double area2=polyarea(ch,m);
        printf("%.1f %\n",area*100/area2);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值