Solved

VB  Calculating the distance between a point and a line segment

Posted on 2003-11-14
11
1,245 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:dj__jebus
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9753141
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
0
 

Author Comment

by:dj__jebus
ID: 9753150
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.
0
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9753158
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?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9753204
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
0
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9753217
ok, right on it.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Accepted Solution

by:
oboblpower earned 215 total points
ID: 9753225
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
0
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9753248
ah you beat me to it.
0
 
LVL 1

Expert Comment

by:oboblpower
ID: 9753253
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
0
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9753294
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
0
 

Author Comment

by:dj__jebus
ID: 9754883
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
0
 

Author Comment

by:dj__jebus
ID: 9755055
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now