题意:有n块矩形木板,用一个面积尽量小的凸多边形把它们包起来,并计算出木板占整个包装面积的百分比。
题解:把每个木板旋转后的四个点加入集合,最后就有4×n个点的点集,然后问题转化为普通的凸包问题就容易解决了。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const int N = 2500;
struct Point {
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
}P[N], res[N];
struct Circle {
Point c;
double r;
Circle() {}
Circle(Point c, double r = 0): c(c), r(r) {}
Point point(double a) {
return Point(c.x + cos(a) * r, c.y + sin(a) * c.y);
}
};
int n, cnt;
double Sqr(double x) {
return x * x;
}
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);
}
Point operator * (Point A, double p) {
return Point(A.x * p, A.y * p);
}
Point operator / (Point A, double p) {
return Point(A.x / p, A.y / p);
}
//计算点积的正负 负值夹角为钝角
int dcmp(double x) {
if (fabs(x) < 1e-9)
return 0;
return x < 0 ? -1 : 1;
}
bool operator < (const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool operator == (const Point& a, const Point& b) {
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
//计算点积
double Dot(Point A, Point B) {
return A.x * B.x + A.y * B.y;
}
//计算叉积,也就是数量积
double Cross(Point A, Point B) {
return A.x * B.y - A.y * B.x;
}
//计算向量长度
double Length(Point A) {
return sqrt(Dot(A, A));
}
//向量A旋转rad弧度,rad负值为顺时针旋转
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 torad(double deg) {
return deg / 180.0 * PI;
}
//得到凸包点数组和顶点数
int ConvexHull(Point* P, int cnt, Point* res) {
sort(P, P + cnt);
cnt = unique(P, P + cnt) - P;
int m = 0;
for (int i = 0; i < cnt; i++) {
while (m > 1 && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
m--;
res[m++] = P[i];
}
int k = m;
for (int i = cnt - 2; i >= 0; i--) {
while (m > k && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
m--;
res[m++] = P[i];
}
if (cnt > 1)
m--;
return m;
}
//计算多边形面积
double PolygonArea(Point* res, int m) {
double area = 0;
for (int i = 1; i < m - 1; i++)
area += Cross(res[i] - res[0], res[i + 1] - res[0]);
return area / 2;
}
int main() {
Point P[N], res[N];
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int cnt = 0;
double x, y, w, h, j, sum1 = 0;
for (int i = 0; i < n; i++) {
scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &j);
Point p(x, y);
double ang = -torad(j);
P[cnt++] = p + Rotate(Point(w / 2, h / 2), ang);
P[cnt++] = p + Rotate(Point(w / 2, -h / 2), ang);
P[cnt++] = p + Rotate(Point(-w / 2, h / 2), ang);
P[cnt++] = p + Rotate(Point(-w / 2, -h / 2), ang);
sum1 += w * h;
}
int temp = ConvexHull(P, cnt, res);
double sum2 = PolygonArea(res, temp);
printf("%.1lf %%\n", sum1 * 100 / sum2);
}
return 0;
}