Finding if line or point is near a line

Quoting my answer to this question

The first step is to find the normal projection of the point onto the line. This is actually quite simple: take the distance from point 1 to the target, and point 2 to the target, and call them D1 and D2 respectively. Then calculate D1+(D2-D1)/2. This is the distance to the projected point on the line from point 1.

You can now find that point, and get the distance from that point to the target. If the distance is zero, then the target is exactly on the line. If the distance is less than 5, then the target was less than 5px away, and so on.

EDIT: A picture is worth a thousand words. Here's a diagram:

Diagram
(source: adamhaskell.net)

(In hindsight, probably should have made those circles a different colour... Also, the purple line is supposed to be perpendicular to line AB. Blame my terrible aim with the blue line!)


You need to find the distance of a point to the line, d.

First, get the slope of a line perpendicular to the original line in question. (It's convenient to keep this as a ratio: dx,dy is the original slope, dy,-dx is the perpendicular, where dx is the difference in x's in the original line, and dy is the difference in y's of the original line.)

To test a point p1, get the intersection (p2) of the original line and the perpendicular that passes through p1. In other words, the intersection of the original line with the line p2 to (p2.x+dy, p2.y-dx)

If p2 lies between the endpoints of the original line, then the distance to the line (d) is the distance between P1 and P2.

If P2 lies outside the endpoints of the original line, then the distance to the line (d) is the shorter of the distances from P1 to the original line's endpoints.

original line: points pq1 and pq2
point to measure: p1
distance to line: d

dx = pq2.x - pq1.x
dy = pq2.y - pq1.y

p2.x = p1.x + dy // get perpendicular, arbitrary length
p2.y = p1.y - dx

px = intersection(pq1-pq2, p1-p2)

if px.x is between pq1.x and pq2.x inclusive then // check y's instead if it's near vertical
  d = distance(p1-px)
else
  d = minimum(distance(p1, pq1), distance(p1, pq2))
end if