14
$\begingroup$

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

$\endgroup$
11
  • $\begingroup$ You did a mistake after subtracting the equations. $\endgroup$ May 11, 2016 at 20:24
  • 1
    $\begingroup$ Hint: I think your life gets easier if you first translate to get one of your given points to the origin. Then if $(x_0,y_0)$ is the center of the circle you have $x_0^2+y_0^2=r^2$ and $2x_1x_0+2y_1y_0=x_1^2+y_1^2$. $\endgroup$
    – lulu
    May 11, 2016 at 20:24
  • $\begingroup$ @ChrisApostol where after subtraction? Just the line # would be nice... $\endgroup$ May 11, 2016 at 20:25
  • $\begingroup$ A circle is uniquely defined by three points. Given only two points and a radius gives two possible circles with different centers. Are you accounting for that in any way? $\endgroup$
    – M47145
    May 11, 2016 at 20:26
  • 1
    $\begingroup$ At fifth line it's $-{x_2}^2,-{y_2}^2$. $\endgroup$ May 11, 2016 at 20:28

3 Answers 3

23
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Interesting concept! I'm not going to change the code now that its working, but this may have been a bit easier... $\endgroup$ May 11, 2016 at 21:50
  • $\begingroup$ Generally it's wise not to fix what ain't broke, and I bet that applies to your situation. But I'll leave this answer up in case someone else comes along trying to solve the same problem; it's not all that unusual a thing to want to do. $\endgroup$
    – David K
    May 11, 2016 at 21:52
  • 1
    $\begingroup$ +1 Just what I was going to propose as an alternative solution. Personally, I found this approach to be less error-prone than solving a system of second-degree equations. $\endgroup$
    – amd
    May 11, 2016 at 22:12
  • $\begingroup$ Thank you, @Tom, for adding the helpful figure. $\endgroup$
    – David K
    Apr 24, 2017 at 1:18
  • $\begingroup$ Coming from LeetCode leetcode.com/problems/…, this solution is perfect! Thanks! $\endgroup$
    – Alan
    May 18, 2020 at 5:35
1
$\begingroup$

Here's a list of the errors:

  1. You lost a negative when subtracting equations, so $c=x_1^2+y_1^2-x_2^2-y_2^2$.
  2. When you divided by $2y_3$ in one equation, you forgot to distribute out the error, so the correct equation is $y=-\frac{x_3}{y_3}x+\frac{1}{2y_3}c$. This also affects the definition of $c_y$, which is now $\frac{1}{2y_3}c-y_1$.
  3. You lost another negative when rearranging terms for the quadratic, so we actually have $b=-2x_1-2c_y\left(\frac{x_3}{y_3}\right)$.
  4. Finally, you forgot to square $c_y$ when calculating $c$ at the bottom.

Thus, we have the following: $$a=2 \ \text{(Nothing changed here.)}$$ $$c=x_1^2+y_1^2-x_2^2-y_2^2=0$$ $$c_y=\frac{1}{2y_3}c_1-y_1=-y_1=-2$$ $$b=-2x_1-2c_y\left(\frac{x_3}{y_3}\right)=-4$$ $$c=(x_1^2+c_y^2-r^2)=(0^2+(-2)^2-2^2)=0$$ $$x=\frac{-b \pm \sqrt{b^2-4ac}}{2a}=1 \pm 1$$ $$y=-\frac{x_3}{y_3}x+\frac{1}{2y_3}c=x=1 \pm 1$$

Thus, our centers are $(0, 0)$ and $(2, 2)$, which is the correct answer.

$\endgroup$
1
  • $\begingroup$ Thanks! I'll go through and try and figure this out myself. $\endgroup$ May 11, 2016 at 21:03
0
$\begingroup$

Where you have "$y = -\frac {x_ 3} {y_ 3} + \frac {1} {2} c$", you should have "$y = -\frac {x_ 3} {y_ 3} + \frac {1} {2 y_ 3} c$". There may be more errors, but I stopped there since this omission is propagated subsequently so is likely not a typographical omission.

$\endgroup$
3
  • $\begingroup$ There were other errors, as discussed in the comments. $\endgroup$ May 11, 2016 at 20:49
  • $\begingroup$ @NobleMushtak : Typical. Relevant answers are not being posted as answers. $\endgroup$ May 11, 2016 at 20:54
  • $\begingroup$ I am writing the answer right now, it's just that I just finished my work and finally got the right answer. $\endgroup$ May 11, 2016 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .