Link to home
Start Free TrialLog in
Avatar of dj__jebus
dj__jebus

asked on

VB Calculating the distance between a point and a line segment

Hey, I'm trying to calculate the distance between a point and a line segment with a function.  The only possible line segments are in 45 degree rotations, eg: _ | /  \

I almost have it done, but my last problem is that if the line is in this orientation / it seems to move the line or somehow thing that it is like \    This is what I have so far.  THe code at the beginning prevents similar screw ups like this with the other line orientations bu just swaping the pairs of coordinates.



' Calculate the distance between the point and the segment
Public Function DistanceToLine(PX As Single, PY As Single, X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
    Dim TempX As Single, TempY As Single
    Dim DX As Single, DY As Single
    Dim T As Single
    'Swap coords if needed to keep the first one smaller
    If (X1 > X2) Or (Y1 > Y2) Then
        TempX = X1
        TempY = Y1
        X1 = X2
        Y1 = Y2
        X2 = TempX
        Y2 = TempY
    End If
    ' Calculate the distance between the point and the segment
    DX = Abs(X2 - X1)
    DY = Abs(Y2 - Y1)
    T = (PX + PY - X1 - Y1) / (DX + DY)
    If T < 0 Then
        DX = PX - X1
        DY = PY - Y1
    ElseIf T > 1 Then
        DX = PX - X2
        DY = PY - Y2
    Else
        X2 = X1 + T * DX
        Y2 = Y1 + T * DY
        DX = PX - X2
        DY = PY - Y2
    End If
    DistanceToLine = Sqr(DX * DX + DY * DY)
End Function





Thanks, Jebus
Avatar of NBrownoh
NBrownoh

that little function below will measure the length of any line, is that what you are looking for?

Public Function DistanceToLine(X1 As Double, Y1 As Double, X2 As Double, Y2 As Double) As Double
    Dim HLen As Double, WLen As Double, TRoot As Double
    HLen = Abs(X1 - X2)
    WLen = Abs(Y1 - Y2)
    HLen = HLen * HLen
    WLen = WLen * WLen
    TRoot = Sqr(WLen + HLen)
    DistanceToLine = TRoot
End Function
Avatar of dj__jebus

ASKER

Umm.. no I'm trying to give the function point X,Y and line X1,Y1,X2,Y2 and I want it to tell me the distance between them.

Thanks, Jebus

PS:  If what I have doesn't seem to work, I've uped the points for anyone and they can completely rewrite it.
ok, i am sure i can do this i just need a bit more info, you want to measure the distance from a random point to a line that will always be in 45 degree increments?

This doesnt make much sense because there are an infinite number of points on a line so you have to pick a point to measure to on the line.  unless you are trying to measure from a point to the center of line.  So where exactly on the line are you measuring to?
Avatar of Mike Tomlinson
The distance being measured, is the length of the shortest line segment required to connect the point to the line, such that the line segment and the existing line are perpendicular to each other.

Idle_Mind
ok, right on it.
ASKER CERTIFIED SOLUTION
Avatar of oboblpower
oboblpower

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ah you beat me to it.
sorry, I posted it before I saw the last two comments. Did you get the same thing I got? (wanna make sure I didn't make any stupid mistakes.. I tend to do that)

-- O'Bob
i was about to post it before i saw yours, but i did double check it and it came out to be correct by my calcs
Ok, that works awsome, the only thing is that it pretends the line carrys on infinately.  I'm sure I can easily add some code at the end though to pick up those cases.  As soon as I get that working i'll accept the answer.

Thanks, Jebus
Ok, I got it done, thanks so much here it is.

'Calculate the distanDistanceToLinece between the point and the segment
Public Function DistanceToLine(PX As Single, PY As Single, X1 As Single, Y1 As Single, X2 As Single, Y2 As Single, MaxHitDistance As Single) As Single
    Dim A As Single, B As Single, C As Single, Distance As Single
    A = Y1 - Y2
    B = X2 - X1
    C = X1 * Y2 - X2 * Y1
    Distance = Abs(A * PX + B * PY + C) / (A ^ 2 + B ^ 2) ^ 0.5
    'Crop the distance to fit the line segment and not an infinate line
    'If the line is running left to right
    If Distance < MaxHitDistance Then
        If X1 < X2 Then
            If PX < X1 Then
                DistanceToLine = X1 - PX
            ElseIf PX > X2 Then
                DistanceToLine = PX - X2
            Else
                DistanceToLine = Distance
            End If
        'If the line is running right to left
        ElseIf X1 > X2 Then
            If PX > X1 Then
                DistanceToLine = PX - X1
            ElseIf PX < X2 Then
                DistanceToLine = X2 - PX
            Else
                DistanceToLine = Distance
            End If
        'If the line is vertical
        ElseIf X1 = X2 Then
            'If the vertical line is top to bottom
            If Y1 < Y2 Then
                If PY < Y1 Then
                    DistanceToLine = Y1 - PY
                ElseIf PY > Y2 Then
                DistanceToLine = PY - Y2
                Else
                DistanceToLine = Distance
                End If
            'If the vertical line is bottom to top
            ElseIf Y1 > Y2 Then
                If PY > Y1 Then
                    DistanceToLine = PY - Y1
                ElseIf PY < Y2 Then
                    DistanceToLine = Y2 - PY
                Else
                    DistanceToLine = Distance
                End If
            End If
        End If
    Else
    DistanceToLine = Distance
    End If
End Function