前情提要:用Python绘制动态变化的曲线
复杂曲线轨迹演示
有一些复杂的曲线,实则有着非常简单的表达式,比如网上流传甚广的这个
f ( x ) = x 2 3 + 0.9 3.3 − x 2 sin b π x f(x)=x^{frac2 3}+0.9sqrt{3.3-x^2}sin bpi x f(x)=x32+0.93.3−x2 sinbπx
from aniDraw import *
xs = np.arange(-1.81,1.81,0.01)
xSquare = xs**2
k1 = np.power(xSquare,1/3)
k2 = 0.9*np.sqrt(3.3-xSquare)
heart = lambda b : [xs, k1+k2*np.sin(b*np.pi*xs)]
def initFunc(fig):
fig.set_size_inches(16,9)
ax = fig.add_subplot(autoscale_on=False,
xlim=(-5,5),ylim=(-2,3))
ax.axis('off')
return ax
ts = np.arange(0,20,0.5)
txtFunc = lambda b : f'b ={b:.1f}n'
# 由于这里新增了一个颜色,所以需要在drawAni中把颜色项填上
para = [{'ls':'-', 'lw':1, 'flush':True, 'color':'red'}]
an = drawAni([heart], para, txtFunc, ts, initFunc)
an.save(f'test.gif')
效果为



