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;
}


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