POJ-1039 判断相交计算交点

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.

枚举两个点(不能是同一管道的上下端点)
连成一条直线,依次判断是否与各垂线(管道)相交
若不相交,则若与该管道与前一个管道之间的两个线段相交,用交点更新ans
特判:如果不能通过第一个管道,不用更新ans
若都相交,则通过全部管道,退出循环
因为第一个管道的x可能为负数,初始ans=p[0].x
用G++交wa的话,试一下C++

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e5+7;
const double inf=1e200;
const double eps=1e-8;
const double pi =acos(-1.0);
int sgn(double x){
	if(fabs(x)<eps)return 0;
	return x>0?1:-1;
}
double hypot(double x,double y){return sqrt(x*x+y*y);}
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
	bool operator ==(const Point& b){return !sgn(x-b.x)&&!sgn(y-b.y);}
	bool operator <(const Point& b){return x==b.x?y<b.y:x<b.x;}
	double len(){return hypot(x,y);}
	double len2(){return x*x+y*y;}
	double dist(Point b){return hypot(x-b.x,y-b.y);}
	void input(){scanf("%lf%lf",&x,&y);}
	void show(){printf("(%.2lf %.2lf)",x,y);}
};
typedef Point Vector;
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);}
Vector operator *(Vector A,double p){return Vector(A.x*p,A.y*p);}
Vector operator /(Vector A,double p){return Vector(A.x/p,A.y/p);}
double operator *(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//点积
double operator ^(Vector A,Vector B){return A.x*B.y-A.y*B.x;}//叉积
double angle(Vector A, Vector B){return acos(A*B/A.len()/B.len());}//夹角
Vector Normal(Vector A){//向量A左转90°的单位法向量
	double L=A.len();
	return Vector(-A.y/L,A.x/L);
}
Vector Rotate(Vector A,double rad){//rad为弧度 逆时针旋转
	double c=cos(rad),s=sin(rad);
	return Vector(A.x*c-A.y*s,A.x*s+A.y*c);
}
Point Rotate(Point S,Point O,double rad){//绕O逆时针选择后点
	Vector v=Rotate(S-O,rad);
	return Point(O.x+v.x,O.y+v.y);
}
struct Line{
	Point s,e;
	Line(){};
	Line(Point s,Point e):s(s),e(e){};
	Line(Point p,double angle){//根据一个点和倾斜角angle 确定直线0<=angle<pi
		s=p;
		if(sgn(angle-pi/2)==0)e=(s+Point(0,1));
		else e=(s+Point(1,tan(angle)));
	}//ax+by+c=0
	Line(double a,double b,double c){
		if(!sgn(a))s = Point(0,-c/b),e=Point(1,-c/b);
		else if(!sgn(b))s=Point(-c/a,0),e=Point(-c/a,1);
		else s=Point(0,-c/b),e=Point(1,(-c-a)/b);
	}
	void adjust(){swap(s,e);}
	int relation(Point p){//点和直线关系1在左侧 2在右侧 3在直线上
		int c=sgn((p-s)^(e-s));
		if(c<0)return 1;
		if(c>0)return 2;
		return 3;
	}
	bool pointonseg(Point p){//点在线段上的判断
		return !sgn((p-s)^(e-s))&&sgn((p-s)*(p-e))<=0;
	}
	bool parallel(Line v){//两向量平行(对应直线平行或重合)
		return !sgn((e-s)^(v.e-v.s));
	}
	int linecrossline(Line v){//两直线关系0平行 1重合 2相交
		if((*this).parallel(v))return v.relation(s)==3;
		return 2;
	}
	int linecrossseg(Line v){//直线和线段相交判断//-*this line -v seg//2 规范相交//1 非规范相交//0 不相交
		int d1=sgn((e-s)^(v.s-s));
		int d2=sgn((e-s)^(v.e-s));
		if((d1^d2)==-2)return 2;
		return !d1||!d2;
	}
	int segcrossseg(Line v){//两线段相交判断//2 规范相交//1 非规范相交//0 不相交
		int d1=sgn((e-s)^(v.s-s));
		int d2=sgn((e-s)^(v.e-s));
		int d3=sgn((v.e-v.s)^(s-v.s));
		int d4=sgn((v.e-v.s)^(e-v.s));
		if((d1^d2)==-2&&(d3^d4)==-2)return 2;
		return (d1==0&&sgn((v.s-s)*(v.s-e))<=0)||(d2==0&&sgn((v.e-s)*(v.e-e))<=0)
		||(d3==0&&sgn((s-v.s)*(s-v.e))<=0)||(d4==0&&sgn((e-v.s)*(e-v.e))<=0);
	}
	double len(){return s.dist(e);}
	double angle(){//返回直线倾斜角0<=angle<pi
		double k=atan2(e.y-s.y,e.x-s.x);
		if(sgn(k)<0)k+=pi;
		if(!sgn(k-pi))k-=pi;
		return k;
	}
	Point crosspoint(Line v){//求两直线的交点//要保证两直线不平行或重合
		double a1=(v.e-v.s)^(s-v.s);
		double a2=(v.e-v.s)^(e-v.s);
		return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
	}
	double dispointtoline(Point p){return fabs((p-s)^(e-s))/len();}//点到直线的距离
	double dispointtoseg(Point p){//点到线段的距离
		if(sgn((p-s)*(e-s))<0||sgn((p-e)*(s-e))<0)return min(p.dist(s),p.dist(e));
		return dispointtoline(p);
	}
	double dissegtoseg(Line v){//返回线段到线段的距离//前提是两线段不相交,相交距离就是0了
		return min(min(dispointtoseg(v.s),dispointtoseg(v.e)),min(v.dispointtoseg(s),v.dispointtoseg(e)));
	}
	Point lineprog(Point p){return s+(((e-s)*((e-s)*(p-s)))/((e-s).len2()));}//返回点p 在直线上的投影
	Point symmetrypoint(Point p){//返回点p 关于直线的对称点
		Point q = lineprog(p);
		return Point(2*q.x-p.x,2*q.y-p.y);
	}
	void input(){s.input(),e.input();}
};
Point p[42];
Line l[42],L[42];
int main(){
	int n;
	while(scanf("%d",&n),n){
		for(int i=0;i<n;++i)p[i<<1].input(),p[i<<1|1]=Point(p[i<<1].x,p[i<<1].y-1);
		for(int i=0;i<n;++i)l[i]=Line(p[i<<1],p[i<<1|1]),L[i<<1]=Line(p[i<<1],p[(i-1)<<1]),L[i<<1|1]=Line(p[i<<1|1],p[(i-1)<<1|1]);
		int ok=1;
		double ans=p[0].x;
		for(int s=0;ok&&s<n<<1;++s)
			for(int i=s/2*2+2;ok&&i<n<<1;++i){
				Line sp=Line(p[s],p[i]);
				int j;
				for(j=0;j<n;++j)
					if(!sp.linecrossseg(l[j])){
						if(j==0)break;
						if(sp.linecrossseg(L[j<<1]))ans=max(ans,sp.crosspoint(L[j<<1]).x);
						if(sp.linecrossseg(L[j<<1|1]))ans=max(ans,sp.crosspoint(L[j<<1|1]).x);
						break;
					}
				if(j==n)ok=0;
			}
		if(!ok)puts("Through all the pipe.");
		else printf("%.2lf\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值