首页 > 代码库 > UVA 10652 Board Wrapping 计算几何
UVA 10652 Board Wrapping 计算几何
多边形凸包。。。。
Board Wrapping
Submit Status Description Problem B Board Wrapping Input: standard input Time Limit: 2 seconds The small sawmill in Mission, British Columbia, has developed a brand new way of packaging boards for drying. By fixating the boards in special moulds, the board can dry efficiently in a drying room. Space is an issue though. The boards cannot be too close, because then the drying will be too slow. On the other hand, one wants to use the drying room efficiently. Looking at it from a 2-D perspective, your task is to calculate the fraction between the space occupied by the boards to the total space occupied by the mould. Now, the mould is surrounded by an aluminium frame of negligible thickness, following the hull of the boards‘ corners tightly. The space occupied by the mould would thus be the interior of the frame.
InputOn the first line of input there is one integer, N <= 50, giving the number of test cases (moulds) in the input. After this line, N test cases follow. Each test case starts with a line containing one integer n, 1< n <= 600, which is the number of boards in the mould. Then n lines follow, each with five floating point numbers x, y, w, h, j where 0 <= x, y, w, h <=10000 and –90° < j <=90°. The x and y are the coordinates of the center of the board and w and h are the width and height of the board, respectively. j is the angle between the height axis of the board to the y-axis in degrees, positive clockwise. That is, if j = 0, the projection of the board on the x-axis would be w. Of course, the boards cannot intersect.
OutputFor every test case, output one line containing the fraction of the space occupied by the boards to the total space in percent. Your output should have one decimal digit and be followed by a space and a percent sign (%).
Sample Input Output for Sample Input
Swedish National Contest
The Sample Input and Sample Output corresponds to the givenpicture
Source Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: (Computational) Geometry :: Polygon :: Standard Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 4. Geometry :: Geometric Algorithms in 2D :: Examples Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: (Computational) Geometry :: Polygon - Standard Submit Status |
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> using namespace std; const double eps=1e-6; int dcmp(double x) { if(fabs(x)<eps) return 0; return (x<0)?-1:1;} struct Point { double x,y; Point(){} Point(double _x,double _y):x(_x),y(_y){}; }; 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);} bool operator<(const Point& A,const Point& B) {return dcmp(A.x-B.x)<0||(dcmp(A.x-B.x)==0&&dcmp(A.y-B.y)<0);} bool operator==(const Point& a,const Point& b) {return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;} double Angle(Point v){return atan2(v.y,v.x);} 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.*acos(-1.);} double Cross(Point A,Point B){return A.x*B.y-A.y*B.x;} int n; double area0,area1; vector<Point> vp,ch; // 点集凸包 // 如果不希望在凸包的边上有输入点,把两个 <= 改成 < // 注意:输入点集会被修改 vector<Point> CovexHull(vector<Point>& p) { sort(p.begin(),p.end()); p.erase(unique(p.begin(),p.end()),p.end()); int n=p.size(); int m=0; vector<Point> ch(n+1); for(int i=0;i<n;i++) { while(m>1&&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&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; ch[m++]=p[i]; } if(n>1) m--; ch.resize(m); return ch; } double PolygonArea(vector<Point>& p) { int n=p.size(); 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.; } int main() { int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d",&n); area0=area1=0.0; vp.clear(); double x,y,w,h,j; for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j); area0+=w*h; double rad=torad(j); Point o(x,y); vp.push_back(o+Rotate(Point(w/2,h/2),-rad)); vp.push_back(o+Rotate(Point(-w/2,h/2),-rad)); vp.push_back(o+Rotate(Point(w/2,-h/2),-rad)); vp.push_back(o+Rotate(Point(-w/2,-h/2),-rad)); } ch=CovexHull(vp); area1=PolygonArea(ch); printf("%.1lf %%\n",100.*area0/area1); } return 0; }
UVA 10652 Board Wrapping 计算几何