Search code examples
c#mathgeometryline-segment

The algorithm to find the point of intersection of two 3D line segment


Finding the point of intersection for two 2D line segments is easy; the formula is straight forward. But finding the point of intersection for two 3D line segments is not, I afraid.

What is the algorithm, in C# preferably that finds the point of intersection of two 3D line segments?

I found a C++ implementation here. But I don't trust the solution because it makes preference of a certain plane (look at the way perp is implemented under the implementation section, it assumes a preference for z plane. Any generic algorithm must not assume any plane orientation or preference).

Is there a better solution?


Solution

  • I found a solution: it's here.

    The idea is to make use of vector algebra, to use the dot and cross to simply the question until this stage:

    a (V1 X V2) = (P2 - P1) X V2
    

    and calculate the a.

    Note that this implementation doesn't need to have any planes or axis as reference.