This problem is WAY to complex to answer here.
An entire computer program can be written to fully solve this problem.
By 2D line segment, you are saying that there are 2 endpoints and that the line segment
(by definition) does NOT extend indefinitely/infinitely in neither direction.
So those endpoints must be input. From there, the slope, distance, midpoint,
and equation of the line segments can be calculated and derived.
Now there are several cases and scenarios that must be considered.
The simplest case is that the line segments actually intersect. In that case, the
shortest distance between them is zero, namely at the intersection point (which again
can be calculated).
Another possibility is that the line segments are parallel (under the same domain).
In that case, the distance between them is constant, and this the shortest distance
by default.
Regarding the issue of domains, which also must be specified, the two line segments can be completely disjoint, That is, the intersection of their domains is empty. For example, one line has [1,2] as
it's domain while the other segment has [10,12] as it's domain. From there, We use the distance
formula to find which points on [1,2] minimize the distance from the segment on [10,12]
THen there are vertical line segments, horizontal line segments, and perpendicular
(but disjoint) line segments.
The computer program to solve this problem requires careful design so as to address
all of these possible scenarios and conditions.
Please respond as to how to proceed.
=================================================
OK as an update, here is a little bit of pseudo code for thought.
Given the endpoints of the line segments, the equations of the line segments are calculated.
Keep in mind this pseudo code snippet is only valid if NEITHER of the lines are vertical.
It is an inefficient algorithm that calculates distances between points on the line segments
and keeps track of the APPROXIMATE smallest distance.
Given or calculated: the equations of the line segments f(x) and g(x)
line segment one has endpoints (X11,Y11) (X12,Y12)
line segment two has endpoint (X21,X21) (X22,Y22)
xLoop1 = x11
H = 0.00001 <--- adjust to the level of accuracy desired; however, the smaller the decimal the longer it takes!
minDistance = 32767 <--- use some max value that will be larger than any distance to start
while xLoop1 <= X12
xLoop2 = X21
while xLoop2 <= X22
y1 = f(xLoop1)
y2 = f(xLoop2)
d = Distance(xLoop1,y1, xLoop2, y2)
if d < minDistance then minDistance = d
xLoop2 = xLoop2 + H
end while
xLoop1 = xLoop1 + H
end while