0
点赞
收藏
分享

微信扫一扫

曲线拟合-贝塞尔曲线

腊梅5朵 2022-04-13 阅读 119

如下图所示:

  1. 在二维平面内选三个不同的点(起点A,中间点B,终点C)并依次用线段连接
  2. 在线段AB和BC上按比例分割找到新的起点和中间点:D、E两点,使得AD/AB=BE/BC
  3. 连接DE,并在DE上找到新的起点F点,EC上找到新的中间点G点,使其满足DF/DE=EG/EC
  4. 重复步骤1、2、3,找出符合上述条件的所有点,直到新的起点和终点C重合或者中间点和终点C重合时结束递归

代码如下:

class Bezier:
    def __init__(self,points,baseRatio=1e-5):
        self.curve = []
        self.ratio = baseRatio
        self.Points = points
    def findP(self):  # start,end:矩阵:1*2,1*2。更新p0,p1。ratio:比例:[0,1]
        self.curve.append(self.Points[0])
        self.ratio=self.ratio+1e-2#使raito非固定值,否则递归深度过深,容易栈溢出
        # 寻找pNew
        for ii in range(1, len(self.Points)):
            pNew = [(self.Points[ii][0] - self.Points[ii - 1][0]) * self.ratio + self.Points[ii - 1][0],
                    (self.Points[ii][1] - self.Points[ii - 1][1]) * self.ratio + self.Points[ii - 1][1]]
            # 更新
            self.Points[ii - 1] = pNew
        if np.sqrt(sum(np.power((self.Points[-1][0] - self.Points[0][0],self.Points[-1][1] - self.Points[0][1]), 2)))>1e-5:#新的起点和终点重合时,结束递归
            self.findP()
    def run(self):
        self.findP()
        self.curve.append(self.Points[-1])
if __name__=='__main__':
    Points = [[1, 1], [2, 1], [2, 2], [1, 2]]
    xx = Bezier(p)
    xx.run()
    xx.curve = np.array(xx.curve)
    plt.figure(1)
    plt.plot(xx.curve[:,0],xx.curve[:,1])
    plt.show()
举报

相关推荐

iOS 绘制贝塞尔曲线

0 条评论