简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping
题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例
分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积。已知矩形的中心,向量在原点参考点再旋转,角度要转换成弧度制。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | /************************************************ * 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; } |
编译人生,运行世界!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步