【判断直线相对位置关系】 POJ 1269

月光林地传送门

【题目大意】给定n组数据,每一组数据是四个互不相同的点,前两个点P1,P2表示直线l1,后两个点Q1,Q2表示直线l2。

给你P1,P2,Q1,Q2的坐标,判断l1和l2的关系【平行、重合、相交】,如果相交要把交点坐标算出来。

模板题。用叉积判位置。两个向量a、b如果叉积为0,则平行或重合——把一个点到另一条直线上两点的两个向量叉积一下,若为0则重合,非0就平行。

如果叉积不为0,就套公式求交点。【时空扭曲】

【代码】:

#include<iostream>
#include<cstdio>
using namespace std;
struct point{
	double x,y;
	point(double m=0,double n=0){x=m,y=n;}
	friend inline point operator +(const point &a,const point &b){
		return point(a.x+b.x,a.y+b.y);
	}
	friend inline point operator -(const point &a,const point &b){
		return point(a.x-b.x,a.y-b.y);
	}
	friend inline double operator *(const point &a,const point &b){
		return a.x*b.y-a.y*b.x;
	}
	friend inline point operator *(const point &a,const double &k){
		return point(a.x*k,a.y*k);
	}
	friend inline point operator /(const point &a,const double &k){
		return point(a.x/k,a.y/k);
	}
	friend inline double dot(const point &a,const point &b){
		return a.x*b.x+a.y*b.y;
	}
}a,b,c,d;
bool on(const point &x,const point &y,const point &z){
	double det=(x-z)*(y-z);
	if(det!=0)
		return 0;
	return 1;
}
int T;
int main(){
	printf("INTERSECTING LINES OUTPUT\n");
	scanf("%d",&T);
	while(T--){
		cin>>a.x>>a.y;
		cin>>b.x>>b.y;
		cin>>c.x>>c.y;
		cin>>d.x>>d.y;
		double det=(b-a)*(d-c);
		if(det==0){
			if(on(c,d,a)){
				puts("LINE");
				continue;
			}
			puts("NONE");
		}
		else{
			double S1=(c-a)*(c-d);
			double S2=(c-d)*(c-b);
			double k=S1/(S1+S2);
			point E=(b-a)*k;
			printf("POINT %.2lf %.2lf\n",a.x+E.x,a.y+E.y);
		}
	}
	puts("END OF OUTPUT\n");
}

猜你喜欢

转载自blog.csdn.net/g21wcr/article/details/83095897