General Equation Method:
The general equation of a straight line is f (x) = AX + by + c = 0. Now that we know the two points of a straight line, assuming (x0, y0), (x1, Y1), we can get a = y0-Y1, B = x1-x0,
C = x0y1-x1y0.
Therefore, we can represent the two straight lines
F0 (x) = A0 * x + B0 * Y + C0 = 0, F1 (x) = A1 * x + B1 * Y + C1 = 0
The intersection of the two straight lines should meet
A0 * x + B0 * Y + C0 = A1 * x + B1 * Y + C1
This can be introduced
X = (B0 * C1-B1 * C0)/d
Y = (A1 * C0-A0 * C1)/d
D = A0 * B1-A1 * B0 (when D is 0, the two straight lines overlap)
The two are actually establishing equations f0 (x) = A0 * x + B0 * Y + C0 = 0, F1 (X) = A1 * x + B1 * Y + C1 = 0 Cross Product Application
I j k
A0 B0 C0
A1 B1 C1
#include"iostream"#include"stdio.h"#include"math.h"using namespace std;struct Point{double x;double y;};struct Line{Point p1,p2;double a,b,c;};void GetLinePara(Line *l){l->a=l->p1.y-l->p2.y;l->b=l->p2.x-l->p1.x;l->c=l->p1.x*l->p2.y-l->p2.x*l->p1.y;}Point GetCrossPoint(Line *l1,Line *l2){GetLinePara(l1);GetLinePara(l2);double D=l1->a*l2->b-l2->a*l1->b;Point p;p.x=(l1->b*l2->c-l2->b*l1->c)/D;p.y=(l1->c*l2->a-l2->c*l1->a)/D;return p;}int main(){Line l1,l2;while(true){scanf("%lf%lf%lf%lf",&l1.p1.x,&l1.p1.y,&l1.p2.x,&l1.p2.y);scanf("%lf%lf%lf%lf",&l2.p1.x,&l2.p1.y,&l2.p2.x,&l2.p2.y);Point Pc=GetCrossPoint(&l1,&l2);printf("Cross point:%lf %lf\n",Pc.x,Pc.y);}return 0;}
From: http://blog.csdn.net/abcjennifer/article/details/7584628General Equation Method:
The general equation of a straight line is f (x) = AX + by + c = 0. Now that we know the two points of a straight line, assuming (x0, y0), (x1, Y1), we can get a = y0-Y1, B = x1-x0,
C = x0y1-x1y0.
Therefore, we can represent the two straight lines
F0 (x) = A0 * x + B0 * Y + C0 = 0, F1 (x) = A1 * x + B1 * Y + C1 = 0
The intersection of the two straight lines should meet
A0 * x + B0 * Y + C0 = A1 * x + B1 * Y + C1
This can be introduced
X = (B0 * C1-B1 * C0)/d
Y = (A1 * C0-A0 * C1)/d
D = A0 * B1-A1 * B0 (when D is 0, the two straight lines overlap)
The two are actually establishing equations f0 (x) = A0 * x + B0 * Y + C0 = 0, F1 (X) = A1 * x + B1 * Y + C1 = 0 Cross Product Application
I j k
A0 B0 C0
A1 B1 C1
#include"iostream"#include"stdio.h"#include"math.h"using namespace std;struct Point{double x;double y;};struct Line{Point p1,p2;double a,b,c;};void GetLinePara(Line *l){l->a=l->p1.y-l->p2.y;l->b=l->p2.x-l->p1.x;l->c=l->p1.x*l->p2.y-l->p2.x*l->p1.y;}Point GetCrossPoint(Line *l1,Line *l2){GetLinePara(l1);GetLinePara(l2);double D=l1->a*l2->b-l2->a*l1->b;Point p;p.x=(l1->b*l2->c-l2->b*l1->c)/D;p.y=(l1->c*l2->a-l2->c*l1->a)/D;return p;}int main(){Line l1,l2;while(true){scanf("%lf%lf%lf%lf",&l1.p1.x,&l1.p1.y,&l1.p2.x,&l1.p2.y);scanf("%lf%lf%lf%lf",&l2.p1.x,&l2.p1.y,&l2.p2.x,&l2.p2.y);Point Pc=GetCrossPoint(&l1,&l2);printf("Cross point:%lf %lf\n",Pc.x,Pc.y);}return 0;}