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


代码:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面只是目标目录 ├─第1章-Shiro权限实战课程介绍 │ 1-1Shiro权限实战课程介绍.mp4 │ 1-2权限控制和初学JavaWeb处理访问权限控制.mp4 │ ├─第2章-大话权限框架核心知识ACL和RBAC │ 2-1权限框架设计之ACL和RBAC讲解.mp4 │ 2-2主流权限框架介绍和技术选型讲解.mp4 │ ├─第3章-ApacheShiro基础概念知识和架构讲解 │ 3-1Shiro核心知识之架构图互和四大模块讲解.mp4 │ 3-2用户访问Shrio权限控制运行流程和常见概念讲解.mp4 │ ├─第4章-Springboot2.x整合ApacheShiro快速上手实战 │ 4-1SpringBoot2.x整合Shiro.mp4 │ 4-2快速上手之Shiro认证和授权流程实操上集.mp4 │ 4-3Shiro认证和授权流程和常用API梳理下集.mp4 │ ├─第5章-详细讲解ApacheShirorealm实战 │ 5-1Shiro安全数据来源之Realm讲解.mp4 │ 5-2快速上手之Shiro内置IniRealm实操.mp4 │ 5-3快速上手之Shiro内置JdbcRealm实操.mp4 │ 5-4ApacheShiro自定义Readl实战.mp4 │ 5-5深入Shiro源码解读认证授权流程.mp4 │ ├─第6章-Shiro权限认证Web案例知识点讲解 │ 6-1Shiro内置的Filter过滤器讲解.mp4 │ 6-2Shiro的Filter配置路径讲解.mp4 │ 6-3Shiro数据安全之数据加解密.mp4 │ 6-4Shiro权限控制注解和编程方式讲解.mp4 │ 6-5Shiro缓存模块讲解.mp4 │ 6-6ShiroSession模块讲解.mp4 │ ├─第7章-ApacheShiro整合SpringBoot2.x综合案例实战 │ 7-10使用ShiroLogout和加密处理.mp4 │ 7-1Shiro整合SpringBoot2.x案例实战介绍.mp4 │ 7-2基于RBAC权限控制实战之Mysql数据库设计.mp4 │ 7-3SpringBoot2.x项目框架和依赖搭建.mp4 │ 7-4案例实战之权限相关服务接口开发.mp4 │ 7-5案例实战之用户角色权限多对多关联查询SQL.mp4 │ 7-6案例实战自定义CustomRealm实战.mp4 │ 7-7项目实战之ShiroFilterFactoryBean配置实战.mp4 │ 7-8前后端分离自定义SessionManager验证.mp4 │ 7-9API权限拦截验证实战.mp4 │ ├─第8章-权限控制综合案例实战进阶 │ 8-1实战进阶之自定义ShiroFilter过滤器上集.mp4 │ 8-2实战进阶之自定义ShiroFilter过滤器下集.mp4 │ 8-3性能提升之Redis整合CacheManager.mp4 │ 8-4性能提升之Redis整合SessionManager.mp4 │ 8-5ShiroConfig常用bean类配置.mp4 │ ├─第9章-大话分布式应用的鉴权方式 │ 9-1单体应用到分布式应用下的鉴权方式介绍.mp4 │ 9-2Shiro整合SpringBoot下自定义SessionId.mp4 │ ├─第10章-Shiro课程总结 │ 10-1Apacheshiro从入门到高级实战课程总结.mp4 │ 10-2高级工程师到架构师-解决问题思路+学习方法.mp4 │ └─课件资料.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值