贝塞尔曲线(基础)

该代码示例使用Python的numpy和scipy库来计算并绘制贝塞尔曲线。用户输入控制点坐标,程序通过伯恩斯坦多项式计算曲线并在matplotlib图表中展示。同时,还展示了每个控制点对应的伯恩斯坦多项式的图形。
摘要由CSDN通过智能技术生成

贝塞尔曲线的一般表达式(含高阶)

其中

Pi即为给定点的坐标,我们可以分为Px与Py单独计算

import numpy as np
import scipy.special
import matplotlib.pyplot as plt
Px1 = input("")
Px = [int(n) for n in Px1.split()]
Py1 = input("")
Py = [int(n) for n in Py1.split()]
n = len(Px)-1
Coe = []
t = np.linspace(0,1,1000) #步长越长图像越光滑
for i in range(n + 1):
    Coe.append(scipy.special.binom(n, i)) #伯恩斯坦多项式系数

def Ber(t,i,n): #伯恩斯坦多项式
    B = Coe[i] * (t ** i) * (1 - t) ** (n -i)
    return B

def B_x():
    Bx=0
    for i in range(n+1):
        Bx += Px[i]*Ber(t,i,n)
    return Bx
def B_y():
    By=0
    for i in range(n+1):
        By += Py[i]*Ber(t,i,n)
    return By

plt.plot(Px,Py)
plt.plot(B_x(),B_y())
plt.figure() #绘制Bernstein图像
for i in range(n + 1):
    plt.plot(t,Ber(t,i,n))
plt.show()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值