Leyni, LOLI and Line hrbust 给出四个点求这四个点确定的两条直线的交点

要求:给出x1,y1,x2,y2,x3,y3,x4,y4;有三种情况,(1)相交(2)平行(3)共线。有交点了求出

Description

Professor Leyni likes to help LOLIs with their math.
This time, Leyni meets several LOLIs in the classroom and gets several problems about "Intersecting Lines".
The LOLIs want to know how and where two lines intersect.Leyni asks you to help him to answer.

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. Then T test cases follow.
For each test case:
Line 1. This line contains 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).
All numbers required by this problem will be in the range [-1000, 1000].

Output

For each test case:
Line 1.If there is no intersection, output "NONE". If they are the same line, output "LINE". Otherwise output the x and y coordinates of the point, correct to two decimal places as the sample 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

POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20

 1 #include<stdio.h>
 2 int main()
 3 {
 4     double a[9];//一般还是设成double型。
 5     int i,t;
 6     double k1,k2,x,y;
 7     scanf("%d",&t);//t组数据。
 8     while(t--)
 9     {
10         for(i=1;i<=8;i++)
11         scanf("%lf",&a[i]);
12         if(a[1]==a[3])//先考虑x1与x2相等时
13         {
14             if(a[5]==a[7])//如果x3等于x4
15             {
16                 if(a[5]==a[1])//四个点都在与x轴平行的直线上。在一条直线上
17                 printf("LINE\n");
18                 if(a[5]!=a[1])//说明两直线平行
19                 printf("NONE\n");
20             }
21             else//只要x3!=x4就考虑成直线,这样可以即简洁又简单。 {
22              k2=(a[8]-a[6])/(a[7]-a[5]);
23              y=k2*(a[1]-a[7])+a[8];
24             printf("POINT %.2lf %.2lf\n",a[1],y);
25             }
26             continue;
27         }
28         else if(a[1]!=a[3]&&a[5]==a[7])//另一种特殊情况。
29         {
30             k1=(a[4]-a[2])/(a[3]-a[1]);
31             y=k1*(a[5]-a[1])+a[2];
32             printf("POINT %.2lf %.2lf\n",a[5],y);
33             continue;
34         }
35         else//这种情况就应该比较好想了 {
36             k1=(a[4]-a[2])/(a[3]-a[1]);
37              k2=(a[8]-a[6])/(a[7]-a[5]);
38              if(k2==k1)
39              {
40                  if(a[6]-(k1*(a[5]-a[1])+a[2])==0&&a[8]-(k1*(a[7]-a[1])+a[2])==0)
41                  printf("LINE\n");
42                  else printf("NONE\n");
43              }
44              else
45              {
46              x=(k2*a[5]-k1*a[1]+a[2]-a[6])/(k2-k1);
47              y=k1*(x-a[1])+a[2];
48              printf("POINT %.2lf %.2lf\n",x,y);
49              continue;
50         }
51     }
52     }
53     return 0;
54 }

 

交点,

考的是基本功,没用到什么比较难的算法。但也不太好做。需要考虑全面一点。

很多时候都是有特殊情况,建议做题时首先考虑特殊情况。

posted @ 2012-09-23 20:01  尔滨之夏  阅读(661)  评论(0编辑  收藏  举报