title: Find the Distance from a 3d Point to a Line Segment
body: <p>I have a 3d point P and a line segment defined by A and B (A is the start point of the line segment, B the end).</p>
  
  <p>I want to calculate the shortest distance between P and the line AB.</p>
  
  <p>Calculating the distance of a point to an infinite line was easy as their was a solution on <a href="http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html" rel="noreferrer">Wolfram Mathworld</a>, and I have implemented that, but I need to do this for a line of finite length.</p>
  
  <p>I have not managed to find a reliable solution for this in 3d after a lot of looking around.</p>
  
  <p>I have implemented algorithms to calculate the dot product, cross product, magnitude and so on in C++ with a struct that contains floats x, y and z.</p>
  
  <p>Pseudo code, links, or code in pretty much any language for this would be great.</p>