ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-07-14 21:02:50  阅读:237  来源: 互联网

标签:return Point lines 线段 Lines db Vector line Intersecting


题目链接: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;
}

 

标签:return,Point,lines,线段,Lines,db,Vector,line,Intersecting
来源: https://blog.csdn.net/qq_43472263/article/details/95922721

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有