(beginer)线段求交:多边形面积 UVA 1301 - Fishnet

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 (Figure 1). 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 one meter long.. a bottom edge, a top edge, a left edge, and a right edge. There aren pegs on each edge, and thus there are 4n pegs in total. The positions ofpegs are represented by their (x, y) -coordinates. Those of an example case with n = 2 are depicted in Figures 2 and 3. The position of the i th 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 into2n 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 that the wood-frame is thin enough for neglecting its thickness.

\epsfbox{p2402a.eps}

\epsfbox{p2402b.eps}

\epsfbox{p2402c.eps}

Input 

The input consists of multiple subproblems followed by a line containing a zero that indicates the end of input. Each subproblem is given in the following format.


n

a1a2 ... an

b1b2 ... bn

c1c2 ... cn

d1d2 ... dn


An integer n followed by a newline is the number of pegs on each edge.a1,..., an , b1,..., bn , c1,...,cn , d1,..., dn are decimal fractions, and they are separated by a space character except thatan, bn, cn anddn are followed by a new line. Eachai (i = 1,...,n) indicates the x -coordinate of thei th peg on the bottom edge. Each bi (i = 1,..., n) indicates thex -coordinate of the i th peg on the top edge. Eachci (i = 1,...,n) indicates the y -coordinate of thei th peg on the left edge. Each di (i = 1,..., n) indicates they -coordinate of the i th peg on the right edge. The decimal fractions are represented by 7 digits after the decimal point. In addition you may assume that0 < n$ \le$30 ,0 < a1 < a2 < ...< an < 1 , 0 < b1 <b2 < ... < bn < 1 , 0 < c1 < c2 < ... < cn < 1 and0 < d1 < d2 < ...< dn < 1 .

Output 

For each subproblem, 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.3000000 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的正方形中连一些线,那就会划分出一些区域,求出这些区域里面的面积的最大值。(大概这意思,连线不是随遍连的,看题目吧)


思路:根据给出的线可以求出各个交点,我们记录下某个交点是“第几行第几列”相交得到的。然后最后只需要求各个四边形的面积就可以了。四边形的面积可以转换为两个三角形的面积。具体看代码

#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define eps 1e-10

const double PI = 4*atan(1.0);
struct Point
{
	Point (double xx=0,double yy=0) : x(xx) , y(yy) { }
	double x;
	double y;
};

typedef Point Vector;
Vector operator+(Vector  v1,Vector  v2) { return Vector(v1.x+v2.x,v1.y+v2.y); }
Vector operator-(Vector  v1,Vector  v2) { return Vector(v1.x-v2.x,v1.y-v2.y); }
Vector operator*(Vector  v, double p) { return Vector(v.x*p,v.y*p); }
Vector operator/(Vector  v,double p) { return Vector(v.x/p,v.y/p); }

bool operator < (Point  a,Point  b) { return a.x < b.x || (a.x==b.x && a.y > b.y); }
int dcmp(double x) 
{
	if (fabs(x) < eps) return 0;
	return x < 0 ? -1 : 1; 
}
bool operator==(const Point & a,const Point & b) 
{
	return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
}

inline double toRad(double x) { return x * PI/180; }
inline double toDegreed(double rad) { return rad*180/PI; }
double Dot(Vector  A,Vector  B) { return A.x*B.x+A.y*B.y; }
double Length(Vector  A) { return sqrt(Dot(A,A)); }
double Angle(Vector A,Vector B) { return acos(Dot(A,B)/Length(A)/Length(B)); }
double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; }
double Area2(Point a,Point b,Point c) {  return Cross(b-a,c-a); }
Vector Rotate(Vector A,double rad) 
{
	return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal(Vector A) { double L = Length(A); return Vector(-A.y/L,A.x/L); }

//点和直线
Point GetLineIntersection(Point P,Vector v,Point Q,Vector w)
{
	Vector u = P-Q;
	double t = Cross(w,u) / Cross(v,w);
	return P+v*t;
}

double DistanceToLine(Point P,Point A,Point B) 
{
	Vector v1 = B-A , v2 = P-A;
	return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceToSegment(Point P,Point A,Point B)
{
	if (A==B) return Length(P-A);
	Vector v1 = B-A , v2 = P-A , v3 = P-B;
	if (dcmp(Dot(v1,v2)) < 0) return Length(v2);
	else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
	else return fabs(Cross(v1,v2))/Length(v1);
}
Point GetLineProjection(Point P,Point A,Point B) 
{
	Vector v = B-A;
	return A+v*(Dot(v,P-A)/Dot(v,v));
}
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2) 
{
	double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1) ,
		     c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
	return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4)<0;
}
bool OnSegment(Point p,Point a,Point b) {
	return dcmp(Cross(a-p,b-p))==0 && dcmp(Dot(a-p,b-p)) < 0;
}
//--------------------------------------------------------------------------------------------
const int maxn = 30+5;
int n;
Point a[maxn] , b[maxn] , c[maxn] , d[maxn];
Point Inter[maxn][maxn];
double ans;

void input() 
{
	for (int i = 1 ; i <= n ; ++i)
	{
		scanf("%lf",&a[i].x);
		Inter[0][i] = a[i];
	}
	for (int i = 1 ; i <= n ; ++i) {
		scanf("%lf",&b[i].x); 
		b[i].y = 1;
		Inter[n+1][i] = b[i];
	}
	for (int i = 1 ; i <= n ; ++i) {
		scanf("%lf",&c[i].y);
		Inter[i][0] = c[i];
	}
	for (int i = 1 ; i <= n ; ++i) 
	{
		scanf("%lf",&d[i].y); 
		d[i].x = 1;
		Inter[i][n+1] = d[i];
	}

	for (int i = 1 ; i <= n ; ++i) {
		Vector v = b[i]-a[i];
		for (int j = 1 ; j <= n ; ++j) {
			Vector w = d[j]-c[j];
			Inter[j][i] = GetLineIntersection(b[i],v,d[j],w);
		}
	}
	Inter[n+1][0] = Point(0,1);
	Inter[0][n+1] = Point(1,0);
	Inter[n+1][n+1] = Point(1,1);
}

void solve()
{
	ans = 0;
	for (int i = 0 ; i <= n ; ++i) {
		for (int j = 0 ; j <= n ; ++j) {
			double S1 = fabs(Area2(Inter[i][j],Inter[i+1][j],Inter[i][j+1]));
			double S2 = fabs(Area2(Inter[i+1][j],Inter[i][j+1],Inter[i+1][j+1]));
			ans = max(ans,0.5*(S1+S2));
		}
	}
	printf("%.6lf\n",ans);
}

int main()
{
	while (scanf("%d",&n)==1,n) {
		input();
		solve();
	}
}


代码:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值