二阶贝塞斯曲线,matplotlib可视

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation


x1=10
y1=80
x2=50
y2=10
x3=90
y3=80
dots_num=100

def two_degree_bc(x1=10, y1=80, x2=50, y2=10, x3=90, y3=80, dots_num=100): #bezier curve
    global xt, yt, x_dots12, x_dots23, y_dots12, y_dots23
    xt = []
    yt = []
    x_dots12 = np.linspace(x1, x2, dots_num)
    y_dots12 = np.linspace(y1, y2, dots_num)
    x_dots23 = np.linspace(x2, x3, dots_num)
    y_dots23 = np.linspace(y2, y3, dots_num)
    for i in range(dots_num):
        x = x_dots12[i] + (x_dots23[i]-x_dots12[i])*i / (dots_num-1)
        y = y_dots12[i] + (y_dots23[i]-y_dots12[i])*i / (dots_num-1)
        xt.append(x)
        yt.append(y)
  
  
def run(i):
    art1.set_data(x_dots12[i], y_dots12[i])
    art2.set_data(x_dots23[i], y_dots23[i])
    art3.set_data([x_dots12[i], x_dots23[i]], [y_dots12[i], y_dots23[i]])
    art4.set_data(xt[i], yt[i])
    return art1,art2,art3,art4


two_degree_bc()
fig, ax = plt.subplots(figsize=(8,8))
ax.set_aspect(1)
plt.xlim([0,100])
plt.ylim([0,100])
ax.plot([x1, x2], [y1, y2], color='#3e82fc')
ax.plot([x2, x3], [y2, y3], color='#3e82fc')
ax.plot(xt,yt,color='orange')
art1, = ax.plot(x_dots12[0], y_dots12[0], color='green', marker='o') #scatter得到的对象不是一个list,是一个object
art2, = ax.plot(x_dots23[0], y_dots23[0], color='green', marker='o') 
art3, = ax.plot([x_dots12[0], x_dots23[0]], [y_dots12[0], y_dots23[0]], color = 'purple') #plot得到的结果是一个list,只包含一个元素,即一个形状object
art4, = ax.plot(xt[0], yt[0], color='red', marker='o')
  
ani = animation.FuncAnimation(
    fig, run, frames=range(100), interval=2, blit=True, save_count=50)
plt.show()

你可能感兴趣的:(二阶贝塞斯曲线,matplotlib可视)