Solved

# VB  Calculating the distance between a point and a line segment

Posted on 2003-11-14
1,363 Views
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
Question by:dj__jebus
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2
• +1

LVL 3

Expert Comment

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

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

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 86

Expert Comment

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

ID: 9753217
ok, right on it.
0

LVL 1

Accepted Solution

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

ID: 9753248
ah you beat me to it.
0

LVL 1

Expert Comment

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

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

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

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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â€¦
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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â€¦
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This lâ€¦
###### Suggested Courses
Course of the Month1 day, 19 hours left to enroll