题目大意:
给定n组数据,每组2条直线,求直线关系,关系有如下
1.重合
2.平行
3.相交于一点
输入:
有n行,每行8个数,表示第一条直线的两点和第二条直线的两点
输出:
格式参照输出样例,对于相交,输出交点
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
题解:
这题不难,主要是函数计算,只要有基础的一次函数知识就能解出
但最重要的是斜率不存在的情况,要特判
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 double x1,y11,x2,y2,x3,y3,x4,y4; 8 int n; 9 int main() 10 {int i; 11 cin>>n; 12 cout<<"INTERSECTING LINES OUTPUT "; 13 for (i=1;i<=n;i++) 14 { 15 cin>>x1>>y11>>x2>>y2>>x3>>y3>>x4>>y4; 16 if (x2-x1==0&&x4-x3==0) 17 { 18 if (x1!=x3) 19 cout<<"NONE "; 20 else cout<<"LINE "; 21 } 22 else 23 if (x2-x1==0) 24 { 25 cout<<"POINT "; 26 double x=x1; 27 printf("%.2lf ",x); 28 double y=((y4-y3)*x+(x4*y3-x3*y4))/(x4-x3); 29 printf("%.2lf ",y); 30 } 31 else 32 if (x4-x3==0) 33 { 34 cout<<"POINT "; 35 double x=x3; 36 printf("%.2lf ",x); 37 double y=((y2-y11)*x+(x2*y11-x1*y2))/(x2-x1); 38 printf("%.2lf ",y); 39 } 40 else 41 if ((y2-y11)*(x4-x3)==(y4-y3)*(x2-x1)) 42 { 43 if ((x2-x1)*(x4*y3-x3*y4)==(x4-x3)*(x2*y11-x1*y2)) 44 { 45 cout<<"LINE "; 46 } 47 else cout<<"NONE "; 48 } 49 else 50 { 51 double fz=-(x2*y11-x1*y2)*(x4-x3)+(x4*y3-x3*y4)*(x2-x1); 52 double fm=(y2-y11)*(x4-x3)-(y4-y3)*(x2-x1); 53 cout<<"POINT "; 54 double x=fz/fm; 55 printf("%.2lf ",x); 56 double y=(y2-y11)*x/(x2-x1)+(x2*y11-x1*y2)/(x2-x1); 57 printf("%.2lf ",y); 58 } 59 } 60 cout<<"END OF OUTPUT"; 61 }