poj 1408(计算几何)
1,求线段交点。
2,枚举各个面积
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 7 using namespace std; 8 9 struct point 10 { 11 double x,y; 12 }; 13 struct line 14 { 15 point a,b; 16 line(){} 17 line(point aa,point bb) 18 { 19 a=aa; 20 b=bb; 21 } 22 }; 23 24 point intersection(line u,line v) 25 { 26 point ret=u.a; 27 double t=((u.a.x-v.a.x) * (v.a.y-v.b.y) -(u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x)); 28 ret.x+=(u.b.x-u.a.x)*t; 29 ret.y+=(u.b.y-u.a.y)*t; 30 return ret; 31 } 32 33 double area_of_polygon(int n,point * p) 34 { 35 double s; 36 if(n<3) return 0; 37 s=p[0].y*(p[n-1].x-p[1].x); 38 for(int i=1;i<n;i++) 39 s+=p[i].y*(p[i-1].x-p[(i+1)%n].x); 40 return fabs(s/2); 41 } 42 43 double a[35],b[35],c[35],d[35]; 44 int n; 45 46 int main() 47 { 48 while(scanf("%d",&n)) 49 { 50 if(n==0) break; 51 a[0]=b[0]=c[0]=d[0]=0.0; 52 a[n+1]=b[n+1]=c[n+1]=d[n+1]=1.0; 53 for(int i=1;i<=n;i++) 54 scanf("%lf",&a[i]); 55 for(int i=1;i<=n;i++) 56 scanf("%lf",&b[i]); 57 for(int i=1;i<=n;i++) 58 scanf("%lf",&c[i]); 59 for(int i=1;i<=n;i++) 60 scanf("%lf",&d[i]); 61 sort(a,a+n+1); 62 sort(b,b+n+1); 63 sort(c,c+n+1); 64 sort(d,d+n+1); 65 double ans=0; 66 point tmp[4]; 67 point intsec[35][35]; 68 for(int i=0;i<=n+1;i++) 69 { 70 point aa,bb; 71 aa.x=a[i]; 72 aa.y=0; 73 bb.x=b[i]; 74 bb.y=1; 75 line u=line(aa,bb); 76 //cout<<u.a.x<<" "<<u.a.y<<" "<<u.b.x<<" "<<u.b.y<<endl; 77 for(int j=0;j<=n+1;j++) 78 { 79 point cc,dd; 80 cc.x=0; 81 cc.y=c[j]; 82 dd.x=1; 83 dd.y=d[j]; 84 line v=line(cc,dd); 85 intsec[i][j]=intersection(u,v); 86 //cout<<intsec[i][j].x<<" "<<intsec[i][j].y<<endl; 87 } 88 } 89 for(int i=1;i<=n+1;i++) 90 { 91 for(int j=1;j<=n+1;j++) 92 { 93 tmp[0]=intsec[i-1][j]; 94 tmp[1]=intsec[i-1][j-1]; 95 tmp[2]=intsec[i][j-1]; 96 tmp[3]=intsec[i][j]; 97 double t=area_of_polygon(4,tmp); 98 if(t>ans) 99 ans=t; 100 } 101 } 102 printf("%.6f\n",ans); 103 } 104 return 0; 105 }