Home All Groups Group Topic Archive Search About

Distance between a line and a point

Author
15 Oct 2005 1:33 PM
Ricardo Furtado
I'm programming in VB .Net but this might be more a trigonometry problem.
i've been searching every maths book i can find and i haven't found anything
i can use with VB.
So, what i need to know is how to calculate the distance, between a line
(defined by two points) and a third point, to that line (the lesser distance)


My thanks in advanced

Author
15 Oct 2005 2:09 PM
Rick Rothstein [MVP - Visual Basic]
> I'm programming in VB .Net but this might be more a trigonometry
problem.
> i've been searching every maths book i can find and i haven't
found anything
> i can use with VB.
> So, what i need to know is how to calculate the distance,
between a line
> (defined by two points) and a third point, to that line (the
lesser distance)

Below is a section of code I've taken from another program of mine
that gives you what you want. I've left the comments in place in
case that helps you any. There are only 4 lines of actual
executing code... the 3 lines starting with A=, B= and C= plus the
last line starting with DistPtToLine= (which yields the answer you
are seeking).

' If you take the general formula for a line
' y = mx + b and substitute two points (X1, Y1)
' and (X2, Y2) into it one at a time, solve the two
' equations simultaneously and then manipulate it
' into the more general equation Ax + By + C = 0,
' you eventually arrive at the solution for A, B, C.
A = Y2 - Y1
B = X1 - X2
C = X2 * Y1 - Y2 * X1
' We do the above because knowing A, B, C for the
' above more general formula for a line allows us
' to use the following formula for the offset
' distance from any point (PX, PY) to the above
' described line:
DistPtToLine = Abs((A * PX + B * PY + C) / _
                   Sqr(A * A + B * B))

Rick