poj1039——直线与线段相交(求交点)

题目链接:poj.org/problem?id=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.

题目翻译:

就是从前面一段过来的光线,问最远可以射到哪,只能直射。

算法书(黑书)上的原题,有关题的具体过程不讲了,书里面已经讲得很详细了。

只提一个结论:最远的光线一定过一个上端点和一个下端点。

然后就可以枚举,判断是否符合条件,求交点就行了(交到上管壁和下管壁两种情况分类讨论)。

#include<iostream>
#include<cmath>
#include<cstdio> 
using namespace std;
const double INF=999999.0;
const double eps=1e-3;
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
}up[2005],down[2005];
int n;
typedef Point Vector;
int sgn(double p)//精度误差 
{
	if(fabs(p)<eps)    
		return 0;       
	return p>0?1:-1;
}
Vector operator + (Vector A, Vector B){
    return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Point A, Point B){
    return Vector(A.x-B.x, A.y-B.y);
}
double Cross(Point A,Point B)//叉乘求三角形有向面积 
{
	return A.x*B.y-B.x*A.y;
}
double ToLeftTest(Point A,Point B,Point P)//P在AB的左侧还是右侧 
{
	return Cross(B-A,P-A);
}
bool SegLineCross(Point A,Point B,Point C,Point D)//判断直线AB和CD是否相交 
{
	return (sgn(ToLeftTest(A,B,C))*sgn(ToLeftTest(A,B,D))<=0);
}
double Intersection(Point A,Point B,Point C,Point D){//返回直线和线段相交的横坐标 
	double S1=ToLeftTest(A,B,C);
	double S2=ToLeftTest(A,B,D);
	int c=sgn(S1);
	int d=sgn(S2);
	if(c*d<0) //规范相交 
		return (S2*C.x-S1*D.x)/(S2-S1);//书上的公式 
	if(c*d==0)//不规范相交 
		if(c==0)
			return C.x;
		else
			return D.x;
	return -INF;//不相交 
}
int main(){
	while(~scanf("%d",&n)&&n){
		for(int 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;
		}
		double maxx=-INF;//初始化最大长度 
		int flag=0,k;//标记能否贯通整个管子 
		for(int i=1;i<=n;i++){//枚举上折点和下折点 
			for(int j=1;j<=n;j++){
				if(i!=j){
					for(k=1;k<=n;k++)
						if(!SegLineCross(up[i],down[j],up[k],down[k]))//找到最大延伸到的管子k 
							break;
					if(k>n){
						flag=1;
						break;
					}	
					if(k>max(i,j)){
						double temp=Intersection(up[i],down[j],up[k],up[k-1]);//上管壁 
						maxx=max(maxx,temp);
						temp=Intersection(up[i],down[j],down[k],down[k-1]);//下管壁 
						maxx=max(maxx,temp);
					}
				}
				
			}
			if(flag) break;
		}	
		if(flag) printf("Through all the pipe.\n");
		else printf("%.2f\n",maxx);
	}
	return 0;
}


 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值