【2011 Greater New York Regional 】Problem I :The Golden Ceiling

时间:2024-08-09 17:06:14

一道比较简单但是繁琐的三维计算几何,找错误找的我好心酸,没想到就把一个变量给写错了 = =;

题目的意思是求平面切长方体的截面面积+正方体顶部所遮盖的面积;

找出所有的切点,然后二维凸包一下直接算面积即可!

发个代码纪念一下!

代码:

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define eps 1e-8
using namespace std; inline int sig(double x){return (x>eps)-(x<-eps);}
double w,l,hh,aa,bb,cc,dd;
int num1,num2;
struct point
{
double x,y,z;
point(double x=,double y=,double z=):x(x),y(y),z(z) { }
bool operator < (const point &t)const
{
if(sig(x-t.x)==)
{
return y<t.y;
}
else return x<t.x;
}
point operator+(const point&b)const{return point(x+b.x,y+b.y,z+b.z);}
point operator-(const point&b)const{return point(x-b.x,y-b.y,z-b.z);}
point operator*(double p){return point(x*p,y*p,z*p);}
point operator/(double p){return point(x/p,y/p,z/p);}
} ve[],tu[],vv[],tt[]; void intersection(point a,point b)
{
double t=(aa*a.x+bb*a.y+cc*a.z+dd)/(aa*(a.x-b.x)+bb*(a.y-b.y)+cc*(a.z-b.z));
if(t>=-eps&&t<=1.00000+eps)
{
point v=a+(b-a)*t;
ve[num1++]=v;
if(sig(v.z-hh)==)
tu[num2++]=v;
}
}
double lenth(point a){return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);}
point cross(point a,point b){return point(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);}
double area(point a,point b,point c){return lenth(cross(b-a,c-a))/2.0;}
double cross2(point a,point b){return a.x*b.y-a.y*b.x;}
bool check(point a)
{
if(sig(aa*a.x+bb*a.y+cc*a.z+dd)<)
return ;
return ;
} int convexhull(point *p,int n,point* ch)
{
sort(p,p+n);
int m=;
for(int i=;i<n;i++)
{
while(m>&&cross2(ch[m-]-ch[m-],p[i]-ch[m-])<=eps)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-;i>=;i--)
{
while(m>k&&cross2(ch[m-]-ch[m-],p[i]-ch[m-])<=eps)m--;
ch[m++]=p[i];
}
if(n>)m--;
return m;
} int main()
{
int t,ca;
scanf("%d",&t);
while(t--)
{
memset(tu,,sizeof tu);
memset(ve,,sizeof ve);
memset(vv,,sizeof vv);
memset(tt,,sizeof tt);
scanf("%d",&ca);
printf("%d ",ca);
scanf("%lf%lf%lf%lf%lf%lf%lf",&l,&w,&hh,&aa,&bb,&cc,&dd);
num1=num2=;
dd=-dd;
point a(0.0,0.0,0.0);
point b(l,0.0,0.0);
point c(l,w,0.0);
point d(0.0,w,0.0);
point e(0.0,0.0,hh);
point f(l,0.0,hh);
point g(l,w,hh);
point h(0.0,w,hh);
intersection(a,b);
intersection(b,c);
intersection(c,d);
intersection(d,a);
intersection(a,e);
intersection(b,f);
intersection(c,g);
intersection(d,h);
intersection(e,f);
intersection(f,g);
intersection(g,h);
intersection(h,e);
if(check(e)==)tu[num2++]=e;
if(check(f)==)tu[num2++]=f;
if(check(g)==)tu[num2++]=g;
if(check(h)==)tu[num2++]=h;
int x=convexhull(ve,num1,vv);
int y=convexhull(tu,num2,tt);
double ans=;
for(int i=;i<x;i++)
ans+=area(vv[],vv[i-],vv[i]);
for(int i=;i<y;i++)
ans+=area(tt[],tt[i-],tt[i]);
printf("%.0lf\n",ceil(ans));
}
return ;
}