TitleFind the shortest distance between two line segments
DescriptionThis example shows how to find the shortest distance between two line segments in Visual Basic 6.
Keywordssegment, distance, geometry
CategoriesGraphics, Algorithms

Click four points to pick the end points for the two segments. The program finds the closest points on the two segments and connects them. Function DistBetweenSegments returns the smallest distance between the segments. It uses ByRef parameters near_x1, near_y1, near_x2, and near_y2 to return the points on the two segments that are nearest.

The closest distance between two segments is either zero if they intersect or the distance from one of the lines' end points to the other line. DistBetweenSegments first calls SegmentsIntersect to see if the segments intersect. If they do, it returns zero. It returns the point of intersection returned by SegmentsIntersect for both of the closest points.

If the segments do not intersect, the function calculates the shortest distance from the first segment's end points to the second segment and vice versa and returns the shortest distance.

' 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

For information on function SegmentsIntersect, see Determine whether two line segments intersect.

For information on function DistToSegment, see Find the distance between a point and a line segment.

Download

Copyright © 1997–2006 Rocky Mountain Computer Consulting, Inc. All rights reserved.

http://www.vb-helper.com/howto_distance_segment_to_segment.html

Updated 11/10/2010 20:06:54