Shortest Distance between a Point and a Line
The issues with lines is an infinite number of them can be drawn to connect the line and the point, but only one is the shortest: the Perpendicular.
But before we can find which perpendicular passes through the point we need to know this property, the slope of a perpendicular line is the negative reciprocal of the original line:
Which means the linear equation of the perpendicular line will look like this:
So by taking the slope of the original line and the coordinates of the point, we just need to find the y-intercept (b) of the perpendicular line:
Once we have the linear equation for the perpendicular line we just need to find the intersection point and then calculate the distance between both points. But if we’re dealing with a line segment and not an infinite line, then we also need to check if the intersection point falls within its range before calculating the distance.
However this method is unnecessary for horizontal or vertical lines. Because those are parallel to the axis the process is much more straightforward:
distance = Lx – Px
distance = Ly – Py