Intersecting Lines(计算几何_直线与直线)

92 篇文章 0 订阅
13 篇文章 0 订阅

Description

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
 
  
ps:终于给水出来了,自己一个人只能用最笨的方法了,学长给的浮点函数完全不知道什么意思,一上午。开始写的代码自己看了都头晕,后来看到别人把些乱七八糟的数都定义了,我也改了改,现在起码能看了。
 
  
题意:给出n组测试数据,每组有两条直线(由四个点构成的),若是两条直线相交,则输出交点,若是共线输出LINE,若是平行输出NONE;
 
  
思路 设直线为y=kx+b;  所以第一条直线可以表示为y=(y2-y1)*x/(x2-x1)+(x2*y1-x1*y2)/(x2-x1);第二条直线可以表示为y=(y4-y3)*x/(x4-x3)+(x4*y3-x3*y4)/(x4-x3);然后分情况讨论;
斜率不存在:
(1)当一条斜率存在,另一条斜率存在;
(2)另一条斜率存在,这一条不存在;
(3)两条斜率都不存在;
斜率存在
(4)两条直线共线;
 (5)两条直线平行但不共线;
(6)存在交点;
 
  
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int main()
{
	int n;
	double x1,y1,x2,y2,x3,y3,x4,y4;
	scanf("%d",&n);
	puts("INTERSECTING LINES OUTPUT");
	while(n--)
	{
		scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);

		double a=x2-x1;
		double b=y2-y1;
		double c=x4-x3;
		double d=y4-y3;
		double x,y;
		double k1=b/a;
		double b1=y1-k1*x1;
		double k2=d/c;
		double b2=y3-k2*x3;
		if(x1==x2||x3==x4)
		{
			if(x1==x2&&x3==x4)//两条斜率都不存在,所以他们的方向是垂直于x轴的,所以只需要看x轴的坐标是否相等
			{
				if(x1==x3)//相等则共线
					printf("LINE\n");
				else
					printf("NONE\n");//不相等则平行
			}
			else if(x1==x2)//第一条斜率不存在,第二条存在
			{
				x=x1;
				y=(d*x+x4*y3-x3*y4)/c;
				printf("POINT %.2lf %.2lf\n",x,y);
			}
			else//第一条斜率存在,第二条不存在;
			{
				x=x3;
				y=(b*x+x2*y1-x1*y2)/a;
				printf("POINT %.2lf %.2lf\n",x,y);
			}
			continue;
		}

  //下面是斜率存在的情况

	    if(a*d==b*c)//斜率相等
		{
			if(y1==k2*x1+b2)//如果这条直线的点也在另一条直线上,证明重合
				printf("LINE\n");
			else
				printf("NONE\n");//否则平行;
		}
		else//有交点的情况
		{
			x=(b1-b2)/(k2-k1);
			y=k2*x+b2;
			printf("POINT %.2lf %.2lf\n",x,y);
		}
	}
	puts("END OF OUTPUT");
	return 0;
}

 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值