UVA - 10652 Board Wrapping (凸包+多边形面积)

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const double ep=1e-10;
const int maxn=6e3+10;
int cnt;

struct Point {
	double x, y;
	Point(){}
	Point(double x, double y): x(x), y(y){}
	bool operator < (const Point A) const{ return x<A.x || (x==A.x&&y<A.y);}
	
}p[maxn], ch[maxn];
typedef Point Vector;

int dcmp(double x){
	if(fabs(x) < ep) return 0; else return x<0? -1 : 1;
}

Point operator + (Point A, Vector B) { return Point(A.x+B.x, A.y+B.y); }
Point operator - (Point A, Point 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 (dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0); }
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; }

bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
	double c1=Cross(a2-a1, b1-a1), c2=Cross(a2-a1, b2-a1);
	double c3=Cross(b2-b1, a1-b1), c4=Cross(b2-b1, a2-b1);
	return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}

Point GetLineIntersection(Point a1, Vector v1, Point a2, Vector v2){
	Vector v=a1-a2;
	double t=Cross(v2, v)/Cross(v1, v2);
	return a1+v1*t;
}

bool OnSegment(Point p, Point a, Point b){
	return dcmp(Cross(a-p, b-p))==0 && dcmp(Dot(a-p, b-p))<0;
}

Vector Rotate(Vector A, double rad){
	return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}

void GetPoint(Point t, double w, double h, double rad){
	p[cnt++]=t + Rotate(Vector(w/2, h/2), rad);
	p[cnt++]=t + Rotate(Vector(-w/2, h/2), rad);
	p[cnt++]=t + Rotate(Vector(-w/2, -h/2), rad);
	p[cnt++]=t + Rotate(Vector(w/2, -h/2), rad);
}

int ConvexHull(){
	sort(p, p+cnt);
	
	int m=0;
	for(int i=0; i<cnt; 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=cnt-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(cnt>1) m--;
	
	return m;
}

double PolygonArea(int m){
	double area =0;
	for(int i=1; i<m-1; i++)
		area+=Cross(ch[i]-ch[0], ch[i+1]-ch[0]);
	return area/2;
}

double torad(double x){
	return x/180 * acos(-1);
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--){
		int n;
		cnt=0;
		double s1=0, x, y, w, h, j, rad;
		scanf("%d", &n);
		for(int i=0; i<n; i++)
		{
			Point t;
			scanf("%lf%lf%lf%lf%lf", &t.x, &t.y, &w, &h, &j);
			rad=torad(j);
			GetPoint(t, w, h, -rad);
			s1+=w*h;
		}
		
		int m=ConvexHull();
		
		double s=PolygonArea(m);
		
		double percenty=s1/s*100;
		printf("%.1lf %\n", percenty);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/du_lun/article/details/79953600
今日推荐