O - Pipe POJ - 1039 (计算几何好题)

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.

 题意:给定一个管道的n个上端点,下端点的y值=上端点的y值-1,x值相等,求从管道开始出发的直线最远可以到达的管道的x值

思路:很容易知道,直线必定通过管道的某个上端点和某个下端点,可以枚举,这里运用到一个计算直线与直线交点的公式:

对a=(x1,y1),a1=(x2,y2),b=(x3,y3),b1=(x4,y4)

已知

重构方法减‘-’ a-b为返回一个点结构体x=x1-x2,y=y1-y2

重构方法内积a*b=x1*x2+y1*y2  ,等式右边的 * 为常规乘法

重构方法外积a^b=x1*y2-x2*y1

则两个相交直线a-a1和b-b1的交点为:

a+((b1-b)^(b-a))/((b1-b)^(a1-a))(a1-a)

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1000010;
const int mmax = 40200+ 7;
const double esp = 1e-5;
struct point
{
    double x,y;
    point() {} 
    //初始化
    point(double _x,double _y):x(_x),y(_y)
    {
    }
    /*
    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.x+y*t.y;
    }
    double operator ^(const point &t)const  //外积/叉积
    {
        return x*t.y-t.x*y;
    }
} down[33],up[33];
int check(double x)
{
    if(fabs(x) operator &(const line &t)const //计算直线p-q与t.p-t.q的交点
    {
          point tem=q;
      /*
        if(check((p-q)^(t.p-t.q))==0)
        {
            if(check((p-t.q)^(t.p-t.q))==0)
                return make_pair(0,tem);  //重合
            else
                return make_pair(1,tem); // 平行
        }
        */
        double r=((t.p-t.q)^(t.q-q))/((t.p-t.q)^(p-q));
        tem.x+=(p.x-q.x)*r;
        tem.y+=(p.y-q.y)*r;
        return make_pair(2,tem);  
        //make_pair的作用只要是为了返回两个值,这里也可以将返回值改为point型
    }
};

bool judge(line a,line b)  //判断直线a与线段b是否相交
{
    return check((b.p-a.p)^(a.p-a.q))*check((b.q-a.p)^(a.p-a.q))<=0; 
    /*线段的两端点与直线两个点分别在直线两端时香蕉水,
    相交情况下这两条直线的斜率一个大于a的斜率,一个小于ad的斜率
    */
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=0; imax(i,j))
                {
                  //对上下管道进行取优
                    if(judge(line(up[i],down[j]),line(up[k-1],up[k])))  
                    {
                        pairs=line(up[i],down[j])&line(up[k-1],up[k]);  
                        point node=s.second;
                        maxl=max(maxl,node.x);
                    }
                    if(judge(line(up[i],down[j]),line(down[k-1],down[k])))
                    {
                        pairs=line(up[i],down[j])&line(down[k-1],down[k]);
                        point node=s.second;
                        maxl=max(maxl,node.x);
                    }
                }
            }
            if(flag==0)
                break;
        }
        if(!flag)
            printf("Through all the pipe.\n");
        else
            printf("%.2f\n",maxl);
    }
    return 0;
}

 

你可能感兴趣的:(计算几何(内积外积运用),算法小结)