其他分享
首页 > 其他分享> > UVA10652 Board Wrapping (二维凸包+多边形面积)

UVA10652 Board Wrapping (二维凸包+多边形面积)

作者:互联网

题目链接:https://www.luogu.com.cn/problem/UVA10652

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

const int maxn = 5010;
const double eps = 1e-10;
const double PI = acos(-1.0);

int T, n;

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

struct Point {
  double x, y;
  Point(double x=0, double y=0):x(x),y(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 dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}

typedef Point Vector;

Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); }
double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }
double Length(const Vector& A) { return sqrt(Dot(A, A)); }
double Angle(const Vector& A, const Vector& B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }

double torad(double x){
	return x / 180 * PI;
}

bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2) {
  double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
  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(const Point& P, const Point& v, const Point& Q, const Point& w) {
  Vector u = P-Q;
  double t = Cross(w, u) / Cross(v, w);
  return P+v*t;
}

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

bool OnSegment(const Point& p, const Point& a1, const Point& a2) {
  return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}

int pc, m;
Point p[maxn], ch[maxn];

void ConvexHull(){
	for(int i = 1 ; i <= pc ; ++i){
		while(m >= 2 && dcmp(Cross(ch[m]-ch[m-1], p[i]-ch[m-1])) <= 0) --m;
		ch[++m] = p[i];
	}
	int k = m;
	for(int i = pc-1 ; i >= 1 ; --i){
		while(m >= k+1 && dcmp(Cross(ch[m]-ch[m-1], p[i]-ch[m-1])) <= 0) --m;
		ch[++m] = p[i];
	}
	if(n > 1) --m;
}

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

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
	scanf("%d", &T);
	while(T--){
		pc = 0, m = 0;
		scanf("%d", &n);
		double x, y, w, h, j;
		double area1 = 0, area2 = 0;
		for(int i = 1 ; i <= n ; ++i){
			scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &j);
			double ang = -torad(j);
			Point o(x, y);
			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;
		}
		
		sort(p+1, p+1+pc);
		pc = unique(p+1, p+1+pc)-p-1;
		
		ConvexHull();

		area2 = Polyarea();
		area2 /= 2.0;
		
		printf("%.1lf %%\n", (area1*100/area2));
	}
	return 0;
}

标签:ch,const,Point,UVA10652,double,Wrapping,凸包,Vector,return
来源: https://www.cnblogs.com/tuchen/p/15362269.html