The Math Forum

Ask Dr. Math - Questions and Answers from our Archives
Associated Topics || Dr. Math Home || Search Dr. Math

3d Distance

Date: Tue, 1 Nov 1994 19:46:14 -0600
From: Jason
Subject: 3d distance

What's the easiest way to find the distance between a point and a line in 
three dimensions: When the line is defined by two points in space, and when 
the line is defined by angles from the cartesian axes?  TIA!

Jason Leiker

From: Dr. Ken
Subject: Re: 3d distance
Date: Tue, 1 Nov 1994 22:18:37 -0500 (EST)


This is one example of a problem that can be stated fairly simply, but is
pretty tricky to figure out.  Good question!

What we'll have to do is first find the point on the line which is closest to 
the point out in space, then use the distance formula to find the distance 
between those two points.  So let's give ourselves some labels to work with.

Call the point in space (i,j,k) and let (a,b,c) and (x,y,z) be two points on
the line (call it line A).  The crucial fact we'll use is that the minimum
distance between the point and the line is the perpendicular distance.  So
we're looking for a point (L,M,N) which is on A, and such that the line
connecting (L,M,N) and (i,j,k) is perpendicular to A.  

In vector geometry, two vectors are perpendicular if and only if their dot
product is zero.  We find the vector direction of a line by subtracting two
points on the line.  So we'll have two equations:

((i,j,k) - (L,M,N)) . (a,b,c) - (x,y,z))  =  0


(a,b,c) + k((x,y,z) - (a,b,c))  =  (L,M,N)        for some k.

This second equation basically says that (L,M,N) is somewhere on line A.
The first thing we should do is simplify that first equation a little bit.

((i-L, j-M, k-N) . (a-x, b-y, c-z))  =  0       **

If you've never taken a dot product before, what you do is multiply the
first coordinates together, multiply the second coordinates together,
multiply the third coordinates together, and then add them up.

Note that the second equation above really gives us three separate 
equations to work with.  Simplifying, we get

(a,b,c) + k(x,y,z) - k(a,b,c) = (L,M,N)
(a(1-k) + kx, b(1-k) + ky, c(1-k) + kz)  =  (L,M,N)

So our three equations are

L = a(1-k) + kx          **
M = b(1-k) + ky          **
N = c(1-k) + kz          **

Note that the only unknowns here are L, M, N, and k.  Presumably we 
already know the point in space and the two points on the line, so we've 
got four equations and four unknowns.  It can be kind of hairy to solve 
them, but it's definitely doable.  See if you can take it from here!  If you're
confused, or you have any questions about what I've said, please don't
hesitate to write back.  But first see how far you can take it from here.


-Doctor Ken,  The Math Forum
 Check out our web site!   

Date: Fri, 14 Mar 1997
Subject: Two more solutions


Here are two more solutions to this problem:

Solution 1

Let,    A(x1,y1,z1), B(x2,y2,z2) = Two points defining the line 
        P(x,y,z) = Point whose distance from line AB is to be found

We can find vectors,
        AB = <x2-x1, y2-y1, z2-z1> = <x3, y3, z3>
        AP = < x-x1,  y-y1, z-z1 > = <x4, y4, z4>

I am using angle brackets < > to distinguish vectors from points.

Now, let's take the cross product AB X AP.  This is how you can calculate it:

    AB X AP = < y3*z4 - z3*y4, -(x3*z4 - x4*z3), x3*y4 - x4*y3 >

Let's let (a) be the length of AB X AP.  Then

    a = Sqrt{ (y3*z4 - z3*y4)^2 + (x3*z4 - x4*z3)^2 + (x3*y4 - x4*y3)^2 }

If you divide (a) by the distance AB you will get the distance of P 
from line AB. Distance AB can be found using the distance formula as,
    AB = square root of (x3^2 + y3^2 + z3^2)

Thus the distance we are looking for is a/AB.

Now, did you see why it works? Because the length of the cross product (a)
gives the area of the Parallelogram formed by the vectors AB and AP as
shown. If you rest the parallelogram on AB as the base, its area (a) is
AB*h. Where (h) is the height, which is nothing but the distance we are
looking for!

       \     |       \
        \    |h       \
         \   |         \
          A             B

As for the last part of your question : When a line is given only in 
terms of its inclinations with the coordinate axes, then the line could 
'float' anywhere parallel to itself, because its not 'anchored' to (or 
through) any point. In such a case, its distance from point P is not 

Solution 2

If you want, you can avoid using vectors and cross products, but it 
takes a little more work, because you need to find distances AB, BP, PA 
first using the distance formula. After that it's just as quick!

This time use triangle ABP instead of the parallelogram.

Let   h = height of triangle ABP (with AB as base)
        = the distance to be found
      T = Area of triangle ABP
      s = (AB + BP + PA)/2  This is called the semiperimeter

Then, T = square root of {s*(s-AB)*(s-BP)*(s-PA)}

The desired distance h = 2*T/AB

This way you can avoid solving a tedious system of equations.

Good luck!
                                                        Ujjwal Rane

Associated Topics:
High School Coordinate Plane Geometry
High School Geometry

Search the Dr. Math Library:

Find items containing (put spaces between keywords):
Click only once for faster results:

[ Choose "whole words" when searching for a word like age.]

all keywords, in any order at least one, that exact phrase
parts of words whole words

Submit your own question to Dr. Math

[Privacy Policy] [Terms of Use]

Math Forum Home || Math Library || Quick Reference || Math Forum Search

Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.