POJ 1408 Fishnet(计算几何,多边形相关算法)

Description

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



题目大意:

        一个边长为1的正方形框,每天边均有n个钉子,相对的两边的对应钉子之间连上线,组成一个网状结构,交错形成一系列四边形,求这个网状结构中最大的四边形的面积

解题思路:

        因为给定了在边框上的点的座标,所以每次枚举横向纵向的四个点,组成两条线段,求出线段的交点存入数组中,之后每次枚举组成四边形的四个顶点,求出四边形面积,最后取最大值输出即可,四边形的面积可通过划分三角形求得,详见代码。


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <bitset>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>

#define STD_REOPEN() freopen("../in.in","r",stdin)
#define STREAM_REOPEN fstream cin("../in.in")
#define INF 0x3f3f3f3f
#define _INF 63
#define eps 1e-8
#define MAX_V 100010
#define MAX_P 510
#define MAX_E 10010
#define MAX 32000
#define MOD_P 3221225473
#define MOD 9901

using namespace std;

struct Point
{
	double x,y;
	Point(){}
	Point(double a,double b):x(a),y(b){}
	Point operator - (const Point a)const	//求向量
	{
		return Point(x-a.x,y-a.y);
	}
	double operator * (const Point a)const	//点积
	{
		return x*a.x+y*a.y;
	}
	double operator ^ (const Point a)const	//叉积
	{
		return x*a.y-y*a.x;
	}
	double operator & (const Point a)const	//两点间距离
	{
		return sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));
	}
}s[35][35];

int n;

Point intersection(Point a,Point b,Point c,Point d)	//求两线段的交点
{
    double area1=(b-a)^(c-a);
    double area2=(b-a)^(d-a);
    double x=(area2*c.x-area1*d.x)/(area2-area1);
    double y=(area2*c.y-area1*d.y)/(area2-area1);
    return Point(x,y);
}

double getArea(Point a,Point b,Point c,Point d)	//求四点围成的四边形面积
{
	double area1=(d-a)^(c-a);
    double area2=(d-a)^(b-a);
    return (fabs(area1)+fabs(area2))/2.0;
}

int main()
{
	//STD_REOPEN();
	while(scanf("%d",&n),n)
	{
		s[0][0]=Point(0,0);
		s[0][n+1]=Point(0,1.0);
		s[n+1][0]=Point(1.0,0);
		s[n+1][n+1]=Point(1.0,1.0);
		for(int i=1;i<=n;i++)
		{
			scanf("%lf",&s[i][0].x);
			s[i][0].y=0;
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%lf",&s[i][n+1].x);
			s[i][n+1].y=1.0;
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%lf",&s[0][i].y);
			s[0][i].x=0;
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%lf",&s[n+1][i].y);
			s[n+1][i].x=1.0;
		}
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				s[i][j]=intersection(s[i][0],s[i][n+1],s[0][j],s[n+1][j]);
		double ans=0;
		for(int i=0;i<=n;i++)
			for(int j=0;j<=n;j++)
				ans=max(ans,getArea(s[i][j],s[i+1][j],s[i][j+1],s[i+1][j+1]));
		printf("%.6f\n",ans);
	}

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章