# Finding the minimal distance from a point to a line

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 , and a line, represented by the line formula . 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 and , the distance 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 and ALL other points in the line . So, we will have to modify our formula to accept not two points, but a point and two variables, and , that will give us the distance between and our variables. However, our variable is dependent on ( is a function of ), so we can rewrite out variables like so: where .

So now we have a function, the distance function, that will give us the distance between our point , and and all other points in the line . So, given a particular x value, we will have the distance between and .

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 . Let’s do it step by step, and by rewriting some of the terms to make it clearer.

*Note the use of the chain rule on *

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

To find the minimal distance, we set . Knowing this, we can greatly simplify the formula above:

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.

So let’s give our new formula a try. Given the point and the line , we will plug these numbers in above to try to find a solution: