求直线交点

来源:互联网 发布:全国城市js代码 编辑:程序博客网 时间:2024/06/01 08:41

参考:http://blog.csdn.net/abcjennifer/article/details/7584628#reply
直线的一般方程为F(x) = ax + by + c = 0。既然我们已经知道直线的两个点,假设为(x0,y0), (x1, y1),那么可以得到a = y0 – y1, b = x1 – x0, c = x0y1 – x1y0。因此我们可以将两条直线分别表示为
F0(x) = a0*x + b0*y + c0 = 0, F1(x) = a1*x + b1*y + c1 = 0
利用线性代数求解连立方程组F0(x) = a0*x + b0*y + c0 = 0, F1(x) = a1*x + b1*y + c1 = 0
i     j     k
a0 b0 c0
a1 b1 c1
由此可推出
x = (b0*c1 – b1*c0)/D
y = (a1*c0 – a0*c1)/D
D = a0*b1 – a1*b0, (D为0时,表示两直线平行)

class FPoint2D {public double x;public double y;public FPoint2D() {}public FPoint2D(double x1, double y1) {x = x1;y = y1;}}int Line_Intersect(FPoint2D p1,FPoint2D p2,FPoint2D p3,FPoint2D p4,FPoint2D p5){//FPoint2D ptRe=new FPoint2D();double a0=p1.y-p2.y;double b0=p2.x-p1.x;double c0=p1.x*p2.y-p2.x*p1.y;double a1=p3.y-p4.y;double b1=p4.x-p3.x;double c1=p3.x*p4.y-p4.x*p3.y;double D=a0*b1-a1*b0;//D==0 直线平行if(D==0)return -1;p5.x=(b0*c1-b1*c0)/D;p5.y=(a1*c0-a0*c1)/D;return 1;}




1 0
原创粉丝点击