# Closest point to a unit circle from a point inside it

I'm working on a code at the moment that calculates the closest point to a circle from a point inside it. Let's say we have the point ($x_0, x_1$) to calculate the closest point to the unit circle it first calculates $\rho = \sqrt{x_0^2 + x_1^2}$ and then the closest point to the unit circle is $(x_0 / \rho, x_1 / \rho)$.

I don't know much about geometry and I don't understand how this works. I'd really appreciate it if someone could explain me why and how this works.

-
I suggest you that just draw the unit circle and one point (examples: $(3,0), (3,4)$) in the plane, and then you go to sence which point in the unit circle is closest to your point. – emiliocba Jan 29 '12 at 1:38

The radius that passes through the center and your point $(x_0,y_0)$ also gives the direct path to the closest point on the circle. So the vector $(x_0,y_0)$ needs to be scaled up until its magnitude is $1$. This is accomplished by taking $(x_0,y_0)/\rho$. (And this also applies to points outside the circle.)
The closest point to $(x_0, y_0)$ (note the change of notation) on the unit circle will be the one that's on the half-line starting at the origin and passing through $(x_0, y_0)$. This is "obvious" if you draw some pictures. It can also be proven by analytic means. We want to minimize $f(x,y) = (x-x_0)^2 + (y-y_0)^2$, subject to the constraint $g(x,y) = 0$ where $g(x,y) = x^2 + y^2 - 1$. The minimization can be done by Lagrange multipliers: we want to find points where the gradients of $f$ and $g$ are parallel. These are $(2(x-x_0), 2(y-y_0))$ and $(2x, 2y)$ respectively; thus $(x-x_0, y-y_0)$ must be a constant multiple of $(x,y)$. So $(x, y)$ is a constant multiple of $(x_0, y_0)$. In particular, for $(x,y)$ to have length $1$ it must be $(x_0, y_0)/\sqrt{x_0^2+y_0^2}$, which is the formula you asked about.