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

本文详细解析了P-FishnetPOJ-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;
}

 

### 向量法实现线段相交判定 在线段相交判定中,向量是一种高效且优雅的方法。通过利用向量的性质,可以快速判断两条线段是否相交。 #### 的基本定义 设两个二维平面向量 $\vec{a} = (x_1, y_1)$ 和 $\vec{b} = (x_2, y_2)$,则它们的定义为: $$ \text{cross product}(\vec{a}, \vec{b}) = x_1y_2 - x_2y_1 $$ 如果结果大于零,则说明 $\vec{b}$ 在 $\vec{a}$ 的逆时针方向;小于零则是顺时针方向;等于零时表示两者共线[^1]。 #### 判断线段相交的核心原理 为了判断两条线段 $AB$ 和 $CD$ 是否相交,通常采用 **快速排斥实验** 和 **跨立实验**: - **快速排斥实验**: 如果以线段 $AB$ 和 $CD$ 为对角线形成的矩形不重叠,则这两条线段不可能相交- **跨立实验**: 使用向量来验证每一条线段的两端点是否位于另一条线段的不同侧。 具体来说,对于四点 $A(x_A,y_A), B(x_B,y_B), C(x_C,y_C), D(x_D,y_D)$ ,可以通过如下方式计算并检验条件: ```cpp // 定义向量乘函数 double crossProduct(double ax, double ay, double bx, double by){ return ax * by - ay * bx; } bool isIntersected(Point A, Point B, Point C, Point D) { // 计算四个向量差值 Vector AC(C.x-A.x,C.y-A.y); Vector AD(D.x-A.x,D.y-A.y); Vector BC(C.x-B.x,C.y-B.y); Vector BD(D.x-B.x,D.y-B.y); // 计算 double d1 = crossProduct(AC.x, AC.y, AB.x, AB.y); double d2 = crossProduct(AD.x, AD.y, AB.x, AB.y); double d3 = crossProduct(BC.x, BC.y, CD.x, CD.y); double d4 = crossProduct(BD.x, BD.y, CD.x, CD.y); // 跨立实验 if ((d1*d2<0)&&(d3*d4<0)) {return true;} // 特殊情况:考虑端点在另一线段上的情形 if(d1==0 && onSegment(A,B,C)) {return true;} if(d2==0 && onSegment(A,B,D)) {return true;} if(d3==0 && onSegment(C,D,A)) {return true;} if(d4==0 && onSegment(C,D,B)) {return true;} return false; } ``` 上述代码片段展示了如何使用向量来进行线段相交检测[^3]。 #### 空间分析算法扩展到三维场景 当讨论的空间从二维拓展至三维时,线段相交问题变得更加复杂。此时不仅需要考虑平面内的交关系,还需要额外关注第三维度的影响。一种常见的做法是先投影回某个特定坐标面(比如XY平面),再结合高度信息进一步确认真实空间位置的关系[^2]。 ### 结论 综上所述,基于向量线段相交判别方法具有简洁性和高效率的特点,在实际编程比赛中被广泛运用。同时该技术也是更高级别的几何运算如凸包构建、最近邻查找等问题的重要组成部分之一。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值