plt 画bezier曲线

plt 画bezier曲线

参考博客:https://www.bbsmax.com/A/D854Y3BWdE/

import numpy as np
import matplotlib.pyplot as plt
import copy

def get_bezier_points(raw_points):
    raw_points = np.array(raw_points, dtype=np.float)
    bezier_points = []
    fenshu = 100

    num = len(raw_points)

    for epoch in range(fenshu):
        raw_points_copy = copy.deepcopy(raw_points)
        weight = epoch/fenshu
        for i in range(num-1):
            for j in range(num-1-i):
                raw_points_copy[j][0] = raw_points_copy[j][0] * (1 - weight) + raw_points_copy[j+1][0] * weight
                raw_points_copy[j][1] = raw_points_copy[j][1] * (1 - weight) + raw_points_copy[j+1][1] * weight
        bezier_points.append(raw_points_copy[0])
    return np.array(bezier_points, dtype=np.float)

def xy2points(x, y):
    points = []
    for i in range(len(x)):
        points.append([x[i], y[i]])
    return np.array(points, dtype=np.float)

def paint_points_line(points):
    plt.plot(points[:,0], points[:,1])

if __name__ == '__main__':
    x = [0, 2, 5, 10, 15, 20]
    y = [0, 6, 10, 0, 5, 5]
    points = xy2points(x, y)
    paint_points_line(points)
    bezier_points = get_bezier_points(points)
    paint_points_line(bezier_points)
    plt.show()
文章目录