poj1039 Pipe(计算几何叉积求交点)

F - Pipe
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape the company ran into the problem of determining how far the light can reach inside each component of the pipe. Note that the material which the pipe is made from is not transparent and not light reflecting. 

Each pipe component consists of many straight pipes connected tightly together. For the programming purposes, the company developed the description of each component as a sequence of points [x1; y1], [x2; y2], . . ., [xn; yn], where x1 < x2 < . . . xn . These are the upper points of the pipe contour. The bottom points of the pipe contour consist of points with y-coordinate decreased by 1. To each upper point [xi; yi] there is a corresponding bottom point [xi; (yi)-1] (see picture above). The company wants to find, for each pipe component, the point with maximal x-coordinate that the light will reach. The light is emitted by a segment source with endpoints [x1; (y1)-1] and [x1; y1] (endpoints are emitting light too). Assume that the light is not bent at the pipe bent points and the bent points do not stop the light beam.

Input

The input file contains several blocks each describing one pipe component. Each block starts with the number of bent points 2 <= n <= 20 on separate line. Each of the next n lines contains a pair of real values xi, yi separated by space. The last block is denoted with n = 0.

Output

The output file contains lines corresponding to blocks in input file. To each block in the input file there is one line in the output file. Each such line contains either a real value, written with precision of two decimal places, or the message Through all the pipe.. The real value is the desired maximal x-coordinate of the point where the light can reach from the source for corresponding pipe component. If this value equals to xn, then the message Through all the pipe. will appear in the output file.

Sample Input

4
0 1
2 2
4 1
6 4
6
0 1
2 -0.6
5 -4.45
7 -5.57
12 -10.8
17 -16.55
0

Sample Output

4.67
Through all the pipe.
刘汝佳黑书《算法艺术与信息学艺术》第三章 计算几何初步 的例2  P359
题意:给出一个曲折的管道,求出光线能够到达的管道的最远点的横坐标。
题解: 对于任意一条光线,如果它没有和任意一个拐点相交,那么就可以通过平移使得光线变长,
   如果和一个拐点相交,而不和另一个相交,就可以通过旋转使得它更长。所以最优解的光线一定是与两个拐点相交的一条线。
   那么我们就每次枚举两个管道折点,求一直线看该直线与管道边缘线段交点。在求的过程中,可以从左到右依次判断
   是否与各个折点的横断面相交,然后可以得知是否会与管道壁相交。
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
struct point{
    double x,y;
}p[22],q[22];
const double eps=1e-3; //精度问题
/* 叉积 以c为原点
   若返回值大于0,则cb在ca的逆时针方向  
   若返回值小于0,则cb在射线ca顺时针方向; 
   若返回值等于0,则ca和cb共线,方向可能相同,也可能相反*/  
double cross(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
double Across(point a,point b,point c,point d) //非规范相交也包含在内
{
    double tmp=cross(c,b,a)*cross(d,b,a);
    if(tmp<0||fabs(tmp)<eps) return true;
    return false;
}
//根据ab两点求AB的一般式a1x+b1y+c1=0 cd两点同理
double getIntersect(point a,point b,point c,point d)
{
    //直线ab
    double a1=b.y-a.y;
    double b1=a.x-b.x;
    double c1=a.y*b.x-a.x*b.y;  //x2y1-x1y2
    //直线cd
    double a2=d.y-c.y;
    double b2=c.x-d.x;
    double c2=d.x*c.y-c.x*d.y; //x2y1-x1y2
    //ab和cd交点的横坐标
    double x=(b1*c2-c1*b2)/(b2*a1-b1*a2);
    return x;
}
int main()
{
    int k,n,th=0;
    double best;
    while(scanf("%d",&n)&&n) //scanf比cin快很多很多
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y); //scanf
            q[i].x=p[i].x;
            q[i].y=p[i].y-1; //根据题意 横坐标相同 纵坐标-1
        }
        best=p[0].x;
        th=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i==j)
                    continue;
                for(k=0;k<n;k++)
                {
                    if(!Across(p[i],q[j],p[k],q[k])) //不与横截面相交
                    break;
                }
                //注意以下内容不是k循环里面的
                if(k==n) th=1; //k=n说明through了
                else if (k>max(i,j)) //与两个管道壁相交的横坐标最大值
                best=max(best,max(getIntersect(p[i], q[j], p[k-1], p[k]),getIntersect(p[i], q[j], q[k-1], q[k])));
            } 
        }
        if(th)
        puts("Through all the pipe.");//puts比printf更快
        else
        printf("%.2f
",best);
    }
    return 0;
}
如果与所有横截面都相交,说明through了。

原文地址:https://www.cnblogs.com/Ritchie/p/5498561.html