简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping

时间:2022-06-06 13:00:56

题目传送门

题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例

分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积。已知矩形的中心,向量在原点参考点再旋转,角度要转换成弧度制。

/************************************************
* Author :Running_Time
* Created Time :2015/11/10 星期二 10:34:43
* File Name :UVA_10652.cpp
************************************************/ #include <bits/stdc++.h>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dcmp(double x) {
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
struct Point {
double x, y;
Point () {}
Point (double x, double y) : x (x), y (y) {}
Point operator - (const Point &r) const { //向量减法
return Point (x - r.x, y - r.y);
}
Point operator * (double p) const { //向量乘以标量
return Point (x * p, y * p);
}
Point operator / (double p) const { //向量除以标量
return Point (x / p, y / p);
}
Point operator + (const Point &r) const {
return Point (x + r.x, y + r.y);
}
bool operator < (const Point &r) const {
return x < r.x || (x == r.x && y < r.y);
}
bool operator == (const Point &r) const {
return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0;
}
};
typedef Point Vector;
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) { //向量叉积
return A.x * B.y - A.y * B.x;
}
Vector rotate(Vector A, double rad) {
return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad));
}
double area_poly(vector<Point> ps) {
double ret = 0;
for (int i=1; i<ps.size ()-1; ++i) {
ret += fabs (cross (ps[i] - ps[0], ps[i+1] - ps[0])) / 2;
}
return ret;
}
vector<Point> convex_hull(vector<Point> ps) {
sort (ps.begin (), ps.end ());
ps.erase (unique (ps.begin (), ps.end ()), ps.end ());
int n = ps.size (), k = 0;
vector<Point> qs (n * 2);
for (int i=0; i<n; ++i) {
while (k > 1 && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-2]) <= 0) k--;
qs[k++] = ps[i];
}
for (int t=k, i=n-2; i>=0; --i) {
while (k > t && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-2]) <= 0) k--;
qs[k++] = ps[i];
}
qs.resize (k - 1);
return qs;
} int main(void) {
int T; scanf ("%d", &T);
while (T--) {
int n; scanf ("%d", &n);
vector<Point> ps;
double area1 = 0;
double x, y, w, h, r;
for (int i=0; i<n; ++i) {
scanf ("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &r);
Point a = Point (x, y);
area1 += w * h;
r = -r / 180 * PI;
ps.push_back (a + rotate (Vector (-w/2, -h/2), r));
ps.push_back (a + rotate (Vector (w/2, -h/2), r));
ps.push_back (a + rotate (Vector (w/2, h/2), r));
ps.push_back (a + rotate (Vector (-w/2, h/2), r));
}
vector<Point> qs = convex_hull (ps);
printf ("%.1f %%\n", 100 * area1 / area_poly (qs));
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}