Algebra – Finding the Center of a Circle Given Two Points and a Radius

circles

Preface: I'm writing a program in which I need to find the center of a circle, given two points on the circle, and the radius. Therefore, a construction or doing the problem out by hand is not an option, I need to derive an equation for the center point.

Note: I Understand there are two circles that fit with this model, I'd like to find the centers of both.

Attempts: I attempted to follow the logic here (see below), but after deriving an equation, I tested my equation against a simple circle (center $(0,0)$, points $(2, 0)$ and $(0, 2)$, and radius $2$), and it failed. I'm not sure if I'm approaching it wrong, or if I made an algebraic error. I've gone over it a few times, but here's my derivation:

Given points $(x_1, y_1)$ and $(x_2, y_2)$ on the circle, and the radius $r$:

Circle equations:

$$(x – x_1)^2 + (y – y_1)^2 = r^2$$
$$(x – x_2)^2 + (y – y_2)^2 = r^2$$
Subtract the equations (and expand the squared binomials):
$$(x^2 – 2(x_1)(x) + x_1^2 + y^2 – 2(y_1)(y) + y_1^2) -$$
$$(x^2 – 2(x_2)(x) + x_2^2 + y^2 – 2(y_2)(y) + y_2^2)= 0$$
$$-2(x_1)(x) – 2(y_1)(y) + 2(x_2)(x) + 2(y_2)(y) + x_1^2 + y_1^2 – x_2^2 – y_2^2 = 0$$
$$-2x(x_1 – x_2) -2y(y_1 – y_2) + x_1^2 + y_1^2 – x_2^2 – y_2^2 = 0$$
$$Let\;c = x_1^2 + y_1^2 – x_2^2 – y_2^2$$
$$Let\;x_3 = x_1 – x_2$$
$$Let\;y_3 = y_1 – y_2$$
$$2y(y_3) = -2x(x_3) + c$$
$$y = -\frac{x_3}{y_3}x + \frac{1}{2y_3}c$$
Substitute value into previous equation:
$$(x – x_1)^2 + (-\frac{x_3}{y_3}x + \frac{1}{2y_3}c – y_1)^2 = r^2$$
$$Let\;c_y = \frac{1}{2y_3}c – y_1$$
$$(x – x_1)^2 + (-\frac{x_3}{y_3}x + c_y)^2 = r^2$$
$$(x^2 – 2(x_1)(x) + x_1^2) + ((\frac{x_3}{y_3}x)^2 + 2(c_y)(-\frac{x_3}{y_3}x) + c_y^2) = r^2$$
$$(x^2 + (\frac{x_3}{y_3}x)^2) + (-2(x_1)(x) + 2(c_y)(-\frac{x_3}{y_3}x)) + (x_1^2 + c_y^2 – r^2) = 0$$
The above is now a quadratic equation. Simplifying $ax^2, bx$, and $c_2$:
$$ax^2 = (x^2 + (\frac{x_3}{y_3}x)^2) = (x^2 + (\frac{x_3^2}{y_3^2}x^2)) = \frac{x^2y_3^2}{y_3^2} + \frac{x^2x_3^2}{y_3^2} = (\frac{x^2x_3^2 + x^2y_3^2}{y_3^2}) = x^2(\frac{x_3^2 + y_3^2}{y_3^2})$$
$$bx = (-2(x_1)(x) + 2(c_y)(-\frac{x_3}{y_3}x)) = x(-2x_1 – 2c_y(\frac{x_3}{y_3}))$$
$$c_2 = (x_1^2 + c_y^2 – r^2)$$
The center point(s) will be:
$$(\frac{-b \pm \sqrt{b^2 – 4ac_2}}{2a}, -\frac{x_3}{y_3}x + \frac{1}{2y_3}c)$$


Now for the example:

Suppose we have a circle, with its center at the origin and a radius of $2$. It is then common sense that said circle will intersect the points $(0, 2)$ and $(2, 0)$. The center could also be at $(2, 2)$, and meet the other constraints. Hence the quadratic in the derived equation.

Radius: $2$

$(x_1, y_1)$: $(0, 2)$

$(x_2, y_2)$: $(2, 0)$

Finding $a$:
$$(\frac{x_3^2 + y_3^2}{y_3^2}) = (\frac{(x_1 – x_2)^2 + (y_1 – y_2)^2}{(y_1 – y_2)^2}) = \frac{(0 – 2)^2 + (2 – 0)^2}{(2 – 0)^2} = \frac{8}{4} = 2$$

Finding $b$:
$$(2x_1 – 2c_y(\frac{x_3}{y_3})) = – 2c_y(\frac{x_3}{y_3}) = -2c_y(\frac{x_1 – x_2}{y_1 – y_2}) = -2c_y(\frac{-2}{2}) = 2c_y = 2(\frac{1}{2}c – y_1) = 2(\frac{1}{2}(x_1^2 + y_1^2 – x_2^2 – y_2^2) – y_1) = 2(-2) = -4$$

Finding $c$:
$$(x_1^2 + c_y^2 – r^2) = (-4 + (\frac{1}{2y_3}c – y_1)^2) = (-4 + 4) = 0$$

Solving the Quadratic:
$$\frac{-b \pm \sqrt{b^2 – 4ac}}{2a} = \frac{4 \pm \sqrt{16}}{4} = 2\;or\;0$$


Clearly, I've done something wrong. The expected $x$-values are $0$ and $2$ (which I realize would result if $b$ was negative and $c$ was $0$, so I feel I'm close) (or if, in the $c$ equation, it was $x_2^2$ instead of $x_1^2$.) (Of course, I realize these are only in this single case, but I feel there must be a simple off by one error or something, this is the second time I've tried to derive this formula, but I just can't get it right).

Any help is appreciated!


Edit 1 Notes: I've fixed the error in the 5th line, where I subtracted the equations incorrectly, and now I have roots that are off by exactly one. Thanks for the help, I'm so close!

Edit 2 Notes: "Merged" Nobled Mushtak's corrections. Derivation is now complete. Thanks everyone!

Edit 3 Notes: Anyone interested can find the code that represents the derivation here

Best Answer

Here's a completely different approach that might be easier to encode.

Your two given points ($(x_1, y_1)$ and $(x_2, y_2)$) and the centers of the two desired circles are at the four vertices of a rhombus with side length $r$. You can use the Pythagorean Theorem to find the length of the diagonal of the rhombus from $(x_1, y_1)$ to $(x_2, y_2)$. Better still, divide by two so you now have half the length of the diagonal. The two diagonals of a rhombus are perpendicular, so the point $(x_1, y_1)$, the center of the rhombus, and one of the circles' centers make a right triangle with hypotenuse $r$ and one leg equal to half the known diagonal. Use the Pythagorean Theorem to find half the length of the other diagonal.

Now you just need to construct line segments of that length with one end at the center of the rhombus, perpendicular to the known diagonal, and the other end of each segment will be the center of one of the desired circles.

That's the entire rationale of the procedure. For the detailed calculations, I'll assign names to various lengths as we go along in order to keep the equations from getting too ugly. (You'd probably want to do this anyway when you do this in software.)

enter image description here

Let $x_a = \frac12(x_2 - x_1)$ and $y_a = \frac12(y_2 - y_1)$; then the center of the rhombus is at $(x_0,y_0) = (x_1+x_a, y_1+y_a)$ and half the length of the known diagonal is $a = \sqrt{x_a^2 + y_a^2}.$ The length of the other diagonal is $b = \sqrt{r^2 - a^2}.$

Now suppose (for example) that $(x_0,y_0)$ is above and to the right of $(x_1,y_1)$ (assuming, at least for now, the usual mathematical Cartesian $x,y$ coordinates). To get from $(x_1,y_1)$ to $(x_0,y_0)$ along a straight line, we go to the right $x_a$ units and up $y_a$ units for every $a$ units of movement. So to get from $(x_0,y_0)$ to the center of one of the circles, $(x_3,y_3)$, we can just turn those rules $90$ degrees: we can go down $x_a$ units and to the right $y_a$ units for every $a$ units of movement; that is, $\frac{x_a}{a}$ down and $\frac{y_a}{a}$ right for each unit traveled along the diagonal. But the distance from $(x_0,y_0)$ to $(x_3,y_3)$ is $b$, so we will end up going down $b\frac{x_a}{a}$ units and to the right $b\frac{y_a}{a}$ units. In other words, \begin{align} x_3 &= x_0 + \frac{b y_a}{a}, \\ y_3 &= y_0 - \frac{b x_a}{a}. \\ \end{align} To get to the center of the other circle, $(x_4,y_4)$, we just go the same amount in the opposite direction from $(x_0,y_0)$: \begin{align} x_4 &= x_0 - \frac{b y_a}{a}, \\ y_4 &= y_0 + \frac{b x_a}{a}. \\ \end{align}

If $(x_0,y_0)$ is not above and to the right of $(x_1,y_1)$, or if you're working in a graphics coordinate system with $y$ downward, or both, the signs of either $x_a$ or $y_a$ (or both) might be negative instead of positive; but all the formulas above continue to work exactly the same way as before.