题目链接:http://192.168.2.240:8080/JudgeOnline/showproblem?problem_id=1965
polygon半平面交 Time Limit:1000MS Memory Limit:165536K Description Input n为半平面个数,以下n行表示若干半平面。形式如ax+by+c<=0 Output 输出半平面交的面积。保留3位小数 Sample Input 4
1 0 -1
0 1 -1
-1 0 -1
0 -1 -1
Sample Output 4.000
数据保证:N<=10000
半平面交出的多边形的坐标在10^10以内。
Source 计算几何 半平面交 |
半平面交裸题。
讲道理不是说好保留三位小数吗,TMD数据全是保留到整数……幸好我WA了一次之后看了看数据,不然不知道要改多久……机智如我
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6 #define inf 1e10 7 #define maxn 10010 8 using namespace std; 9 int n,head,tail,tot,cnt; 10 double ans; 11 const double eps=1e-15; 12 struct point{double x,y;}p[maxn]; 13 point operator +(point x,point y){return (point){x.x+y.x,x.y+y.y};} 14 point operator -(point x,point y){return (point){x.x-y.x,x.y-y.y};} 15 struct line{double ang,a,b,c;point pt;}li[maxn],que[maxn]; 16 double dot(point a,point b){return a.x*b.x+a.y*b.y;} 17 double cross(point x,point y,point z){return (x.x-z.x)*(y.y-z.y)-(x.y-z.y)*(y.x-z.x);} 18 bool includ(line x,point y){return y.x*x.a+y.y*x.b+x.c<=eps;} 19 bool comp(line x,line y){ 20 if(x.ang==y.ang)return includ(y,x.pt); 21 return x.ang<y.ang; 22 } 23 point calc(line s1,line s2){ 24 double v1=s1.b*s2.c-s1.c*s2.b,v2=s1.c*s2.a-s1.a*s2.c; 25 double v0=s1.a*s2.b-s1.b*s2.a; 26 return (point){v1/v0,v2/v0}; 27 } 28 bool check(line x,line y,line z){return !includ(z,calc(x,y));} 29 bool solve(){ 30 head=1;tail=0; 31 for(int i=1;i<=tot;i++){ 32 if(i>1&&fabs(li[i].ang-li[i-1].ang)<=eps)continue; 33 while(head<tail&&check(que[tail-1],que[tail],li[i]))tail--; 34 while(head<tail&&check(que[head],que[head+1],li[i]))head++; 35 que[++tail]=li[i]; 36 } 37 while(head<tail&&check(que[tail-1],que[tail],que[head]))tail--; 38 while(head<tail&&check(que[head],que[head+1],que[tail]))head++; 39 for(int i=head;i<tail;i++)p[++cnt]=calc(que[i],que[i+1]); 40 p[++cnt]=calc(que[head],que[tail]); 41 p[cnt+1]=p[1]; 42 } 43 void getans(){ 44 ans=0;for(int i=1;i<=cnt;i++)ans+=cross(p[i],p[i+1],(point){0,0});ans=fabs(ans)/2; 45 } 46 int main(){ 47 //freopen("polygon.in","r",stdin); 48 //freopen("polygon.out","w",stdout); 49 scanf("%d",&n); 50 for(int i=1;i<=n;i++){ 51 double x,y,z; 52 scanf("%lf%lf%lf",&x,&y,&z); 53 li[++tot].a=x;li[tot].b=y;li[tot].c=z; 54 } 55 ++tot,li[tot].a=-1,li[tot].b=0,li[tot].c=-inf; 56 ++tot,li[tot].a=1,li[tot].b=0,li[tot].c=-inf; 57 ++tot,li[tot].a=0,li[tot].b=-1,li[tot].c=-inf; 58 ++tot,li[tot].a=0,li[tot].b=1,li[tot].c=-inf; 59 for(int i=1;i<=tot;i++){ 60 li[i].ang=atan2(li[i].b,li[i].a); 61 if(li[i].b) li[i].pt=(point){0,-li[i].c/li[i].b}; 62 else li[i].pt=(point){-li[i].c/li[i].a,0}; 63 } 64 sort(li+1,li+tot+1,comp); 65 solve();getans(); 66 printf("%.0lf ",ans); 67 return 0; 68 }