As a graphics programmer, I often need to find a point on a hyperplane (line or plane) closest to a given point. So far my understanding of the problem has been based on trigonometric concepts that is probably the simplest way of approaching the problem. I’m not going to explain the boring trigonometric approach to the problem here. You can find that on some other website using Google.
A few weeks back I came across a new concept in maths called Lagrange multipliers. In addition to the problem that I was addressing at the time, I was interested in seeing if it could be applied to find a point on a hyperplane closest to a given point.
If you are not familiar with Lagrange multipliers then just go to the Wikipedia page that explains it all. I think they give a better explanation of the problem than I ever will. Ok, so if you haven’t already figured out how we are going to approach this problem, read on.
Lets work in 2 dimensions with a line of the form
Lets try and visualize this…
The vector represents the normal to the line
The constant is the distance to closest point on the line to the origin.
If you are used to only seeing the line in the slope intercept form as
then we can simply assume that
, and .
Now lets take our point in space. Now we would like to do is minimise the Euclidean distance to this point. The Euclidean distance is calculated using the equation
Minimizing this problem is dead simple. The closest point to a point is the point it self. But now we would like to add a constraint such that the closest point must lie of the line, whose equation is
We approach this problem using a simple form of the Lagrange multiplier where we minimize the function subject to the constraint . The best way to show that this works is with an example so…
Lets try this with an example
Lets assume we have a line of the form
and we would like to find a point on this line that is closest to a given point
We know the general form of the Lagrange multiplier which states that
In English this simply means the that we need to find a point where the functions and give us vectors that point in the same direction. By multiplying one of the vectors with a constant we make both vectors equal in length and direction. We could also write the equation above as
Before we start we change our distance function to a squared distance function. So in this case would become
We can do this because is just a function that needs to be minimised and for us it does not make a difference if we minimise the distance or the squared distance. We make this change as differentiating the squared distance function is much simpler and results in a system of linear equations. The function remains the same.
So now we need to find
In this case
We now need to find
After doing all the hairy math, we get
We now have a system of three linear equations with three unknowns. This should be fairly easy to solve using a number of methods. If we had used the distance function instead of the squared distance, we would have ended up with a system of non-linear equations which would not been so easy to solve.
Ok so after solving for we get
We are not really interested in anymore, so we shall leave that out. So the point on the line is
We can use several different methods to verify if this answer is correct. We can solve for the same point using the trigonometric method or we can check if the vectors and are orthonormal to each other. Here is just another point on our line. We shall use the latter method to verify our results. We already have
, . We can find by just setting in line equation. So we get
So we get
…or in plain English: IT WORKS!!