uva10652
题目
就是给你几个长方形,并且要旋转一定角度,用一个凸包把它们包起来并且求长方形面积总和与凸包面积之比。
思路
模版题,多了个求凸包面积和向量的旋转。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define pi acos (-1)
using namespace std;
typedef long long ll;
const int maxn=3000;
int tot;
struct point
{
double x, y;
point (double _x = 0, double _y = 0) : x(_x), y(_y) {}
point operator - (point a) const
{
return point (x-a.x, y-a.y);
}
point operator + (point a) const
{
return point (x+a.x, y+a.y);
}
bool operator < (const point &a) const
{
return x < a.x || (x == a.x && y < a.y);
}
} p[maxn];
const double eps = 1e-10;
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;
}
double AngleToRad (double x)
{
return x*pi/180;
}
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 ConvexPolygonArea (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 area/2.0;
}
int n, m;
double l;
point ch[maxn];
int ConvexHull (int n)
{
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-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;
}
double dis (point a, point b)
{
double xx = a.x-b.x, yy = a.y-b.y;
return sqrt (xx*xx + yy*yy);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
tot=0;
double sum1=0,sum2=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
double x,y,w,h,ang;
scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&ang);
ang=-AngleToRad(ang);
sum1+=w*h;
point from(x,y);
p[tot++]=from+rotate(point(w/2.0,h/2.0),ang);
p[tot++]=from+rotate(point(w/2.0,-h/2.0),ang);
p[tot++]=from+rotate(point(-w/2.0,h/2.0),ang);
p[tot++]=from+rotate(point(-w/2.0,-h/2.0),ang);
}
m=ConvexHull(tot);
sum2=ConvexPolygonArea(ch,m);
printf ("%.1f %\n", 100.0*sum1/sum2);
}
return 0;
}