计算几何——Intersecting Lines(两个线段相交方式的判断)

博客围绕平面上两条线相交问题展开,介绍了两条线相交的三种方式,给出程序输入输出要求,输入为描述线对数量的整数及点坐标,输出需说明相交情况,若为点相交要输出坐标,还提到这是线段相交板子题,要注意特殊情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=1269

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.

Input

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".

Sample Input

5
0 0 4 4 0 4 4 0
5 0 7 6 1 0 2 3
5 0 7 6 3 -6 4 -3
2 0 2 27 1 5 18 5
0 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT

题意翻译:

我们都知道,平面上的一对不同点定义一条线,平面上的一对线将以三种方式之一相交:1) 没有交点,因为它们是平行的,2) 在一条线中相交,因为它们彼此位于顶部(即它们是同一条线),3)在一个点中相交。在此问题中,您将使用代数知识创建一个程序,确定两条线的相交方式和位置。‎
‎程序将在四个点中重复读取,这些点定义 x-y 平面中的两条线,并确定这些线的相交方式和位置。此问题所需的所有数字都是合理的,例如 -1000 和 1000 之间。‎

‎输入‎

‎第一行包含介于 1 和 10 之间的整数 N,描述表示的行数对数。下一个 N 行将各包含八个整数。这些整数表示平面上四个点的坐标,顺序为 x1y1x2y2x3x3x4y4。因此,这些输入线中的每一条代表平面上的两条线:通过 (x1,y1) 和 (x2,y2) 的线和通过 (x3,y3) 和 (x4,y4) 的线。点 (x1,y1) 始终与 (x2,y2) 不同。与 (x3,y3) 和 (x4,y4) 类似。‎

‎输出‎

‎应该有 N+2 行输出。第一行输出应读取"相线输出"。然后,每对平面线将有一条输出线,由输入线表示,描述这些直线的相交方式:无、线或点。如果交点是一个点,则程序应输出点的 x 和 y 坐标,更正为小数点后两位。输出的最后一行应为"输出结束"。

两个线段相交的板子题,只不过要注意几个特殊情况,

#include <iostream>
#include<cmath>
#include<cstdio> 
#include<algorithm>
const double eps=1e-6;
using namespace std;
typedef double db;
struct Point{
	db x,y;
	Point(db x=0,db y=0):x(x),y(y){}
};
typedef Point Vector;
int dcmp(db p,db v){
	if(fabs(p-v)<eps) return 0;
	if(p>v) return 1;
	return -1;
}
int sgn(db x){
	if(fabs(x)<eps) return 0;
	if(x>0) return 1;
	return -1;
}
Vector operator-(Point a,Point 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*(Point a,db p){
	return Vector(a.x*p,a.y*p);
} 
double Dot(Vector a,Vector b){
	return a.x*b.x+a.y*b.y;
}
double Cross(Vector a,Vector b){
	return a.x*b.y-a.y*b.x;
}
struct Line{
	Point v,p;
	Line(Point p,Point v):p(p),v(v){}
	Point point(db t){
		return v+(p-v)*t;
	}
};
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
    Vector u = P-Q;
    double t = Cross(w, u)/Cross(v, w);
    return P+v*t;
}
int main(int argc, char** argv) {
	int n;
	cin>>n;
	cout<<"INTERSECTING LINES OUTPUT\n";
	while(n--){
		Point p1,p2,p3,p4;
		cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y;
		Vector m,n;
		m=p2-p1;
		n=p4-p3;
		if(!sgn(Cross(m,n))){
			if(!sgn(m.x)&&!sgn(n.x)){
				if(!dcmp(p1.x,p3.x)){
					cout<<"LINE\n";
				}
				else 
				    cout<<"NONE\n";
			}
			else{
				db k=m.y/m.x;
				db k1=(p3.y-p1.y)/(p3.x-p1.x);
				if(!dcmp(k,k1)){
					cout<<"LINE\n";
				}
				else cout<<"NONE\n";
			}
		}
		else{
			cout<<"POINT ";
			Point p;
			p=GetLineIntersection(p1,m,p3,n);
			printf("%.2f %.2f\n",p.x,p.y);
		}
	}	
	cout<<"END OF OUTPUT\n"; 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值