Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

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

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

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.

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?

Idle_Mind

-- O'Bob

Thanks, Jebus

'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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

|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