# Intersection of two 3D Lines

I wrote PHP code, following step by step by top voted comment from this topic: Find intersection of two 3D lines

But something it's wrong and I can't find the solution. Sometimes, the result is correct, but not always.

It's my code:

https://pastebin.com/3UBYkmnz

I think, that the problem is because I do not understand this part:

Let l=(h/k)*e. Then one of M=C±l is the point of intersection

But h and k are vectors. How do divide vector by vector? I was tried to divide for example h->x by k->x (but what if k->x is equal to zero?). So in this case i tried to divide h->y by k->y or h->z by k->z (LINES 28 - 33).

So, for lines that are not pararell to any axies, the result is correct. But it's wrong for line for example like this:

Line 1:

Given by two Points:
Point1:
x: 1440
y: 2500
z: 4500

Point2
x: 1440
y: 700
z: 3500


Line2:

Point1:
x: 1520
y: 1406.86
z: 3892.70

Point2:
x: 1440
y: 1326.86
z: 3848.26


Can anyone give me php solution for this problem or explain me, how to divide (h/k)? Thanks!

## 1 Answer

$h$ and $k$ are not vectors, they are norms of vectors. In the notation of the post you link

$$h = || {\bf f} \times {\bf g}||$$

So you need to calculate the cross product and then take the norm. In your code

$h = Vector3D::crossProduct($f, $g );  calculates the cross product, but you still need to calculate the norm of $h