# Determine the closest point along a circle's $(x_1, y_1)$ radius from any point $(x_2, y_2)$, inside or outside the radius of the circle.

I have a circle centered at point $(x_1, y_1)$ and another point at $(x_2, y_2)$. This point, $(x_2, y_2)$ may or may not be within the radius ($r$) of the circle. I wanted to create a line going from the center of the circle $(x_1, y_1)$ to the point $(x_2, y_2)$. I have been using the quadratic formula to determine the intersection between this line and the radius of the circle. In my application, the point $(x_2, y_2)$ was always outside of the circle, so it made sense to always have a single intersection point along the radius of the circle. However, when $(x_2, y_2)$ is within the circle, I was not able to determine a point of intersection.

Is there a simple way to determine the closest point from $(x_2, y_2)$ to the circle's radius? If the line from $(x_1, y_1)$ were to extend forever, it would be the intersecting point of this extended line the circle's radius.

I am using the quadratic equation, but I am having troubles figuring out the intersection, without artificially extending the line. Is it also possible to determine the closest point from $(x_2, y_2)$ to the circle's radius if you don't know whether it is in or out of the radius? If wondering, I am using this in a coding environment.

I added my Inkscape drawing (editing with Paint it to show my application to match the question; please ignore the missing line and resulting gap from the circle).

Thank you.

-
You must learn LaTeX markup! – kjetil b halvorsen Mar 28 '14 at 19:34
Thanks. I was not sure that it could be used here. – baconcow Mar 29 '14 at 17:05

I eventually found this answer here. Might be best to mark as duplicate, as this solution worked for me. Here is a summary of the solution as required within my application.

Point of intersection along the circumference of a circle centered at $(x_1, y_1)$ with a radius, $r$.

$x_{int} = x_1+\frac{r (x_2-x_1)}{\sqrt{(x_2-x_1)+(y_2-y_1)}}$

$y_{int} = y_1+\frac{r (y_2-y_1)}{\sqrt{(x_2-x_1)+(y_2-y_1)}}$

Where $(x_{int}, y_{int})$ is the point of intersection along the circumference of the circle with range $r$.

This apparently works for both scenarios where point $(x_2, y_2)$ is either within or outside of the radius of the circle.

-

I would have added this as a comment to baconcow's answer, however I don't have enough points.

The formula didn't work for me because he forgot to square the parts of the equation in the denominator. I also found I needed to use the absolute value of the denominator before getting the square root because there were situations where a negative number resulted from the subtraction.

There was a similar post that shed some light on what I was doing wrong: Closest point on circle edge from point outside/inside the circle, URL (version: 2012-04-03): http://math.stackexchange.com/q/127615.

Rewritten, it looks like this:

$x_{int} = x_1+\frac{r (x_2-x_1)}{\sqrt{||(x_2-x_1)||^2+||(y_2-y_1)||^2}}$

$y_{int} = y_1+\frac{r (y_2-y_1)}{\sqrt{||(x_2-x_1)||^2+||(y_2-y_1)||^2}}$

I am using the formula in ECMA script to connect two circles with a line using SVG. I wanted the line to plot from the closest point on the outside of one circle to the closest point on the outside of the second circle. Here's how the formula looks in code:

    var dist = Math.sqrt(
Math.abs(
Math.pow(
x1 - x2, 2
) +
Math.pow(
y1 - y2, 2
)
)
);

var intX1 = x1 + ((radius1*(x2 - x1))/(dist));
var intY1 = y1 + ((radius1*(y2 - y1))/(dist));
var intX2 = x2 + ((radius2*(x1 - x2))/(dist));
var intY2 = y2 + ((radius2*(y1 - y2))/(dist));


As you can probably surmise, I'm not a mathematician. If I've misrepresented the math, please let me know. Also I hope posting code here isn't a faux pas; I just thought it might be a useful shortcut for someone looking for a quick answer.

-