题目大意:求两条直线的交点坐标。
解题关键:叉积的运用。
证明:
直线的一般方程为$F(x) = ax + by + c = 0$。既然我们已经知道直线的两个点,假设为$(x_0,y_0), (x_1, y_1)$,那么可以得到$a = {y_0} - {y_1}$,$b = x_1 – x_0$,$c = x_0y_1 – x_1y_0$。
因此我们可以将两条直线分别表示为
${F_0}(x) = {\rm{ }}{a_0}x{\rm{ }} + {\rm{ }}{b_0}y{\rm{ }} + {c_0} = 0,{F_1}(x) = {a_1}x + {b_1}y + {c_1} = 0$
那么两条直线的交点应该满足
${a_0}x + {b_0}y + {c_0} = {\rm{ }}{a_1}x + {b_1}y + {c_1}$
由此可推出
$\begin{array}{*{20}{l}}
{x = ({b_0}{c_1} - {b_1}{c_0})/D}\\
{y = ({a_1}{c_0} - {a_0}{c_1})/D}
\end{array}$
$D = {a_0}{b_1} - {a_1}{b_0}$ (D为0时,表示两直线平行)
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<cmath> #include<iostream> #define pi acos(-1) using namespace std; typedef long long ll; const double eps=1e-8; const int N=5,maxn=100005,inf=0x3f3f3f3f; struct point{ double x,y; }; struct line{ point a,b; }l[N]; int main(){ int t; double x1,x2,x3,x4,y1,y2,y3,y4; cin>>t; cout<<"INTERSECTING LINES OUTPUT"<<endl; while(t--){ cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4; if((x4-x3)*(y2-y1)==(y4-y3)*(x2-x1)){ if((x2-x1)*(y3-y1)==(y2-y1)*(x3-x1)) cout<<"LINE"<<endl;//用叉积判断共线 else cout<<"NONE"<<endl; } else{ double a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1;//c是叉积 double a2=y3-y4,b2=x4-x3,c2=x3*y4-x4*y3; double x=(c2*b1-c1*b2)/(b2*a1-b1*a2); double y=(a2*c1-a1*c2)/(b2*a1-b1*a2); printf("POINT %.2f %.2f\n",x,y); } } cout<<"END OF OUTPUT"<<endl; return 0; }