求两直线交点的一般做法
/** *假设有两条直线分别为m、n。直线m的直线方程为F(x,y)=a1*x+b1*y+c1=0。 设直线m的两端点为(x1,y1)、(x2,y2),代入方程得 a1*x1+b1*y1+c1=0,a1*x2+b1*y2+c1=0,联立解可得 a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1。 设直线n的两端点分别为(x3,y3)、(x4,y4),同理可得a2=y3-y4,b2=x4-x3,c2=x3*y4-x4*y3。 又因为两直线相交即 a1*x+b1*y+c1=a2*x+b2*y+c2 可解得x=c2*b1-b2*c1/D,y=c1*a2-c2*a1/D (D=a1*b2-a2*b1) */ #include <iostream> #include<cstdio> using namespace std; const int maxn=1<<10; struct EDGE { double x1,y1,x2,y2; }edge[maxn]; void getpoint(double &x,double &y,int m,int n) { double a1=edge[m].y1-edge[m].y2; double b1=edge[m].x2-edge[m].x1; double c1=edge[m].x1*edge[m].y2-edge[m].x2*edge[m].y1; double a2=edge[n].y1-edge[n].y2; double b2=edge[n].x2-edge[n].x1; double c2=edge[n].x1*edge[n].y2-edge[n].x2*edge[n].y1; double D=a1*b2-a2*b1; x=(c2*b1-b2*c1)/D; y=(c1*a2-c2*a1)/D; } int main() { printf("First line\n"); scanf("%lf%lf%lf%lf",&edge[0].x1,&edge[0].y1,&edge[0].x2,&edge[0].y2); printf("Second line\n"); scanf("%lf%lf%lf%lf",&edge[1].x1,&edge[1].y1,&edge[1].x2,&edge[1].y2); double x,y; getpoint(x,y,0,1); printf("x=%lf y=%lf\n",x,y); return 0; }