本文由CSDN点云侠原创,爬虫网站请自重。
已知不共线的三个点,设其坐标为 ( x 1 , y 1 ) (x_1,y_1) (x1,y1)、 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)、 ( x 3 , y 3 ) (x_3,y_3) (x3,y3),圆的一般方程为:
A ( x 2 + y 2 ) + B x + C y + D = 0 (1) A(x^2+y^2)+Bx+Cy+D=0\tag{1} A(x2+y2)+Bx+Cy+D=0(1)
将式(1)变形可得圆的标准方程为:
( x + B 2 A ) 2 + ( y + C 2 A ) 2 = B 2 + C 2 − 4 A D 4 A 2 (2) (x+\frac{B}{2A})^2+(y+\frac{C}{2A})^2=\frac{B^2+C^2-4AD}{4A^2}\tag{2} (x+2AB)2+(y+2AC)2=4A2B2+C2−4AD(2)
将三个已知点代入式(1),可得关于 A , B , C , D A,B,C,D A,B,C,D的齐次线性方程组:
[ 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 ] ⋅ [ A B C D ] = [ 0 0 0 0 ] (3) \left[ \begin{matrix} 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{matrix} \right]\cdot \left[ \begin{matrix} A\\ B \\ C \\ D \\ \end{matrix} \right]= \left[ \begin{matrix} 0\\ 0 \\ 0 \\ 0 \\ \end{matrix} \right]\tag{3} x2+y2x12+y12x22+y22x32+y32xx1x2x3yy1y2y31111 ⋅ ABCD = 0000 (3)
在三点不共线的前提下,该齐次线性方程组有非零解,其等价于系数矩阵不满秩,即有:
∣ 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 ∣ = 0 (4) \left| \begin{matrix} 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{matrix} \right|=0\tag{4} x2+y2x12+y12x22+y22x32+y32xx1x2x3yy1y2y31111 =0(4)
将式(4)展开,并与式(1)对比可得四个系数:
A = + ∣ x 1 y 1 1 x 2 y 2 1 x 3 y 3 1 ∣ (5) A=+\left| \begin{matrix} x_1 & y_1&1 \\ x_2 & y_2&1 \\ x_3 & y_3&1 \\ \end{matrix} \right|\tag{5} A=+ x1x2x3y1y2y3111 (5)
B = − ∣ x 1 2 + y 1 2 y 1 1 x 2 2 + y 2 2 y 2 1 x 3 2 + y 3 2 y 3 1 ∣ (6) B=-\left| \begin{matrix} x_1^2+y_1^2& y_1&1 \\ x_2^2+y_2^2 & y_2&1 \\ x_3^2+y_3^2 & y_3&1 \\ \end{matrix} \right|\tag{6} B=− x12+y12x22+y22x32+y32y1y2y3111 (6)
C = + ∣ x 1 2 + y 1 2 x 1 1 x 2 2 + y 2 2 x 2 1 x 3 2 + y 3 2 x 3 1 ∣ (7) C=+\left| \begin{matrix} x_1^2+y_1^2 & x_1 &1 \\ x_2^2+y_2^2 & x_2 &1 \\ x_3^2+y_3^2 & x_3 &1 \\ \end{matrix} \right|\tag{7} C=+ x12+y12x22+y22x32+y32x1x2x3111 (7)
D = − ∣ x 1 2 + y 1 2 x 1 y 1 x 2 2 + y 2 2 x 2 y 2 x 3 2 + y 3 2 x 3 y 3 ∣ (8) D=-\left| \begin{matrix} x_1^2+y_1^2 & x_1 & y_1 \\ x_2^2+y_2^2 & x_2 & y_2 \\ x_3^2+y_3^2 & x_3 & y_3 \\ \end{matrix} \right|\tag{8} D=− x12+y12x22+y22x32+y32x1x2x3y1y2y3 (8)
由式(2)可得圆心坐标 ( x c , y c ) (x_c,y_c) (xc,yc)和半径 r r r,即
{ x c = − B 2 A y c = − C 2 A r = B 2 + C 2 − 4 A D 4 A 2 (9) \begin{cases} x_c=-\frac{B}{2A}\\ y_c=-\frac{C}{2A}\\ r=\sqrt\frac{B^2+C^2-4AD}{4A^2} \end{cases} \tag{9} ⎩ ⎨ ⎧xc=−2AByc=−2ACr=4A2B2+C2−4AD(9)
import numpy as np
def three_points_fit_circle(points):
P1 = points[0]
P2 = points[1]
P3 = points[2]
# 共线检查
temp01 = P1 - P2
temp02 = P3 - P2
temp03 = np.cross(temp01, temp02)
temp = (temp03 @ temp03) / (temp01 @ temp01) / (temp02 @ temp02)
if temp < 10 ** -6:
print('\t三点共线, 无法确定圆')
return None
A = np.ones((3, 3))
A[0, 0] = P1[0]
A[0, 1] = P1[1]
A[1, 0] = P2[0]
A[1, 1] = P2[1]
A[2, 0] = P3[0]
A[2, 1] = P3[1]
B = np.ones((3, 3))
B[0, 0] = P1[0] ** 2 + P1[1] ** 2
B[0, 1] = P1[1]
B[1, 0] = P2[0] ** 2 + P2[1] ** 2
B[1, 1] = P2[1]
B[2, 0] = P2[0] ** 2 + P2[1] ** 2
B[2, 1] = P3[1]
C = np.ones((3, 3))
C[0, 0] = P1[0] ** 2 + P1[1] ** 2
C[0, 1] = P1[0]
C[1, 0] = P2[0] ** 2 + P2[1] ** 2
C[1, 1] = P2[0]
C[2, 0] = P2[0] ** 2 + P2[1] ** 2
C[2, 1] = P3[0]
D = np.ones((3, 3))
D[0, 0] = P1[0] ** 2 + P1[1] ** 2
D[0, 1] = P1[0]
D[0, 2] = P1[1]
D[1, 0] = P2[0] ** 2 + P2[1] ** 2
D[1, 1] = P2[0]
D[1, 2] = P2[1]
D[2, 0] = P2[0] ** 2 + P2[1] ** 2
D[2, 1] = P3[0]
D[2, 2] = P3[1]
A = +np.linalg.det(A)
B = -np.linalg.det(B)
C = +np.linalg.det(C)
D = -np.linalg.det(D)
Xc = -B / (2 * A)
Yc = -C / (2 * A)
r = np.sqrt((B * B + C * C - 4 * A * D) / (4 * A * A))
return Xc, Yc, r
圆心x坐标:14.558850728542366
圆心y坐标:11.80858513779518
圆半径:22.163390629231692