题意:
给出一些矩形,(长、宽、中心位置、顺时针旋转的角度),用一个最小的凸多边形把它围起来,求矩形面积和/凸多边形面积。
解法:求凸包
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const double PI=cos(-1.0);
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); }
Vector operator -(Vector A) {return Vector(-A.x,-A.y);}
double torad(double deg)
{
return deg/180*acos(-1);
}
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
bool operator<(const Point &a ,const Point& b)
{
return dcmp(a.x-b.x)<0|| dcmp(a.x-b.x)==0 &&dcmp(a.y-b.y)<0;
}
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 Length(Vector A)
{
return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B)
{
return acos(Dot(A,B)/Length(A)/Length(B));
}
double Cross(Vector A,Vector B)//叉乘
{
return A.x*B.y-A.y*B.x;
}
double Area2(Point A,Point B,Point C)//平行四边形面积
{
return Cross(B-A,C-A);
}
Vector Rotate(Vector A,double rad)//向量A 逆时针旋转rad度
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad) );
}
Vector Normal(Vector A)//计算A的单位向量
{
double L=Length(A);
return Vector( -A.y/L,A.x/L );
}
int np;
Point p[2500],ch[2500];
int ConvexHell(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++)
{
while(m>1&&dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2] )) <=0 ) m--;//叉乘<=0,逆时针,上凸
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--)
{
while(m>k &&dcmp( 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()
{
std::ios::sync_with_stdio(false);
int n,T;cin>>T;
while(T--)
{
cin>>n;
double x,y,w,h,ang,area1=0;
np=0;
for0(i,n)
{
cin>>x>>y>>w>>h>>ang;
Point o(x,y);
ang=-torad(ang);
p[np++]=o+Rotate(Vector(-w/2,-h/2),ang);
p[np++]=o+Rotate(Vector(w/2,-h/2),ang);
p[np++]=o+Rotate(Vector(-w/2,h/2),ang);
p[np++]=o+Rotate(Vector(w/2,h/2),ang);
area1+=w*h;
}
int m=ConvexHell(p,np,ch);
double area2=PolygonArea(ch, m);
// cout<<area1<<" "<<area2<<endl;
printf("%.1f %%\n",area1*100/area2);
}
return 0;
}