简体   繁体   English

识别两条线的交点

[英]Identification of the intersection point of two lines

Here are the calculations 这是计算

L1 = Line joining points A(x1,y1) and B(x2,y2) L2 = Line joining points c(x3,y3) and D(x4,y4) L1 =线连接点A(x1,y1)和B(x2,y2)L2 =线连接点c(x3,y3)和D(x4,y4)

For Line L1 对于线L1

Line Equation : y = m1*x + c1 线方程式:y = m1 * x + c1

slope m1 : (y2-y1)/(x2-x1) 斜率m1:(y2-y1)/(x2-x1)

Y intercept : c1 = (y1 - m1*x1) Y截距:c1 =(y1-m1 * x1)

For Line L2 对于L2线

Line Equation : y = m2*x + c2 线方程式:y = m2 * x + c2

slope m2 : (y4-y3)/(x4-x3) 斜率m2:(y4-y3)/(x4-x3)

Y intercept : c2 = (y3 - m2*x3) Y截距:c2 =(y3-m2 * x3)

For Point of Intersection 对于交叉点

Solving the above equations we get 解决上述方程式,我们得到

x = (c2 -c1)/(m1-m2) x =(c2 -c1)/(m1-m2)

y = (c1*m2 - c2*m1)/(m2-m1) y =(c1 * m2-c2 * m1)/(m2-m1)

These above calculations are used to calculate the intersection points in my java program. 以上这些计算用于计算Java程序中的交点。

The Problem 问题

The problem occurs when the two lines are parallel to x-axis and y-axis respectively. 当两条线分别平行于x轴和y轴时,就会出现问题。 For example if the connecting points are as follows 例如,如果连接点如下

L1 = A(34,112) B(34,180) ...(x-coordinate value remains constant) L1 = A(34,112)B(34,180)...(x坐标值保持不变)

L2 = C(72,100) D(88,100) ...(y-coordinate value remains constant) L2 = C(72,100)D(88,100)...(y坐标值保持不变)

now m1 will become Infinity and m2 will become 0 accordingly c1 will become Infinity and c2=y3 as a result the computation of the intersection points using below given formula gives strange (NaN) result although the lines L1 and L2 must meet at (34,100). 现在m1将变为无穷大,并且m2将变为0,因此c1将变为无穷大,并且c2 = y3作为结果,尽管线L1和L2必须相交于(34,100),但使用以下给定公式计算交点会得出奇怪的(NaN)结果。

x = (c2 -c1)/(m1-m2) x =(c2 -c1)/(m1-m2)

y = (c1*m2 - c2*m1)/(m2-m1) y =(c1 * m2-c2 * m1)/(m2-m1)

Why such a problem ? 为什么会有这样的问题? How is this problem handled using math so that it can be implemented in the program. 如何使用数学处理此问题,以便可以在程序中实现它。

A line which is parallel to the y-axis cannot be expressed as y = ax + b . 平行于y轴的线不能表示为y = ax + b You need to use the general equation of a line ax + by + c = 0 . 您需要使用ax + by + c = 0的线的通用方程式。 Determine the coefficients of the equations of your two lines and solve the linear system of their intersection. 确定两条直线方程的系数,并求解它们的交点的线性系统。 Make sure the determinant of the system is different from 0, otherwise there is no solution, or an infinity (which you can consider being another case of no solution). 确保系统的行列式不同于0,否则没有解或无穷大(您可以考虑是无解的另一种情况)。

You can obtain a and b coefficients quite easily considering the normal vector of your segment (if vect(AB) = (x,y) then normal(AB) = (-y,x) = (a,b) . You then determine c by introducing the coordinates of A in the equation : c = -a*x_A - b*y_A . 考虑段的法线向量,可以很容易地获得ab系数(如果vect(AB) = (x,y)normal(AB) = (-y,x) = (a,b)然后确定c通过引入的坐标方程式中: c = -a*x_A - b*y_A

You now have a linear system to solve : 您现在有了一个线性系统来解决:

(S) : { a1*x + b1*y + c1 = 0    
      { a2*x + b2*y + c2 = 0

If det = a1*b2 - a2*b1 = 0 (be careful with loss of precision, make your comparison epsilon-wise), then the system has no unic solution. 如果det = a1*b2 - a2*b1 = 0 (请注意精度损失,以epsilon方式进行比较),则系统没有unic解。 Otherwise, you can find the inverse of the matrix of the system : 否则,您可以找到系统矩阵的逆:

M = (a1  b1), M^(-1) = 1/det * ( b2  -b1) 
    (a2  b2)                   (-a2   a1) 

Now you just need to compute 现在您只需要计算

M^(-1) * (-c1) = 1/det * (-b2*c1 + b1*c2)
         (-c2)           ( a2*c1 - a1*c2)

And that's it, you have your solution ! 就是这样,您有解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM