B样条曲线绘制
1.B样条曲线原理
2. 固定基函数进行测试
def B_curve_uni(V,n):
U = np.linspace(0,1,100)
X = V[0]
Y = V[1]
n = 3
coef_matrix = 1/6*np.matrix([[1,4,1,0],[-3,0,3,0],[3,-6,3,0],[-1,3,-3,1]])
if len(X) < n+1:
return '型值点个数不够'
else:
x = []
y = []
fig = plt.figure()
plt.plot(X,Y,marker='o',markerfacecolor='white')
for i in range(len(X)-n):
for u in U:
x.append((np.matrix([1,u,np.power(u,2),np.power(u,3)])*coef_matrix*(np.matrix(X[i:i+n+1]).T))[0,0])
y.append((np.matrix([1,u,np.power(u,2),np.power(u,3)])*coef_matrix*(np.matrix(Y[i:i+n+1]).T))[0,0])
plt.plot(x,y)
plt.plot([x[0],x[100],x[200],x[-1]],[y[0],y[100],y[200],y[-1]],'o')
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.show()
V = [[0,0,2,3,2,1],[1,3,3,2,1,1]]
B_curve_uni(V,3)
3.B样条曲线绘制(基函数随曲线次数改变)
def N(i,k,T,t):
if k == 0:
if t<T[i] or t>T[i+1]:
return 0
else:
return 1
else:
result = (t-T[i])/(T[i+k]-T[i])*N(i,k-1,T,t) + (T[i+k+1]-t)/(T[i+k+1]-T[i+1])*N(i+1,k-1,T,t)
return result
def main(n,V,V_num):
T = np.linspace(0,1,n+V_num+1)
t_x = np.linspace(0,1,160)
X = V[0]
Y = V[1]
x = []
y = []
for i in range(V_num-n):
result = pd.DataFrame(t_x,columns=['t'])
for j in range(n+1):
result1 = []
for t in t_x:
result1.append(N(i+j,n,T,t))
result['N_{0}{1}'.format(i+j,n)] = result1
Ni_matrix = np.matrix(result[result['t'].apply(lambda x: x>=T[i+n] and x<=T[i+n+1])].iloc[:,1:])
x = x+((Ni_matrix*np.matrix(X[i:i+n+1]).T).T).tolist()[0]
y = y+((Ni_matrix*np.matrix(Y[i:i+n+1]).T).T).tolist()[0]
fig = plt.figure()
plt.plot(X,Y,marker='o',markerfacecolor='white')
plt.plot(x,y)
plt.show()
if __name__ == '__main__':
n = int(input('请输入曲线的次数:'))
V_num = int(input('请输入控制顶点个数:'))
V = [[1,1,1.5,3,3.8,3.2,5,8,8],[0,0,2.6,3,2.2,-0.3,-0.8,2,2]]
main(n,V,V_num)