1.最小二乘法求圆度误差
import numpy as np
import math
import geometry_function
def roundness_lsm(x1,y1):
e=0.00000000001
y1max_index=y1.index(max(y1))
x1min_index=x1.index(min(x1))
y1min_index=y1.index(min(y1))
pp=[[x1[y1max_index],y1[y1max_index]],[x1[x1min_index],y1[x1min_index]],[x1[y1min_index],y1[y1min_index]]]
ce=geometry_function.circle_three_point(pp[0], pp[1], pp[2])
n1=len(x1)
a1 = ce[0]
b1 = ce[1]
R1 = ce[2]
for j in range(10):
#print(a1, b1, R1)
xk=np.array([[a1],[b1],[R1]])
jk =np.zeros((n1, 3))
Fk = np.zeros((n1, 1))
i = 0
while i < n1:
xii=x1[i]
yii=y1[i]
fida1=(a1-xii)/math.sqrt((xii-a1)**2+(yii-b1)**2)
fidb1=(b1-yii)/math.sqrt((xii-a1)**2+(yii-b1)**2)
fidR1=-1
jk[i][0]=fida1
jk[i][1] =fidb1
jk[i][2] =fidR1
fki = math.sqrt((xii - a1)**2 + (yii - b1)**2) - R1
Fk[i]=fki
i += 1
jkt=np.transpose(jk)
jjk=np.linalg.inv(np.dot(jkt, jk))
dk=-np.dot(np.dot(jjk, jkt),Fk)
xk1=xk+dk
xka=xk1[0].tolist()
xkb=xk1[1].tolist()
xkR1=xk1[2].tolist()
if abs(xka[0]-a1)
- 几何计算
import numpy as np
import math
def circle_three_point(p1, p2, p3):
temp = p2[0] * p2[0] + p2[1] * p2[1]
bc = (p1[0] * p1[0] + p1[1] * p1[1] - temp) / 2
cd = (temp - p3[0] * p3[0] - p3[1] * p3[1]) / 2
det = (p1[0] - p2[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p2[1])
# Center of circle
cx = (bc * (p2[1] - p3[1]) - cd * (p1[1] - p2[1])) / det
cy = ((p1[0] - p2[0]) * cd - (p2[0] - p3[0]) * bc) / det
radius = np.sqrt((cx - p1[0]) ** 2 + (cy - p1[1]) ** 2)
return [cx, cy, radius.tolist()]
def direction(p1, p2, p3):
# calculates the direction value of an ordered triplet of points in the plane
return (p2[0]-p1[0])*(p3[1]-p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0])
def point_in_circle(cir_j,p0):
xc=cir_j[0]
yc = cir_j[1]
rc=cir_j[2]
rj=math.sqrt((p0[0]-xc)**2+(p0[1]-yc)**2)
if rj>rc:
return 1#点在圆外
elif rj==rc:
return 0.0#点在圆上
else:
return -1#点在圆内
- 绘图
import numpy as np
def plot_circle(x0,y0,r,ax):
t = np.arange(0.0, 2.0*np.pi, 0.01)
xt =(r* np.sin(t)+x0)
yt =(r* np.cos(t)+y0)
ax.plot(xt, yt,'--',color='k',linewidth =1,label="Line")
def plot_point(x1, y1,ax):
n1=len(x1)
for k in range(n1):
x_point = ax.plot(x1[k], y1[k], marker='o')
x_point[0].set_color('k')
- 主程序
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import roundness_lsm
import drawing_geometry
x1=[-15.4512,-29.3892,-40.4511,-47.553,-49.9998,-47.5535,-40.4504,-29.3905,-15.4508,0.0000,15.4512, 29.3893,40.4508, 47.5520, 49.9996,47.5512,40.451, 29.3894,15.4505, 0.0000]
y1=[47.5540,40.4508,29.3895,15.4509, 0.0000,-15.4511,-29.3889,-40.4526 ,-47.5527 ,-50.0001 ,-47.5538 ,-40.4509 ,-29.3892 ,-15.4506 ,0.0000,15.4503 ,29.3893 ,40.4511 ,47.5517 ,50.0012]
fig, axs = plt.subplots(1, 1)
drawing_geometry.plot_point(x1, y1,axs)
r_lsm=roundness_lsm.roundness_lsm(x1,y1)
drawing_geometry.plot_circle(r_lsm[0],r_lsm[1],r_lsm[2],axs)
plt.show()
你可能感兴趣的:(Python,python,最小二乘法,numpy,圆度误差)