zoukankan      html  css  js  c++  java
  • POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 8280   Accepted: 2483

    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.

    Source

     
     
     
     
    很好的题目。
    意思需要先理解。
    就是从前面一段过来的光线,问最远可以射到哪,只能直射。
     
     
    最远的那条光线肯定过一个上端点和一个下端点。枚举两个点,然后判断,求交点。
     
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    
    using namespace std;
    
    const double eps = 1e-8;
    int sgn(double x)
    {
        if(fabs(x) < eps)return 0;
        if(x < 0)return -1;
        else return 1;
    }
    struct Point
    {
        double x,y;
        Point(){}
        Point(double _x,double _y)
        {
            x = _x;y = _y;
        }
        Point operator -(const Point &b)const
        {
            return Point(x - b.x,y - b.y);
        }
        //叉积
        double operator ^(const Point &b)const
        {
            return x*b.y - y*b.x;
        }
        //点积
        double operator *(const Point &b)const
        {
            return x*b.x + y*b.y;
        }
        void input()
        {
            scanf("%lf%lf",&x,&y);
        }
    };
    struct Line
    {
        Point s,e;
        Line(){}
        Line(Point _s,Point _e)
        {
            s = _s;e = _e;
        }
        //两直线相交求交点
        //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
        //只有第一个值为2时,交点才有意义
        pair<int,Point> operator &(const Line &b)const
        {
            Point res = s;
            if(sgn((s-e)^(b.s-b.e)) == 0)
            {
                if(sgn((s-b.e)^(b.s-b.e)) == 0)
                    return make_pair(0,res);//重合
                else return make_pair(1,res);//平行
            }
            double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
            res.x += (e.x-s.x)*t;
            res.y += (e.y-s.y)*t;
            return make_pair(2,res);
        }
    };
    //判断直线和线段相交
    bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
    {
        return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0;
    }
    
    Point up[100],down[100];
    int main()
    {
        int n;
        while(scanf("%d",&n) == 1 && n)
        {
            for(int i = 0;i < n;i++)
            {
                up[i].input();
                down[i] = up[i];
                down[i].y -= 1;
            }
            bool flag = false;//穿过所有的标记
            double ans = -10000000.0;
            int k;
            for(int i = 0;i < n;i++)
            {
                for(int j = i+1;j < n;j++)
                {
                    for(k = 0;k < n;k++)
                        if(Seg_inter_line(Line(up[i],down[j]),Line(up[k],down[k])) == false)
                            break;
                    if(k >= n)
                    {
                        flag = true;
                        break;
                    }
                    if(k > max(i,j))
                    {
                        if(Seg_inter_line(Line(up[i],down[j]),Line(up[k-1],up[k])))
                        {
                            pair<int,Point>pr = Line(up[i],down[j])&Line(up[k-1],up[k]);
                            Point p = pr.second;
                            ans = max(ans,p.x);
                        }
                        if(Seg_inter_line(Line(up[i],down[j]),Line(down[k-1],down[k])))
                        {
                            pair<int,Point>pr = Line(up[i],down[j])&Line(down[k-1],down[k]);
                            Point p = pr.second;
                            ans = max(ans,p.x);
                        }
                    }
    
                    for(k = 0;k < n;k++)
                        if(Seg_inter_line(Line(down[i],up[j]),Line(up[k],down[k])) == false)
                            break;
                    if(k >= n)
                    {
                        flag = true;
                        break;
                    }
                    if(k > max(i,j))
                    {
                        if(Seg_inter_line(Line(down[i],up[j]),Line(up[k-1],up[k])))
                        {
                            pair<int,Point>pr = Line(down[i],up[j])&Line(up[k-1],up[k]);
                            Point p = pr.second;
                            ans = max(ans,p.x);
                        }
                        if(Seg_inter_line(Line(down[i],up[j]),Line(down[k-1],down[k])))
                        {
                            pair<int,Point>pr = Line(down[i],up[j])&Line(down[k-1],down[k]);
                            Point p = pr.second;
                            ans = max(ans,p.x);
                        }
                    }
                }
                if(flag)break;
            }
            if(flag)printf("Through all the pipe.
    ");
            else printf("%.2lf
    ",ans);
        }
        return 0;
    }
     
     
  • 相关阅读:
    HTML5 实现Link跳线效果
    在TWaver的Tree节点上画线
    用TWaver加载大型游戏场景一例
    22万个木箱!TWaver 3D极限压榨
    如何在MONO 3D寻找最短路路径
    如何创建TWaver 3D的轮廓选中效果
    巧用TWaver 3D 矢量图形功能
    如何实现TWaver 3D颜色渐变
    HDU 1390 Binary Numbers
    HDU 1328 IBM Minus One
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3192590.html
Copyright © 2011-2022 走看看