Intersecting Lines(poj1269,判斷直線與直線相交)

http://poj.org/problem?id=1269

Intersecting Lines

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 9061 Accepted: 4066

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

Source

Mid-Atlantic 1996

解析:

題意:

給出n對直線,判斷每對直線是否相交,平行,重疊,如果相加就要求出交點

思路:

根據所給條件求出直線的解析式:

line1: y=k1*x+b1;

line2: y=k2*x+b2;

交點爲(XY

X=(b2-b1)/(k1-k2);

Y=k2*X+b2;

注意:判斷的時候要根據斜率直線是否存在分類討論

184 0MS C++ 1472B

*/


#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=20;
struct line
{
double x1,x2;
double y1,y2;
}lin[maxn][2];
void solve(line L1,line L2)
{
  double k1,k2,a1,a2,b1,b2,c1,c2,x,y;
  a1=L1.x1-L1.x2;
  a2=L2.x1-L2.x2;
  b1=L1.y1-L1.y2;
  b2=L2.y1-L2.y2;
  if(a1==0||a2==0)//當斜率不存在時
  {
  	if(a1==0&&a2==0)
  	{
  	 if(L1.x1==L2.x1)//重疊
  	 printf("LINE\n");
  	 else
  	 printf("NONE\n");//平行
  	}
  	if(a1==0&&a2!=0)
  	{
  	 x=L1.x1;
  	 k2=b2/a2;
  	 c2=L2.y2-k2*L2.x2;
  	 y=k2*x+c2;
  	 printf("POINT %.2lf %.2lf\n",x,y);
  	}
  	if(a2==0&&a1!=0)
  	{
  	 x=L2.x2;
  	 k1=b1/a1;
  	 c1=L1.y1-k1*L1.x1;
  	 y=k1*x+c1;
  	 printf("POINT %.2lf %.2lf\n",x,y);
  	}
  }
  else
  {
  	k1=b1/a1;
  	k2=b2/a2;
  	c1=L1.y1-k1*L1.x1;
  	c2=L2.y2-k2*L2.x2;
  	if(k1==k2)
  	{
  	 if(c1==c2)
  	 printf("LINE\n");
  	 else
  	     printf("NONE\n");
  	}
  	else
  	{
  	 x=(c2-c1)/(k1-k2);
  	 y=k1*x+c1;
  	 printf("POINT %.2lf %.2lf\n",x,y);
  	}
  }
}
int main()
{
 int n,i,j;
 while(scanf("%d",&n)!=EOF)
 {
 	for(i=0;i<n;i++)
 	{
 	 scanf("%lf%lf%lf%lf",&lin[i][0].x1,&lin[i][0].y1,&lin[i][0].x2,&lin[i][0].y2);
 	 scanf("%lf%lf%lf%lf",&lin[i][1].x1,&lin[i][1].y1,&lin[i][1].x2,&lin[i][1].y2);
 	}
 	printf("INTERSECTING LINES OUTPUT\n");
 	for(i=0;i<n;i++)
solve(lin[i][0],lin[i][1]);
printf("END OF OUTPUT\n");
 }
return 0;
}


發佈了228 篇原創文章 · 獲贊 11 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章