POJ1269(直线之间的关系)

传送门

很经典的基础题吧

判断直线之间的关系

有两种情况

平行(包括重合) 和 相交

那么我们首先考虑平行

是不是斜率相等就可以了?

也就是说 Δ x Δ y = = Δ x Δ y \frac{Δx}{Δy}==\frac{Δx'}{Δy'}

但是注意到如果给出的线段与y轴平行,也就是 Δ y = 0 Δy=0 的时候会RE

所以把式子转换一下 变成 Δ x Δ y = Δ x Δ y Δx*Δy'=Δx'*Δy 就完美解决了 这样就不同专门特判了

那么如果两线段又重合呢?

那么也就是说四个点中任意两个向量的叉积就都为0了

那么如果不为0也就是不重合了

那么判一下叉积是否为0就是了

那么再来考虑求交点

对于两条直线 y = a x + b y=ax+b y = a x + b y=a'x+b'

我们可以直接得出 x = b b a a x=\frac{b'-b}{a-a'} , y = a b b + a b a b a a y=\frac{a*b'-b+a*b-a'*b}{a-a'}

当然也可以直接叉积判断

看代码吧

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
#define eps 1e-5
inline int read(){
	char ch=getchar();
	int res=0,f=1;
	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
	return res*f;
}
struct point{
	int x,y;
	point(){}
	point (int a,int b):
		x(a),y(b){};
	friend inline point operator -(const point &a,const point &b){
		return point(a.x-b.x,a.y-b.y);
	}
	friend inline int operator *(const point &a,const point &b){
		return a.x*b.y-a.y*b.x;
	}
}a1,a2,a3,a4;
double ax,bx,cx,dx,ay,by,cy,dy;
int n;
inline void check(){
	if((point(cx-ax,cy-ay)*point(bx-ax,by-ay))==0){
		cout<<"LINE"<<'\n';
	}
	else cout<<"NONE"<<'\n';
}
inline void solve(){
	double s1=(point(dx-ax,dy-ay))*(point(cx-ax,cy-ay));
	double s2=(point(cx-bx,cy-by))*(point(dx-bx,dy-by));
	double c=(bx-ax)*(double)(s1/(s1+s2));
	double d=(by-ay)*(double)(s1/(s1+s2));
	c+=ax;
	d+=ay;
	printf("POINT %.2lf %.2lf\n",c,d);
}
int main(){
//	freopen("223.cpp","r",stdin);
	n=read();
	cout<<"INTERSECTING LINES OUTPUT"<<'\n';
	for(int i=1;i<=n;i++){
		ax=read(),ay=read(),bx=read(),by=read(),cx=read(),cy=read(),dx=read(),dy=read();	       
		if(fabs((double)((double)(bx-ax)*(double)(dy-cy))-(double)((double)(dx-cx)*(double)(by-ay)))<=eps) check();
		else solve();
	}
	cout<<"END OF OUTPUT"<<'\n';
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42555009/article/details/83085400