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. |
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. |
Output |
For each test case: |
Sample Input |
5 |
Sample Output |
POINT 2.00 2.00 |
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 }
交点,
考的是基本功,没用到什么比较难的算法。但也不太好做。需要考虑全面一点。
很多时候都是有特殊情况,建议做题时首先考虑特殊情况。