UVA 10652 Board Wrapping(凸包)

#include<bits/stdc++.h>
using namespace std;

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);}
Vector operator *(Vector A,double p){return Vector(A.x*p,A.y*p);}
Vector operator /(Vector A,double p){return Vector(A.x/p,A.y/p);}

bool operator <(const Point& a,const Point &b){
	return a.x<b.x || (a.x==b.x && a.y<b.y);
}

const double eps=1e-10;
int dcmp(double x){
	if(fabs(x)<eps) return 0;else return x<0?-1 :1; 
}

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 torad(double d){
	return d/180*acos(-1);
}

bool cmp(Point a,Point b){
	if(a.x==b.x) return a.y<b.y;
	else return a.x<b.x;
}

int convexhull(Point *p,int n,Point *ch){
	sort(p,p+n,cmp);
	int m=0;
	for(int i=0;i<n;i++){
		while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-1])<=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--;
	return m;
}

double PolygonArea(Point *p,int n){
	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;
	Point p[2500],ch[2500];
	scanf("%d",&t);	
	while(t--){
		int n,pc=0;
		double area1=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			double x,y,w,h,j,ang;
			scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j);
			Point o(x,y);
			ang=-torad(j);
			p[pc++]=o+rotate(Vector(-w/2,-h/2),ang);
			p[pc++]=o+rotate(Vector(w/2,-h/2),ang);
			p[pc++]=o+rotate(Vector(-w/2,h/2),ang);
			p[pc++]=o+rotate(Vector(w/2,h/2),ang);
			area1+=w*h;
		}
		int m=convexhull(p,pc,ch);
		double area2=PolygonArea(ch,m);
		printf("%.1lf %%\n",area1*100/area2);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章