Calculating distances between elements is a common operation when dealing with graphics. In our case, we want the minimal distance between a point, represented by two coordinates (P_x,P_Y), and a line, represented by the line formula y=ax+b. There are many different methods of determining this distance, but we will be using calculus, so this method should hopefully be clear to anyone who understands the basics of calculus.

There are two major steps to solving our problem:

  • Find the distance between our point and all other points in the line
  • Pick out the smallest of these distances

Finding the distance between two points is not difficult, usually a slightly modified pythagorean formula is used. Given two points (x_1,y_1) and (x_2,y_2), the distance d is equal to:


This formula will always give us a non-negative number, since distances can’t be negative. However, we are not trying to find the distance between two particular points, but between one point (P_x,P_y) and ALL other points in the line y=ax+b. So, we will have to modify our formula to accept not two points, but a point and two variables, x and y, that will give us the distance between (P_x,P_y) and our variables. However, our variable y is dependent on x (y is a function of x), so we can rewrite out variables like so:(x,f(x)) where f(x)=ax+b.


So now we have a function, the distance function, that will give us the distance between our point (P_x, P_y), and and all other points in the line f(x)=ax+b. So, given a particular x value, we will have the distance between (P_x,P_y) and (x,f(x)).

But how do we find the minimal distance between our new function and our point? This is where the calculus part comes in. A function reaches it’s minimal value whenever the derivative of that function is equal to 0. We will have to calculate the derivative of our function d(x) . Let’s do it step by step, and by rewriting some of the terms to make it clearer.


g(x) = {P_x}^2-2{P_x}x+x^2+ {P_y}^2-2{P_y}f(x) + {f(x)}^2
g(x) = {P_x}^2-2{P_x}x+x^2+ {P_y}^2-2{P_y}ax-2{P_y}b + {a^2}{x^2}+2axb+{b^2}

g'(x)=-2{P_x}+2x-2{P_y}a-2{P_y}b+2{a^2}x +2ab

Note the use of the chain rule on {f(x)}^2

d(x) = \sqrt{g(x)}

d'(x) = {{g'(x)}\over{2\sqrt{g(x)}}}

We are using the chain rule here as well, inside the square root

d'(x)={{-2{P_x}+2x-2{P_y}a-2{P_y}b+2{a^2}x +2ab}\over{2\sqrt{{P_x}^2-2{P_x}x+x^2+ {P_y}^2-2{P_y}ax-2{P_y}b + {a^2}{x^2}+2axb+{b^2}}}}

To find the minimal distance, we set d'(x)=0. Knowing this, we can greatly simplify the formula above:

d'(x)={{-2{P_x}+2x-2{P_y}a-2{P_y}b+2{a^2}x +2ab}\over{2\sqrt{{P_x}^2-2{P_x}x+x^2+ {P_y}^2-2{P_y}ax-2{P_y}b + {a^2}{x^2}+2axb+{b^2}}}}=0

We can divide both sides by the denominator. This simplification is generally not always valid, due to cases of division by zero, however, in our case, the only time when there is division by zero is when the distance is zero. In that case, the numerator is also zero, and we already have a solution, since zero is the smallest possible distance. So we will accept this simplification, as it always leads to a correct answer.

d'(x)={-2{P_x}+2x-2{P_y}a-2{P_y}b+2{a^2}x +2ab}=0

So let’s give our new formula a try. Given the point (1,3) and the line y=2x+3, we will plug these numbers in d'(x) above to try to find a solution: