# General solution for 3D line intersection

I've been trying to get the intersection point of 2 3D lines (in general, so i can code an algorithm) using the following equations:

$$x_0 + k_0 a_0 = x_1 + k_1 a_1 \tag{1}$$

$$y_0 + k_0 b_0 = y_1 + k_1 b_1 \tag{2}$$

$$z_0 + k_0 c_0 = z_1 + k_1 c_1 \tag{3}$$

where $x,y,z$ are point coordinates and $a,b,c$ direction vector coordinates.

I think the idea is to get $k_0$ or $k_1$ so I can get the point from the line ($l_0$ if i use $k_0$, $l_1$ otherwise) using it's parametric equation.

I've made some tests and if I use equations $(1)$ and $(2)$ I get different results that if I use $(2)$ and $(3)$, so i think i'm doing something wrong...

From $(1)$: $$k_0 = \frac{x_1-x_0 + k_1a_1}{a_0} \tag{4}$$

from $(2)$: $$k_1 = \frac{y_0-y_1 + k_0b_0}{b_1} \tag{5}$$

from $(4)$ & $(5)$: $$k_1 = \frac{a_0 (y_0 - y_1) + b_0 (x_1 - x_0)}{a_0 b_1 - b_0 a_1} \tag{6}$$

And from $(1)$ & $(3)$: $$k_1 = \frac{a_0 (z_0 - z_1) + c_0 (x_1 - x_0)}{a_0 c_1 - c_0 a_1} \tag{7}$$

I don't know why, but $(6)$ & $(7)$ get different results for $k_1$...

I'm not controlling divisions by zero because C# returns NaN ($0/0$), -Infinity ($-x/0$), +Infinity ($x/0$) when needed, so I assume that if there is no intersection there would be some division by zero or something like that, right?

Any idea of whats wrong?

Thanks a lot!!

-

The problem is quite simplyy that if you take 2 lines in a 3D space they usually do not intersect (think about it!). So it's perfectly normal to get different results for $k_1$ : it simply means that your lines are not coplanar (you will only get a divide by zero error if they happen to be parallel).