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
dj__jebusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NBrownohCommented:
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
dj__jebusAuthor Commented:
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.
NBrownohCommented:
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?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
NBrownohCommented:
ok, right on it.
oboblpowerCommented:
The shortest distance between a point (x0,y0) and the line ax+by+c=0 is

|ax0 + by0 + c|/sqrt(a^2 + b^2)

Using that, this function should work:

Public Function DistanceToLine(PX As Single, PY As Single, X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
Dim A As Single, B As Single, C As Single
A = Y1 - Y2
B = X2 - X1
C = X1 * Y2 - X2 * Y1
DistanceToLine = Abs(A * PX + B * PY + C) / (A ^ 2 + B ^ 2) ^ 0.5
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NBrownohCommented:
ah you beat me to it.
oboblpowerCommented:
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
NBrownohCommented:
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
dj__jebusAuthor Commented:
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
dj__jebusAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.