Find the Center of a Circle Defined by 3 Points¶
In [1]:
from sympy import *
from IPython.display import Math
x0, y0, x1, y1, x2, y2, xc, yc, r = symbols('x0, y0, x1, y1, x2, y2, xc, yc, r')
xc0, yc0, r0, xc1, yc1, r1 = symbols('xc0, yc0, r0, xc1, yc1, r1')
eq0, eq1, eq2 = symbols('eq0 , eq1, eq2')
eq0 = Eq((x0-xc)**2 + (y0-yc)**2, r**2)
eq1 = Eq((x1-xc)**2 + (y1-yc)**2, r**2)
eq2 = Eq((x2-xc)**2 + (y2-yc)**2, r**2)
display(Math(latex(eq0)))
display(Math(latex(eq1)))
display(Math(latex(eq2)))
$\displaystyle \left(x_{0} - xc\right)^{2} + \left(y_{0} - yc\right)^{2} = r^{2}$
$\displaystyle \left(x_{1} - xc\right)^{2} + \left(y_{1} - yc\right)^{2} = r^{2}$
$\displaystyle \left(x_{2} - xc\right)^{2} + \left(y_{2} - yc\right)^{2} = r^{2}$
In [2]:
s = solve([eq0, eq1, eq2], (xc, yc, r))
# there are two solutions, r > 0 and r < 0
xc0, yc0, r0 = s[0]
print("\nxc0 =", xc0)
print("\nyc0 =", yc0)
print("\nr0 =", r0)
xc1, yc1, r1 = s[1]
print("\nxc1 =", xc1)
print("\nyc1 =", yc1)
print("\nr1 =", r1)
xc0 = (x0**2*y1 - x0**2*y2 - x1**2*y0 + x1**2*y2 + x2**2*y0 - x2**2*y1 + y0**2*y1 - y0**2*y2 - y0*y1**2 + y0*y2**2 + y1**2*y2 - y1*y2**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1)) yc0 = -(x0**2*x1 - x0**2*x2 - x0*x1**2 + x0*x2**2 - x0*y1**2 + x0*y2**2 + x1**2*x2 - x1*x2**2 + x1*y0**2 - x1*y2**2 - x2*y0**2 + x2*y1**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1)) r0 = -sqrt((x0**2 - 2*x0*x1 + x1**2 + y0**2 - 2*y0*y1 + y1**2)*(x0**2 - 2*x0*x2 + x2**2 + y0**2 - 2*y0*y2 + y2**2)*(x1**2 - 2*x1*x2 + x2**2 + y1**2 - 2*y1*y2 + y2**2))/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1)) xc1 = (x0**2*y1 - x0**2*y2 - x1**2*y0 + x1**2*y2 + x2**2*y0 - x2**2*y1 + y0**2*y1 - y0**2*y2 - y0*y1**2 + y0*y2**2 + y1**2*y2 - y1*y2**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1)) yc1 = -(x0**2*x1 - x0**2*x2 - x0*x1**2 + x0*x2**2 - x0*y1**2 + x0*y2**2 + x1**2*x2 - x1*x2**2 + x1*y0**2 - x1*y2**2 - x2*y0**2 + x2*y1**2)/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1)) r1 = sqrt((x0**2 - 2*x0*x1 + x1**2 + y0**2 - 2*y0*y1 + y1**2)*(x0**2 - 2*x0*x2 + x2**2 + y0**2 - 2*y0*y2 + y2**2)*(x1**2 - 2*x1*x2 + x2**2 + y1**2 - 2*y1*y2 + y2**2))/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))
In [3]:
display(Math("\nxc0 ="+latex(xc0)))
display(Math("\nyc0 ="+latex(yc0)))
display(Math("\nr0 ="+latex(r0)))
display(Math("\nxc1 ="+latex(xc1)))
display(Math("\nyc1 ="+latex(yc1)))
display(Math("\nr1 ="+latex(r1)))
$\displaystyle
xc0 =\frac{x_{0}^{2} y_{1} - x_{0}^{2} y_{2} - x_{1}^{2} y_{0} + x_{1}^{2} y_{2} + x_{2}^{2} y_{0} - x_{2}^{2} y_{1} + y_{0}^{2} y_{1} - y_{0}^{2} y_{2} - y_{0} y_{1}^{2} + y_{0} y_{2}^{2} + y_{1}^{2} y_{2} - y_{1} y_{2}^{2}}{2 \left(x_{0} y_{1} - x_{0} y_{2} - x_{1} y_{0} + x_{1} y_{2} + x_{2} y_{0} - x_{2} y_{1}\right)}$
$\displaystyle
yc0 =- \frac{x_{0}^{2} x_{1} - x_{0}^{2} x_{2} - x_{0} x_{1}^{2} + x_{0} x_{2}^{2} - x_{0} y_{1}^{2} + x_{0} y_{2}^{2} + x_{1}^{2} x_{2} - x_{1} x_{2}^{2} + x_{1} y_{0}^{2} - x_{1} y_{2}^{2} - x_{2} y_{0}^{2} + x_{2} y_{1}^{2}}{2 \left(x_{0} y_{1} - x_{0} y_{2} - x_{1} y_{0} + x_{1} y_{2} + x_{2} y_{0} - x_{2} y_{1}\right)}$
$\displaystyle
r0 =- \frac{\sqrt{\left(x_{0}^{2} - 2 x_{0} x_{1} + x_{1}^{2} + y_{0}^{2} - 2 y_{0} y_{1} + y_{1}^{2}\right) \left(x_{0}^{2} - 2 x_{0} x_{2} + x_{2}^{2} + y_{0}^{2} - 2 y_{0} y_{2} + y_{2}^{2}\right) \left(x_{1}^{2} - 2 x_{1} x_{2} + x_{2}^{2} + y_{1}^{2} - 2 y_{1} y_{2} + y_{2}^{2}\right)}}{2 \left(x_{0} y_{1} - x_{0} y_{2} - x_{1} y_{0} + x_{1} y_{2} + x_{2} y_{0} - x_{2} y_{1}\right)}$
$\displaystyle
xc1 =\frac{x_{0}^{2} y_{1} - x_{0}^{2} y_{2} - x_{1}^{2} y_{0} + x_{1}^{2} y_{2} + x_{2}^{2} y_{0} - x_{2}^{2} y_{1} + y_{0}^{2} y_{1} - y_{0}^{2} y_{2} - y_{0} y_{1}^{2} + y_{0} y_{2}^{2} + y_{1}^{2} y_{2} - y_{1} y_{2}^{2}}{2 \left(x_{0} y_{1} - x_{0} y_{2} - x_{1} y_{0} + x_{1} y_{2} + x_{2} y_{0} - x_{2} y_{1}\right)}$
$\displaystyle
yc1 =- \frac{x_{0}^{2} x_{1} - x_{0}^{2} x_{2} - x_{0} x_{1}^{2} + x_{0} x_{2}^{2} - x_{0} y_{1}^{2} + x_{0} y_{2}^{2} + x_{1}^{2} x_{2} - x_{1} x_{2}^{2} + x_{1} y_{0}^{2} - x_{1} y_{2}^{2} - x_{2} y_{0}^{2} + x_{2} y_{1}^{2}}{2 \left(x_{0} y_{1} - x_{0} y_{2} - x_{1} y_{0} + x_{1} y_{2} + x_{2} y_{0} - x_{2} y_{1}\right)}$
$\displaystyle
r1 =\frac{\sqrt{\left(x_{0}^{2} - 2 x_{0} x_{1} + x_{1}^{2} + y_{0}^{2} - 2 y_{0} y_{1} + y_{1}^{2}\right) \left(x_{0}^{2} - 2 x_{0} x_{2} + x_{2}^{2} + y_{0}^{2} - 2 y_{0} y_{2} + y_{2}^{2}\right) \left(x_{1}^{2} - 2 x_{1} x_{2} + x_{2}^{2} + y_{1}^{2} - 2 y_{1} y_{2} + y_{2}^{2}\right)}}{2 \left(x_{0} y_{1} - x_{0} y_{2} - x_{1} y_{0} + x_{1} y_{2} + x_{2} y_{0} - x_{2} y_{1}\right)}$
Enter the location of the 3 points that define the circle¶
In [4]:
x0=0
y0=0
x1=0
y1=2
x2=3
y2=1
Only the solution with a positive radius is valid. Copy the solution from above.¶
In [5]:
xc0 = (x0**2*y1 - x0**2*y2 - x1**2*y0 + x1**2*y2 + x2**2*y0 - x2**2*y1
+ y0**2*y1 - y0**2*y2 - y0*y1**2 + y0*y2**2 + y1**2*y2 - y1*y2**2) \
/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))
yc0 = (-x0**2*x1 + x0**2*x2 + x0*x1**2 - x0*x2**2 + x0*y1**2 - x0*y2**2
- x1**2*x2 + x1*x2**2 - x1*y0**2 + x1*y2**2 + x2*y0**2 - x2* y1**2) \
/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))
r0 = sqrt((x0**2 - 2*x0*x1 + x1**2 + y0**2 - 2*y0*y1 + y1**2)
*(x0**2 - 2*x0*x2 + x2**2 + y0**2 - 2*y0*y2 + y2**2)
*(x1**2 - 2*x1*x2 + x2**2 + y1**2 - 2*y1*y2 + y2**2)) \
/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))
xc1 = (x0**2*y1 - x0**2*y2 - x1**2*y0 + x1**2*y2 + x2**2*y0 - x2**2*y1
+ y0**2*y1 - y0**2*y2 - y0*y1**2 + y0*y2**2 + y1**2*y2 - y1*y2**2) \
/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))
yc1 = (-x0**2*x1 + x0**2*x2 + x0*x1**2 - x0*x2**2 + x0*y1**2 - x0*y2**2
- x1**2*x2 + x1*x2**2 - x1*y0**2 + x1*y2**2 + x2*y0**2 - x2*y1**2) \
/(2*(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1))
r1 = -sqrt((x0**2 - 2*x0*x1 + x1**2 + y0**2 - 2*y0*y1 + y1**2)
*(x0**2 - 2*x0*x2 + x2**2 + y0**2 - 2*y0*y2 + y2**2)
*(x1**2 - 2*x1*x2 + x2**2 + y1**2 - 2*y1*y2 + y2**2)) \
/(2*x0*y1 - 2*x0*y2 - 2*x1*y0 + 2*x1*y2 + 2*x2*y0 - 2*x2*y1)
if float(r0) >= 0:
x = xc0
y = yc0
r = float(r0)
else:
x = xc1
y = yc1
r = float(r1)
print(f'xc={x:6.3f} yc={y:6.3f} r={r:6.3f}')
xc= 1.333 yc= 1.000 r= 1.667
Draw the circle, the three points and the center¶
In [6]:
import numpy as np
import matplotlib.pyplot as plt
#matplotlib inline
angle = np.linspace(0,2*np.pi,100)
xs = r*np.cos(angle)+x
ys = r*np.sin(angle)+y
fig, ax = plt.subplots()
ax.set_aspect(1)
plt.grid(linestyle=':')
ax.plot(xs,ys)
ax.plot([x, x0, x1, x2],[y, y0, y1, y2], 'r+')
plt.title('Circle')
plt.show()
In [ ]: