UVA 10652 Board Wrapping

看到题目思路应该很明确,同过给的坐标和偏转角度求出每个多边形的顶点;

将顶点构成的集合排序去重后构建凸包;

利用多边形面积的计算方法就凸包面积求得百分比;

#include<cstdio> 
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
const double PI=acos(-1.0);
double torad(double x){return x*PI/180;}
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator - (Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}

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 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));
}
vector<Point> ConvexHull(vector<Point> p){
	sort(p.begin(),p.end());
	p.erase(unique(p.begin(),p.end()),p.end());
	int m=0,n=p.size();
	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){
	double area=0;
	for(int i=1;i<p.size()-1;i++)area+=Cross(p[i]-p[0],p[i+1]-p[0]);
	return area/2;
}
int main(){
	int N,n;
	scanf("%d",&N);
	while(N--){
		double area1=0;
		vector<Point>p;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			double x,y,w,h,rad;
			scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&rad);
			rad=torad(-rad);
			Point o(x,y);
			p.push_back(o+Rotate(Vector(w/2,-h/2),rad));
			p.push_back(o+Rotate(Vector(w/2,h/2),rad));
			p.push_back(o+Rotate(Vector(-w/2,h/2),rad));
			p.push_back(o+Rotate(Vector(-w/2,-h/2),rad));
			area1+=w*h;
		}
		double area2=PolygonArea(ConvexHull(p));
		printf("%.1lf %\n",area1*100/area2);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/love_phoebe/article/details/81280487