UVA 378 Intersecting Lines
- 根据题意编代码即可
- 注意:分类讨论很可能因为过于混乱出错,因此,代码格式很重要(看得更加清楚),建议不用else
CODE:
#include<iostream>
#include<cstdio>
using namespace std;
int n,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4;
double k_1,k_2,b_1,b_2,pointx,pointy;
int main()
{
int ok=1;
cin >> n;
cout << "INTERSECTING LINES OUTPUT" << endl;
while(n&&cin>>x_1>>y_1>>x_2>>y_2>>x_3>>y_3>>x_4>>y_4)
{
n--;
if(x_1==x_2)
{
if(x_3==x_4)
{
if(x_3==x_1){cout << "LINE" << endl;continue;}
if(x_3!=x_1){cout << "NONE" << endl;continue;}
}
if(x_3!=x_4)
{
k_2=(double)(y_3-y_4)/(x_3-x_4);
b_2=(double)(x_3*(y_3+y_4)-y_3*(x_3+x_4))/(x_3-x_4);
pointx=x_1;
pointy=x_1*k_2+b_2;
printf("POINT %.2f %.2f\n",pointx,pointy);
continue;
}
}
if(x_1!=x_2)
{
if(x_3==x_4)
{
k_1=(double)(y_1-y_2)/(x_1-x_2);
b_1=(double)(x_1*(y_1+y_2)-y_1*(x_1+x_2))/(x_1-x_2);
pointx=x_3;
pointy=x_3*k_1+b_1;
printf("POINT %.2f %.2f\n",pointx,pointy);
continue;
}
if(x_3!=x_4)
{
if((x_1-x_2)*(y_3-y_4)==(x_3-x_4)*(y_1-y_2))
{
b_1=(double)(x_1*(y_1+y_2)-y_1*(x_1+x_2))/(x_1-x_2);
b_2=(double)(x_3*(y_3+y_4)-y_3*(x_3+x_4))/(x_3-x_4);
if(b_1==b_2){cout << "LINE" << endl;continue;}
if(b_1!=b_2){cout << "NONE" << endl;continue;}
}
if((x_1-x_2)*(y_3-y_4)!=(x_3-x_4)*(y_1-y_2))
{
k_1=(double)(y_1-y_2)/(x_1-x_2);
b_1=(double)(x_1*(y_1+y_2)-y_1*(x_1+x_2))/(x_1-x_2);
k_2=(double)(y_3-y_4)/(x_3-x_4);
b_2=(double)(x_3*(y_3+y_4)-y_3*(x_3+x_4))/(x_3-x_4);
pointx=(double)(b_1-b_2)/(k_2-k_1);
pointy=(double)(pointx*k_1+b_1);
printf("POINT %.2f %.2f\n",pointx,pointy);
continue;
}
}
}
}
cout << "END OF OUTPUT" << endl;
return 0;
}
输出OUTPUT:
done...