TITLE: Closest point on circle edge from point outside/inside the circle
QUESTION [16 upvotes]: Alright, I am programming a plugin for a game that requires me to get the closest point on a circle when all you have is a point B, which is outside of the circle, the radius of the circle, and the location of the center of the circle.
Say I have this situation:

So, how would I be able to get the coordinates of point C? I need a formula that allows me to calculate those coordinates when I only know the radius of the circle and the coordinates of B. I sketched the line for ease of understanding, but all I start with is just the circle and point B. OH, one other thing, B isn't a static point, each time this calculation will be executed B will be at another position.
And, as a bonus (not really needed) would you care to show an example on how to do the same thing, but then when point B is inside the circle.
Thanks in advance!

REPLY [19 votes]: $$ \vec C = \vec A + r\frac{(\vec B - \vec A)}{||\vec B - \vec A||} $$
Where $r$ is the radius of the circle. Works for points both inside and outside the circle. Imagine $(\vec B - \vec A)$ to be a vector in the direction of $\vec B$ and $\frac{(\vec B - \vec A)}{||\vec B - \vec A||}$ thus is the same vector but with a length of $1$. By multiplying it with $r$, you "walk in that direction" a total distance of $r$, thus reaching the circle.
With coordinats $\vec A = (A_x, A_y)$ etc. this reads
$$ C_x = A_x + r\frac{B_x-A_x}{\sqrt{(B_x-A_x)^2+(B_y-A_y)^2}} $$
$$ C_y = A_y + r\frac{B_y-A_y}{\sqrt{(B_x-A_x)^2+(B_y-A_y)^2}} $$