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