Fishnet——计算几何 求两直线交点、面积

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. 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.
n
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

题意:指定一个n。在一个边长为1的正方形每条边的给定的位置钉上钉子,并且用线将这些钉子对边用线连起来,输入顺序是 底 边 → 顶 边 → 左 边 → 右 边 底边 \rightarrow 顶边 \rightarrow 左边 \rightarrow 右边 底边的y坐标都是0,顶边的y坐标都是1,左边的x左边都是0,右边的x坐标都是1。求:被线分割的四边形中的最大的一个的面积。

思路:我们首先求得坐标系中间的交点坐标,然后通过叉积来求被线分割的面积,枚举所有的面积进行比较即可得出最大面积。

  • 首先来解决交点的坐标,参考博客
  • 我们得到了每个交点的坐标,进而我们利用交点坐标求叉积,即可得出每个被分割的四边形的面积,枚举所有情况即可得到最终答案。

c++ AC 代码

#include<cmath>
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 40
using namespace std;

int n;

struct Point
{
    double x,y;
}pset[N+1][N+1];

struct Line
{
    void L(Point pp1,Point pp2)
    {
        p1 = pp1;
        p2 = pp2;
    }
    Point p1,p2;
    double a,b,c;
}lineset[N][N];

void GetLinePara(Line *l)
{
    l->a = l->p1.y - l->p2.y;
    l->b = l->p2.x - l->p1.x;
    l->c = l->p1.x * l->p2.y - l->p1.y * l->p2.x;
}

Point GetCrossPoint(Line *l1,Line *l2)
{
    GetLinePara(l1);
    GetLinePara(l2);
    double D = l1->a * l2->b - l2->a * l1->b;
    Point p;
    p.x = (l1->b * l2->c - l2->b * l1->c) / D;
    p.y = (l2->a * l1->c - l1->a * l2->c) / D;
    return p;
}

void Init_Points()
{
    int i;
    for(i = 1;i<=n;i++)
        scanf("%lf",&pset[i][0].x), pset[i][0].y = 0;
    for(i = 1;i<=n;i++)
        scanf("%lf",&pset[i][n+1].x), pset[i][n+1].y = 1;
    for(i = 1;i<=n;i++)
        scanf("%lf",&pset[0][i].y), pset[0][i].x = 0;
    for(i = 1;i<=n;i++)
        scanf("%lf",&pset[n+1][i].y), pset[n+1][i].x = 1;

    pset[0][0].x = pset[0][0].y = 0;
    pset[0][n+1].x = 0; pset[0][n+1].y = 1;
    pset[n+1][0].x = 1; pset[n+1][0].y = 0;
    pset[n+1][n+1].x = pset[n+1][n+1].y = 1;
}

void Init_Lines()
{
    int i,j;
    Line l1,l2;
    for(i = 1;i<=n;i++)
        for(j = 1;j<=n;j++)
        {
            l1.L(pset[i][0],pset[i][n+1]);
            l2.L(pset[0][j],pset[n+1][j]);
            pset[i][j] = GetCrossPoint(&l1,&l2);
        }
}

double Det(Point a,Point b,Point c)
{
    return (c.x-a.x) * (c.y-b.y) - (c.y-a.y) * (c.x-b.x);
}

double GetArea(int i,int j)
{
    double area1 = Det(pset[i][j],pset[i+1][j],pset[i+1][j+1]) / 2;
    double area2 = Det(pset[i][j],pset[i][j+1],pset[i+1][j+1]) / 2;
    return fabs(area1) + fabs(area2);
}

int main()
{
    while(scanf("%d",&n) && n)
    {
        Init_Points();
        Init_Lines();
        double maxarea = 0;
        double area;
        for(int i=0;i<n+1;i++)
            for(int j=0;j<n+1;j++)
            {
                area = GetArea(i,j);
                maxarea = area > maxarea ? area : maxarea;
            }
        printf("%lf\n",maxarea);
    }
    return 0;
}

代码参考自:算法之美——求两直线交点(三维叉积)——求四边形面积(二维叉积)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值