   Shortest Distance from a Point to a Line
  Okay, so I'm trying to find the shortest distance between a point and a line in 3D.  It seems to be a simple enough problem:  A perpendicular from the point to the line should be the shortest distance, yes?  But I seem to be having problems getting the right results....Take the following diagram:P is my point.O is my origin for the ray R, which together form my line.Given the projection equation of:A dot B------- * A |A|^2Where A is the vector representing the line and B is the vector formed from the point and the origin of A.I should be able to find the perpendicular vector, yes?  Well, my pen-and-paper work for it doesn't seem to work properly....Let P = (-4, 0, 0)Let O = (0, 0, -3)Let I = (0, 0, 0)  (This is a point we don't know, but we're trying to find)This forms a simple 3/4/5 right triangle, so theta must equal 60 degrees, right?In the projection equation, we've got A and B, so in this example, let's:Let R = <0, 0, 3>  (for simplicity's sake, it'll be the vector from O to I)Let A = R  (to substitute into the projection equation)Let B = P - O = <(-4 - 0), (0 - 0), (0 - -3)> = <-4, 0, 3>Therefore,A dot B = |A||B|cos(theta)A dot B = (3)(5)cos(60 degrees)A dot B = 7.5Correct?Then,Proj(B onto A) =7.5--- * <0, 0, 3>3^2Which yields the end result:Proj(B onto A) = <0, 0, 2.5>Which should be the line segment between P and I, correct?  But the length of this projection is 2.5, while according to the 3/4/5 right triangle concept, I should get a result of 4, and in the X direction on top of that:<4, 0, 0>Where am I going wrong with this?Another example is if my line passes through the point (let's say that P = I's location from the previous example)....  Everything in the projection equation goes to 1 except the final multiplier of A, which yields a length of more than 1....  But the distance should be 0!Am I simply using the wrong equation to find the shortest distance between a point and a line?  Or am I doing something entirely wrong in the calculations?   
  The point: PXif line is p1 to p2,pv = p2-p1An equation for a point on the line:p(t) = p1+pv*tsolve(dot(p1-p(t), PX) == 0)You should be able to work out the math   ;  Aha!  I think I got it.B should equal P in my equation, not (P - O).  I'm not sure why, but it may have something to do with the coordinates I'm using?  At least it yields the right answers.Thanks for the nudge, incin, although your equation seems a bit off....Given:PX = (-4, 0, 0)p1 = (0, 0, -3)p2 = (0, 0, 0)pv = p2 - p1 = <0, 0, 3>  (although this is an unknown in my code, but the unit vector representing it, <0, 0, 1>, is not unknown)p(t) = p1 + pv*tThen,dot(p1 - p(t), PX) == 0Should reduce to:dot(p1 - (p1 + pv*t), PX) == 0dot(pv*t, PX) == 0Well, pv*t dot PX = |pv||PX|cos(theta) = (1)(4)cos(60 degrees) = 2Does this substitute for t in your equation?  If so, then:p(t) = p1 + pv*2 = <0, 0, -3> + <0, 0, 2> = <0, 0, -1>While we should be getting <0, 0, 0>.   ;  You're right, i wasn't paying enough attention.dot(pv, PX-p(t)) == 0   