提交地址:https://vjudge.net/problem/UVA-10652
题目:
pdf:https://uva.onlinejudge.org/external/106/10652.pdf
解题思路:
凸包模版题,求完凸包再求这个凸包(多边形)的面积。
有两个需要注意的小点:
(1)转角的范围: 且这个木板是以木板中心(x,y)为旋转中心旋转的,若要求出旋转后的四个点坐标,坐标中心应该是(x,y),不能先求出平放时的四点坐标再旋转,因为那样旋转中心是(x,y)对应的(0,0)。
(2)Rotate(Vecotr p, double rad)中的参数rad是逆时针旋转时的弧度数,要将输入的度数转化一下。
ac代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4*610;
const double pi = acos(-1.0);
const double eps = 1e-10;
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
friend bool operator < (Point a, Point b)
{
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
};
typedef Point Vector;
Vector operator - (Vector a, Vector 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);
}
int dcmp(double x)//精度三态函数(>0,<0,=0)
{
if (fabs(x) < eps)return 0; //等于
else return x < 0 ? -1 : 1;//小于,大于
}
double Cross(Vector a, Vector b)//外积
{
return a.x * b.y - a.y * b.x;
}
Vector Rotate(Vector a, double rad)//逆时针旋转rad弧度
{
return Vector(a.x*cos(rad) - a.y*sin(rad), a.x*sin(rad) + a.y*cos(rad));
}
int ConvexHull(Point *p, int n, Point* ch)//凸包
{
sort(p, p+n);//先x后y
int m = 0;//ch存最终的凸包顶点,下标从0开始
for(int i = 0; i < n; i++)
{
//m是待确定的
while(m > 1 && dcmp(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 && dcmp(Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;//同样的方向
ch[m++] = p[i];
}
if(n>1) m--;//m是凸包上的点数+1,相当于n,ch[m]和ch[0]相同,遍历的时候<m即可
return m;
}
double PolygonArea(Point* p, int n)//多边形面积
{
double area = 0;
for(int i = 1; i < n - 1; i++)//下标从0开始存坐标,共n个点
area += Cross(p[i] - p[0], p[i + 1] - p[0]);
return area/2.0;
}
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
int t, n;
double x, y, w, h, ang;
Point allp[maxn], ch[maxn];//p存所有点
scanf("%d", &t);
while(t--)
{
double barea = 0;
int cnt = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%lf %lf %lf %lf %lf", &x, &y, &w, &h, &ang);
double hw = w/2.0, hh = h/2.0;
if(ang >= 0) ang = 2*pi-(pi*ang)/180.0;
else ang = -pi*ang/180.0;//本身就是逆时针的度数
Point c = Point(x, y);
//绕着中心旋转,中心c不动,而不是水平时的四个坐标旋转(此时的旋转中心是(0,0),中心的坐标也会变)
allp[cnt++] = c + Rotate(Point(-hw, -hh), ang);
allp[cnt++] = c + Rotate(Point(hw, -hh), ang);
allp[cnt++] = c + Rotate(Point(-hw, hh), ang);
allp[cnt++] = c + Rotate(Point(hw, hh), ang);
barea += w*h;
}
int m = ConvexHull(allp, cnt, ch);
double tarea = PolygonArea(ch, m);
printf("%.1lf %%\n", barea*100/tarea);
}
return 0;
}