# Distance between two lines (textbook answer vs practical application)

I'm reading through "Mathematics for 3D Game Programming, and Computer Graphics" as a refresher, and leading in to some more complex real world problems down the line. However, one of the early chapters has a bit of math that seems overly complicated. This question deals with a problem from the textbook, that I believe the solution is far simpler than their recommended one.

The problem is to find the distance between two lines (in 2 or 3 dimensions).

Some set up:

1) Lines are set up parametrically as opposed to two points.

Line 1: Line 2: Where P = S + tV -- S is the starting point (say, A) and equals (1-t) * A

Now the book states to use this solution : Expanded looks like: Then the books next step is to reduce this, and take the derivatives with respect to time for both and Once the derivatives have been calculated. We move to a matrix form, and solve using some linear algebra.

My question is this, why would I use that series of steps when I already know the [ x, y, z ] for both points using the parametric. With those values wouldn't the simpler solution be just to do:  Then: Finally: Is there some direct application of the former equations that I am missing (the book does not elaborate currently on its applications here)? I've had some discussion with other game developers, and we haven't found much a use case for the linear algebra set up.

Edit: (The linear algebra solution)

First both derivatives with respect to time:  Then in matrix form, solve for both t variables: • I recommend math.stackexchange.com for this. – Almo Dec 17 '15 at 19:06
• This is really geared for software development, math.stackexchange may not know how this applies to games and software development – Bennett Yeates Dec 17 '15 at 19:07
• I also recommend taking this to math.stackexchange.com. While this is meant for game development, a lot of software questions boil down to understanding the math involved. If you don't understand the math (such as here) then the place to ask is the place dealing with math. Nothing in your question explicitly deals with games at all. – Draco18s no longer trusts SE Dec 17 '15 at 19:16
• I can definitely add this question to math.stackexchange, however, distance between points is a trivial and reoccurring game dev. problem. Also I disagree that this problem can be reduced to a simple math understanding, since comprehension of the math involved is not the problem, but the issue resides in the direct application of the more complicated solution which math.stackexchange may not know the answer to. – Bennett Yeates Dec 17 '15 at 19:21
• The linear algebra you've shown is being used to solve for the values of t1 & t2 (which are initially unknown), so that we know which two points Va & Vb to measure the distance between. As Sam Hocevar says in an answer below, that makes this a necessary step to determining the distance between these lines in the way that you propose. (If you don't want to know the location of the closest approach, then there are simpler ways) – DMGregory Dec 18 '15 at 22:30

Your V = Vb - Va is a function of two variables t1 and t2, and so is your sqrt(V·V). You cannot minimise that value without analysing a function of t1 and t2 of some sort, which is what the author does. It’s complicated because it’s a difficult problem, but it’s not overly complicated.