【POJ 1269】Intersecting Lines

【题目】

传送门

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


【分析】

大致题意:给出两条直线,判断这两条直线的关系(重合,平行或相交),若相交还要输出交点

题解:平面几何入门题

以下用 c r o s s cross 表示叉积,用 p 1 , p 2 , p 3 , p 4 p_1,p_2,p_3,p_4 表示四个点, p 1 , p 2 p_1,p_2 表示 l 1 l_1 p 3 , p 4 p_3,p_4 表示 l 2 l_2

  1. 若两直线重合,则 p 3 p_3 应该在 l 1 l_1 上,且 p 4 p_4 也应该在 l 1 l_1 上,用叉积判断一下即可
  2. 若两直线平行,则两直线的叉积应为 0 0 (可以理解成形成的平行四边形面积为 0 0
  3. 若上述两种情况都不符合,那就肯定相交,求交点可以用代数法(即求 k k b b ),也可以用几何法,这里就不多说怎么实现了,不懂的就上网搜吧

【代码】

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define eps 1e-8
using namespace std;
struct point
{
	double x,y;
	point(){}
	point(double x,double y):x(x),y(y){}
	point operator+(const point &a)  {return point(x+a.x,y+a.y);}
	point operator-(const point &a)  {return point(x-a.x,y-a.y);}
	double operator*(const point &a)  {return x*a.x+y*a.y;}
	double operator^(const point &a)  {return x*a.y-a.x*y;}
};
struct line
{
	point start,end;
}a,b;
point mul(point a,double k)
{
	return point(a.x*k,a.y*k);
}
bool Collinear(line a,line b)
{
	double s1=(a.end-a.start)^(b.start-a.start);
	double s2=(a.end-a.start)^(b.end-a.start);
	return fabs(s1)<eps&&fabs(s2)<eps;
}
bool Parallel(line a,line b)
{
	double s1=(a.end-a.start)^(b.end-b.start);
	return fabs(s1)<eps;
}
point inter(line p,line q)
{
	double s1=(p.start-q.start)^(p.end-q.start);
	double s2=(p.start-q.end)^(p.end-q.end);
	double k=s1/(s1-s2);
	return mul(q.end-q.start,k)+q.start;
}
int main()
{
	int n,i;
	scanf("%d",&n);
	int x1,y1,x2,y2,x3,y3,x4,y4;
	printf("INTERSECTING LINES OUTPUT\n");
	for(i=1;i<=n;++i)
	{
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		scanf("%d%d%d%d",&x3,&y3,&x4,&y4);
		a.start=point(x1,y1),a.end=point(x2,y2);
		b.start=point(x3,y3),b.end=point(x4,y4);
		if(Collinear(a,b))  printf("LINE\n");
		else  if(Parallel(a,b))  printf("NONE\n");
		else
		{
			point p=inter(a,b);
			printf("POINT %.2f %.2f\n",p.x,p.y);
		}
	}
	printf("END OF OUTPUT");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/forever_dreams/article/details/83057010