Given two 2D line segments, A and B, how do I calculate the length of the shortest 2D line segment, C, which connects A and B?
Consider your two line segments A and B to be represented by two points each:
line A represented by A1(x,y), A2(x,y)
Line B represented by B1(x,y) B2(x,y)
First check if the two lines intersect using this algorithm.
If they do intersect, then the distance between the two lines is zero, and the line segment joining them is the intersection point.
If they do not intersect, Use this method: http://paulbourke.net/geometry/pointlineplane/ to calculate the shortest distance between:
The shortest of those four line segments is your answer.