UVA 10652 Board Wrapping

哇咔咔,这题RANK 8 (*^__^*) 嘻嘻……巨开心~~

 

UVA 10652 Board Wrapping_第1张图片

 

其实这题也不难哈,但是为嘛做的人这么少捏。。

 

就是给你N个矩形,问你所有矩形的面积占包围这个矩形的面积的百分比。

 

矩形的面积很好弄,长*宽即可,但是所有包围这个矩形的面积呢。既然是包装,肯定是个凸包,所以只要计算矩形的四个点,然后以这四个点作为一个点集求凸包即可。

 

这四个点的话,有点数学知识就会求了,我用了好多三角函数,还好不卡精度。。。

 

#include <queue> #include <stack> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h> #include <string.h> #include <string> #include <algorithm> using namespace std; const int MAX = 10010; const double pi = acos(-1); struct point{double x,y;}; const double eps = 1e-6; bool dy(double x,double y) { return x > y + eps;} // x > y bool xy(double x,double y) { return x < y - eps;} // x < y bool dyd(double x,double y) { return x > y - eps;} // x >= y bool xyd(double x,double y) { return x < y + eps;} // x <= y bool dd(double x,double y) { return fabs( x - y ) < eps;} // x == y point c[MAX]; point stk[MAX]; int top; double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 { return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y); } double disp2p(point a,point b) { return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) ); } bool cmp(point a,point b) // 排序 { double len = crossProduct(c[0],a,b); if( dd(len,0.0) ) return xy(disp2p(c[0],a),disp2p(c[0],b)); return xy(len,0.0); } void Graham(point c[],int n) { int tmp = 0; for(int i=1; i<n; i++) if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) ) tmp = i; swap(c[0],c[tmp]); sort(c+1,c+n,cmp); stk[0] = c[0]; stk[1] = c[1]; top = 1; for(int i=2; i<n; i++) { while( xyd( crossProduct(stk[top],stk[top-1],c[i]), 0.0 ) && top >= 1 ) top--; stk[++top] = c[i]; } } double area_polygon(point p[],int n) { double s = 0.0; for(int i=0; i<n; i++) s += p[(i+1)%n].y * p[i].x - p[(i+1)%n].x * p[i].y; return fabs(s)/2.0; } int main() { int n,ncases,cou; double area,x,y,w,h,a; scanf("%d",&ncases); while( ncases-- ) { scanf("%d",&n); cou = 0; area = 0.0; while( n-- )// x y是木板中心坐标 { scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&a); a = a/180*pi; area += w*h; double l = sqrt(w*w + h*h)/2; double b = pi - (a + atan(w/h)); c[cou].x = x - l*sin(b); c[cou++].y = y + l*cos(b); c[cou].x = 2*x - c[cou-1].x; c[cou++].y = 2*y - c[cou-1].y; double s = atan(h/w)*2 + pi/2 - b; c[cou].x = x - l*cos(s); c[cou++].y = y + l*sin(s); c[cou].x = 2*x - c[cou-1].x; c[cou++].y = 2*y - c[cou-1].y; } Graham(c,cou); double area_p = area_polygon(stk,top+1); printf("%.1lf %%/n",area/area_p*100); } return 0; }  

你可能感兴趣的:(c,struct)