' Calculate the distance between
' the segment (X11, Y11)-(X12, Y12) and
' the segment (X21, Y21)-(X22, Y22).
' Return the distance. Return the closest points
' on the segments through parameters
' (near_x1, near_y1) and (near_x2, near_y2).
Private Function DistBetweenSegments(ByVal X11 As Single, _
ByVal Y11 As Single, ByVal X12 As Single, ByVal Y12 As _
Single, ByVal X21 As Single, ByVal Y21 As Single, ByVal _
X22 As Single, ByVal Y22 As Single, ByRef near_x1 As _
Single, ByRef near_y1 As Single, ByRef near_x2 As _
Single, ByRef near_y2 As Single) As Single
Dim dx As Single
Dim dy As Single
Dim t As Single
Dim best_dist As Single
Dim test_dist As Single
Dim test_x As Single
Dim test_y As Single
' See if the segments intersect.
If SegmentsIntersect(X11, Y11, X12, Y12, X21, Y21, X22, _
Y22, near_x1, near_y1) Then
' The segments intersect.
DistBetweenSegments = 0
near_x2 = near_x1
near_y2 = near_y1
Exit Function
End If
' Check (X11, Y11) with segment 2.
test_dist = DistToSegment(X11, Y11, X21, Y21, X22, Y22, _
test_x, test_y)
best_dist = 1E+38
If test_dist < best_dist Then
best_dist = test_dist
near_x1 = test_x
near_y1 = test_y
near_x2 = X11
near_y2 = Y11
End If
' Check (X12, Y12) with segment 2.
test_dist = DistToSegment(X12, Y12, X21, Y21, X22, Y22, _
test_x, test_y)
If test_dist < best_dist Then
best_dist = test_dist
near_x1 = X12
near_y1 = Y12
near_x2 = test_x
near_y2 = test_y
End If
' Check (X21, Y21) with segment 1.
test_dist = DistToSegment(X21, Y21, X11, Y11, X12, Y12, _
test_x, test_y)
If test_dist < best_dist Then
best_dist = test_dist
near_x1 = test_x
near_y1 = test_y
near_x2 = X21
near_y2 = Y21
End If
' Check (X22, Y22) with segment 1.
test_dist = DistToSegment(X22, Y22, X11, Y11, X12, Y12, _
test_x, test_y)
If test_dist < best_dist Then
best_dist = test_dist
near_x1 = test_x
near_y1 = test_y
near_x2 = X22
near_y2 = Y22
End If
DistBetweenSegments = best_dist
End Function
|