码迷,mamicode.com
首页 > 移动开发 > 详细

●UVA 10652 Board Wrapping

时间:2018-01-07 14:06:35      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:highlight   cpp   app   stream   and   name   ora   cas   abs   

 

题链:

https://vjudge.net/problem/UVA-10652

题解:

计算几何,Andrew求凸包,

裸题。。。(数组开小了,还整了半天。。。)

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 650
using namespace std;
const double eps=1e-8,Pi=acos(-1);
int sign(double x){
	if(fabs(x)<=eps) return 0;
	return x<0?-1:1;
}
struct Point{
	double x,y;
	Point(double _x=0,double _y=0):x(_x),y(_y){}
};
typedef Point Vector;
bool operator < (const Point A,const Point B){return sign(A.x-B.x)<0||(sign(A.x-B.x)==0&&sign(A.y-B.y)<0);}
bool operator == (Point A,Point B){return sign(A.x-B.x)==0&&sign(A.y-B.y)==0;}
Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Point A,Point B){return Vector(A.x-B.x,A.y-B.y);}
double operator ^ (Vector A,Vector B){return A.x*B.y-A.y*B.x;}
Point D[MAXN*4],C[MAXN*4];
double torad(double deg){return deg/180*Pi;}//角度转弧度
Vector rotate(Vector A,double angle){//向量A逆时针旋转angle(弧度)
	return Vector(A.x*cos(angle)-A.y*sin(angle),A.x*sin(angle)+A.y*cos(angle));
}
int Andrew(int dnt){
	int cnt=0,k=0;
	sort(D+1,D+dnt+1); 
	dnt=unique(D+1,D+dnt+1)-D-1;
	for(int i=1;i<=dnt;i++){
		while(cnt>1&&((C[cnt]-C[cnt-1])^(D[i]-C[cnt-1]))<=0) cnt--;
		C[++cnt]=D[i];
	} k=cnt;
	for(int i=dnt-1;i>=1;i--){
		while(cnt>k&&((C[cnt]-C[cnt-1])^(D[i]-C[cnt-1]))<=0) cnt--;
		C[++cnt]=D[i];
	}
	return cnt;
}
double PolygonArea(int cnt){
	double area=0;
	for(int i=1;i<cnt;i++) area+=(C[i]^C[i+1])/2;
	return area;
}
int main(){
	double area1,area2,x,y,w,h,angle;
	int Case,N,dnt,cnt; scanf("%d",&Case);
	while(Case--){
		area1=area2=dnt=cnt=0;
		scanf("%d",&N);
		for(int i=1;i<=N;i++){
			scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&angle);
			angle=-torad(angle);
			D[++dnt]=Point(x,y)+rotate(Vector(w/2,h/2),angle);
			D[++dnt]=Point(x,y)+rotate(Vector(-w/2,h/2),angle);
			D[++dnt]=Point(x,y)+rotate(Vector(w/2,-h/2),angle);
			D[++dnt]=Point(x,y)+rotate(Vector(-w/2,-h/2),angle);
			area1+=w*h;
		}	
		cnt=Andrew(dnt);
		//printf("%d\n",cnt);
		area2=PolygonArea(cnt);
		printf("%.1lf %%\n",area1*100/area2);
	}
	return 0;
}

  

●UVA 10652 Board Wrapping

标签:highlight   cpp   app   stream   and   name   ora   cas   abs   

原文地址:https://www.cnblogs.com/zj75211/p/8227616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!