P - Fishnet POJ - 1408 (计算几何,线段相交,叉积)

P - Fishnet POJ - 1408 

A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previous night he had encountered a terrible storm and had reached this uninhabited island. Some wrecks of his ship were spread around him. He found a square wood-frame and a long thread among the wrecks. He had to survive in this island until someone came and saved him. 

In order to catch fish, he began to make a kind of fishnet by cutting the long thread into short threads and fixing them at pegs on the square wood-frame. He wanted to know the sizes of the meshes of the fishnet to see whether he could catch small fish as well as large ones. 

The wood frame is perfectly square with four thin edges on meter long: a bottom edge, a top edge, a left edge, and a right edge. There are n pegs on each edge, and thus there are 4n pegs in total. The positions of pegs are represented by their (x,y)-coordinates. Those of an example case with n=2 are depicted in figures below. The position of the ith peg on the bottom edge is represented by (ai,0). That on the top edge, on the left edge and on the right edge are represented by (bi,1), (0,ci) and (1,di), respectively. The long thread is cut into 2n threads with appropriate lengths. The threads are strained between (ai,0) and (bi,1),and between (0,ci) and (1,di) (i=1,...,n). 

You should write a program that reports the size of the largest mesh among the (n+1)2 meshes of the fishnet made by fixing the threads at the pegs. You may assume that the thread he found is long enough to make the fishnet and the wood-frame is thin enough for neglecting its thickness. 
 

Input

The input consists of multiple sub-problems followed by a line containing a zero that indicates the end of input. Each sub-problem is given in the following format. 

a1 a2 ... an 
b1 b2 ... bn 
c1 c2 ... cn 
d1 d2 ... dn 
you may assume 0 < n <= 30, 0 < ai,bi,ci,di < 1

Output

For each sub-problem, the size of the largest mesh should be printed followed by a new line. Each value should be represented by 6 digits after the decimal point, and it may not have an error greater than 0.000001.

Sample Input

2
0.2000000 0.6000000
0.3000000 0.8000000
0.1000000 0.5000000
0.5000000 0.6000000
2
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
4
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
2
0.5138701 0.9476283
0.1717362 0.1757412
0.3086521 0.7022313
0.2264312 0.5345343
1
0.4000000
0.6000000
0.3000000
0.5000000
0

Sample Output

0.215657
0.111112
0.078923
0.279223
0.348958

题意:求线段相交形成的四边形中最大的面积

思路:枚举每个四边形的面积,进行取优

要求四边形面积,就需要知道,四边形每个顶点的坐标,用叉积求面积

向量的叉积

--本质上是有向面积

 

向量a到向量b成逆时针,上述结果大于0;

--向量a到向量b成顺时针,上述结果小于0;

--向量a和向量b共线时(不论同向还是反向),上述结果等于0.

 那么问题就转变成求线段的交点,求交点具体看下面代码

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1000010;
const int mmax = 40200+ 7;
const double esp = 1e-8;
struct point{
double x,y;
point(){
}
point (double _x,double _y):x(_x),y(_y){
}
point operator -(const point &t) const{
return point(x-t.x,y-t.y);
}
double operator ^(const point &t)const{  //叉积
return x*t.y-t.x*y;
}
}dis[44][44];
struct line{
point a,b;
line(){
}
line(point _a,point _b):a(_a),b(_b){
}
point operator &(const line &t)const{  //求线段交点
/*
假设相交的两个线段为ab,cd,线段ab和线段cd的交点为o,则可
根据叉积求四边形面积求出三角形abc的面积s1和三角形abd的面积s2,
则交点即线段cd的中点=c的行坐标+(d的行坐标-c的行坐标)*(s1/(s1+s2))
*/
      point tem;
      double s1=(a-b)^(a-t.a);
      double s2=(a-b)^(a-t.b);
      tem.x=fabs((s2*t.a.x-s1*t.b.x)/(s1-s2));
      tem.y=fabs((s2*t.a.y-s1*t.b.y)/(s1-s2));
      return tem;
}
};


int main(){
      int n;
      while(~scanf("%d",&n)&&n)
      {
       for(int i=1;i<=n;i++){
            scanf("%lf",&dis[i][0].x);
            dis[i][0].y=0;
       }
       for(int i=1;i<=n;i++){
            scanf("%lf",&dis[i][n+1].x);
            dis[i][n+1].y=1.0;
       }
       for(int i=1;i<=n;i++){
            scanf("%lf",&dis[0][i].y);
            dis[0][i].x=0.0;
       }
       for(int i=1;i<=n;i++){
            scanf("%lf",&dis[n+1][i].y);
            dis[n+1][i].x=1.0;
       }
       for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                  dis[i][j]=line(dis[i][0],dis[i][n+1])&line(dis[0][j],dis[n+1][j]);
            }
       }
       dis[0][0].x=dis[0][0].y=dis[0][n+1].x=dis[n+1][0].y=0.0;
       dis[n+1][n+1].x=dis[n+1][n+1].y=dis[0][n+1].y=dis[n+1][0].x=1.0;
       double maxl=-10.0;
       for(int i=0;i<=n;i++){
            for(int j=0;j<=n;j++){
//计算面积
                  double ans=0.5*(fabs(((dis[i][j]-dis[i][j+1])^(dis[i][j]-dis[i+1][j]))-((dis[i+1][j+1]-dis[i][j+1])^(dis[i+1][j+1]-dis[i+1][j]))));
                  if(ans-maxl>esp)
                        maxl=ans;
            }
       }
       printf("%.6f\n",maxl);
      }
      return 0;
}

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/82830582