Language:
Pipe
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. |
题意:给出一个管道拐角处坐标在管道入口用光照射求光线能射到的最大X坐标值
解题思路:枚举管道上下链接的每一条连线算出该线由入口开始能够到达的最大X值。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define eps 1e-8
using namespace std;
struct point{
double x,y;
}A[30];
double cp(point p1,point p2,point p3){
return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}
double MAX(double a,double b){
return a>b?a:b;
}
bool judge(point p1,point p2,point p3,point p4){
double l=cp(p1,p2,p3);
double r=cp(p1,p2,p4);
if(l*r<=0)return true;
return false;
}
bool judge1(point p1,point p2,point p3,point p4){
double l=cp(p1,p2,p3);
double r=cp(p1,p2,p4);
if(l*r>=0)return false;
return true;
}
void findinter(point p1,point p2,point p3,point p4,point &inter){
inter=p1;
double ans=((p1.x-p3.x)*(p3.y-p4.y)-(p1.y-p3.y)*(p3.x-p4.x))/((p1.x-p2.x)*(p3.y-p4.y)-(p1.y-p2.y)*(p3.x-p4.x));
inter.x+=(p2.x-p1.x)*ans;
inter.y+=(p2.y-p1.y)*ans;
}
point botpoi(point p){
p.y--;
return p;
}
int main()
{
int t,i,j,k,n;
double XMAX;
while(scanf("%d",&n),n){
for(i=0;i<n;++i){
scanf("%lf%lf",&A[i].x,&A[i].y);
}
XMAX=A[0].x;point inter;
for(i=0;i<n;++i){
for(j=0;j<n;++j){//枚举管道上下管壁的连线
if(judge(A[i],botpoi(A[j]),A[0],botpoi(A[0]))){//该直线能由入口传来即判断是否和入口相交
for(k=1;k<n;++k){//枚举每一节管道
if(!judge(A[i],botpoi(A[j]),A[k],botpoi(A[k]))){//该直线不能通过该管道
if(judge1(A[i],botpoi(A[j]),A[k],A[k-1])){//该直线交于此节管道的上壁
findinter(A[i],botpoi(A[j]),A[k],A[k-1],inter);//计算交点
XMAX=MAX(XMAX,inter.x);
break;//因为不能通过此节管道所以不用再往后枚举下同
}
if(judge1(A[i],botpoi(A[j]),botpoi(A[k]),botpoi(A[k-1]))){//该直线交于此节管道下壁
findinter(A[i],botpoi(A[j]),botpoi(A[k]),botpoi(A[k-1]),inter);
XMAX=MAX(XMAX,inter.x);
break;
}
XMAX=MAX(XMAX,A[k-1].x);//既没有交于上壁也没有交于下壁既考虑上一个端点
break;
}
}
if(k==n){XMAX=A[n-1].x;break;}//该直线没有交于任何管道即通过了全部管道。
}
}
}
if(XMAX>=A[n-1].x)
printf("Through all the pipe.\n");
else
printf("%.2lf\n",XMAX);
}
return 0;
}