poj 1039 直线与直线相交的问题

来源:互联网 发布:kanahei知乎 编辑:程序博客网 时间:2024/05/21 22:35

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
40 12 24 16 460 12 -0.65 -4.457 -5.5712 -10.817 -16.550
Sample Output
4.67Through all the pipe.


题意:

给出一个管道

求一束光线最多可以到多远

题解:

枚举管道的折点,得到直线,判断最优即可



#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const double eps=1e-8;const double inf=99999.0;#define MAXN 100struct point{    double x,y;    point(){}    point(double xx,double yy){        x=xx,y=yy;    }};point up[MAXN],down[MAXN];struct line{    point a,b;};int dcmp(double x){    if (fabs (x) < eps) return 0;    else    return x < 0 ? -1 : 1;}double Cross(point p1,point p2,point p3){    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);}double Cross2(point A, point B){    return A.x * B.y - A.y * B.x;}double Dis(point A, point B){    return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));}point operator - (point A,point B){    return point(A.x-B.x, A.y-B.y);}point operator + (point A, point B){    return point(A.x+B.x, A.y+B.y);}point operator * (point A, double p){    return point(A.x*p, A.y*p);}bool operator == (point A, point B){    return (A.x-B.x) == 0 && (A.y-B.y) == 0;}/*判断直线AB、CD是否相交*/bool check(point A,point B,point C,point D){    return (dcmp(Cross(A,B,C)) * dcmp(Cross(A,B,D)) <= 0);}/*计算直线AB、CD的交点横坐标*/double intersection(point A,point B,point C,point D){    double area1=Cross(A,B,C);    double area2=Cross(A,B,D);    int c=dcmp(area1);    int d=dcmp(area2);    if(c*d<0) //CD在AB的两侧,规范相交        return (area2*C.x - area1*D.x)/(area2-area1);  //黑书P357交点计算公式    if(c*d==0)   //CD的其中一个端点在AB上,不规范相交        if(c==0)            return C.x;//C在AB上,返回AB与CD非规范相交时的交点C的横坐标        else            return D.x;//D在AB上,返回AB与CD非规范相交时的交点D的横坐标    return -inf;  //CD在AB同侧,无交点,返回 负无穷}int main(){    int n;    //折点数    int i,j,k;    //freopen("in.txt","r",stdin);    while(scanf("%d",&n),n)    {        double max_x=-inf;  ///最大可见度(管中最远可见点的横坐标)        for(i=1;i<=n;i++){            scanf("%lf%lf",&up[i].x,&up[i].y);            down[i].x=up[i].x;            down[i].y=up[i].y-1;        }        bool flag=false;        point temp;        for(i=1;i<=n;i++) ///枚举所有通过一个上折点、一个下折点的直线        {            for(j=1;j<=n;j++)            {                if(i!=j)                {                    for(k=1;k<=n;k++)     ///直线L最大延伸到第k-1节管子                        if(!check(up[i],down[j],up[k],down[k]))   ///up[k]->down[k]为折点处垂直x轴的直线                            break;                    if(k>n){                        flag=true;                        break;                    }                    else if(k>max(i,j))  ///由于不清楚L究竟是与第k-1节管子的上管壁还是下管壁相交,因此都计算交点,取最优                    {                        double temp=intersection(up[i],down[j],up[k],up[k-1]);                        if(max_x < temp)  ///L与第k-1节管子的上管壁相交                            max_x=temp;                        temp=intersection(up[i],down[j],down[k],down[k-1]);                        if(max_x < temp)  ///L与第k-1节管子的上管壁相交                            max_x=temp;                    }                }            }            if(flag)                break;        }        if(flag)            puts("Through all the pipe.");        else            printf("%0.2lf\n",max_x);    }    return 0;}


原创粉丝点击