What is easiest way to find the a point on a line $(a1, b1)$, $(a2, b2)$ or the extension of the line, which is nearest to a point $(x1, y1)$.

What is easiest way to find the a point on a line $(a1, b1)$, $(a2, b2)$ or the extension of the line, which is nearest to a point $(x1, y1)$.

The points on the line containing $(a_1,b_1),(a_2,b_2)$ are given by $(a_1,b_1) + t
At the nearest point, the angle that the line between $x_1x_2$ and $(?_1,?_2)$ makes with the line through $a_1b_1$ is 90. So its gradient is $\frac{-1}{g}$ if g is the gradient of the given line. Now make this line go through $(x_1,x_2)$ and find the intersection with the line through $(a_1,b_1)$.