Get the equation of a circle when given 3 points
Follow these steps:
- Consider the general equation for a circle as $(x-x_c)^2+(y-y_c)^2 - r^2 = 0$
- Plug in the three points to create three quadratic equations $$ (1-x_c)^2+(1-y_c)^2 - r^2 = 0 $$ $$ (2-x_c)^2+(4-y_c)^2 - r^2 = 0 $$ $$ (5-x_c)^2+(3-y_c)^2 - r^2 = 0 $$
- Subtract the first from the second, and the first from the third to create two linear equations $$ -2 x_c -6 (y_c-3)=0 $$ $$ (y_c+7)-6 x_c = 0 $$
- Solve for the center as $$ (x_c,y_c) = (3,2) $$
- Plug the values for the center in any of the three quadratic equations above (I choose the first) and solve for $r$ $$ (1-3)^2+(1-2)^2-r^2 = 0 $$ $$ 5-r^2 = 0 $$ $$ r = \sqrt 5 $$
- Verify result with GeoGebra (optional)
Based on what I've learnt from this page by Stephen R. Schmitt (thanks to @Dan_Uznanski for pointing the concept out), there's a faster way to do it using matrices:
$$\text{let }〈x_1, y_1〉, 〈x_2, y_2〉, 〈x_3, y_3〉\text{ be your 3 points, and let }〈x_0, y_0〉\text{ represent the center of the circle.}\\\ \\ \text{let }A = \left[\begin{array}{cccc} x^2+y^2 & x & y & 1\\ x_1^2+y_1^2 & x_1 & y_1 & 1\\ x_2^2+y_2^2 & x_2 & y_2 & 1\\ x_3^2+y_3^2 & x_3 & y_3 & 1\\ \end{array}\right] = \left[\begin{array}{cccc} x^2+y^2 & x & y & 1\\ 1^2+1^2 & 1 & 1 & 1\\ 2^2+4^2 & 2 & 4 & 1\\ 5^2+3^2 & 5 & 3 & 1\\ \end{array}\right] = \left[\begin{array}{cccc} x^2+y^2 & x & y & 1\\ 2 & 1 & 1 & 1\\ 20 & 2 & 4 & 1\\ 34 & 5 & 3 & 1\\ \end{array}\right]\\\ \\\ \\\ \text{Note: }M_{11} = 0 ⟹ 〈x_0, y_0〉\text{ is undefined}∧\text{the points lie on a line rather than a circle.}\\\ \\\ x_0 = \frac{1}{2} ⋅ \frac{M_{12}}{M_{11}} = \frac{1}{2} ⋅ \left|\begin{array}{cccc} 2 & 1 & 1\\ 20 & 4 & 1\\ 34 & 3 & 1\\ \end{array}\right| \Bigg{/} \left|\begin{array}{cccc} 1 & 1 & 1\\ 2 & 4 & 1\\ 5 & 3 & 1\\ \end{array}\right| = \frac{1}{2} ⋅ \frac{-60}{-10} = 3 $$
$$ y_0 = -\frac{1}{2} ⋅ \frac{M_{13}}{M_{11}} = -\frac{1}{2} ⋅ \left|\begin{array}{cccc} 2 & 1 & 1\\ 20 & 2 & 1\\ 34 & 5 & 1\\ \end{array}\right| \Bigg{/} \left|\begin{array}{cccc} 1 & 1 & 1\\ 2 & 4 & 1\\ 5 & 3 & 1\\ \end{array}\right| = -\frac{1}{2} ⋅ \frac{40}{-10} = 2 $$
The radius, $r$, can then be calculated with Pythogoras' theorem, or using matrices again:
$$r^2 = x_0^2 + y_0^2 + \frac{M_{14}}{M_{11}}$$
- $M_{12}(A)$ is a minor of A — the determinant of $A$ without row $1$ or column $2$.
- Lines on either side of a matrix indicate the determinant (e.g. $|A|$) (sometimes written $\det{(A)}$)
If you have the time, it's really worth learning about matrices — they make a lot of things much faster, and are just generally awesome! KhanAcademy has a pretty easy introduction to matrices and linear algebra.
I'm surprised this one hasn't been mentioned; you can find the equation by using the determinant of a matrix:
$$\left|\begin{array}{cccc} x^2+y^2&x&y&1\\ 1^2+1^2&1&1&1\\ 2^2+4^2&2&4&1\\ 5^2+3^2&5&3&1\\ \end{array}\right|=0$$ This gives the equation of the circle through those three points. This sort of thing can be used in a lot of situations: matrix-determinant solutions are available for any shape I can think of where you're given points that land on the shape.
Implementing this on a computer involves having a thing that calculates determinants; to do it numerically you'll need to apply the cofactors method to avoid multiplying the variables in.