希望大家使用这个程序时回复一句:愿南宫大大赐予我幸福(滑稽)。和我一样的恩爱狗,我会赐予你们幸福,单身狗的话我会赐予你一个女朋友(当然是虚拟的,滑稽)。当然咯,函数生成的图片我才不会给你们看呢,这是我和我女票的。
PS:程序如果运行出错,多半属于没有及时更新matplotlib函数库,请使用如下语句在cmd控制台安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random
#99个轮廓点选取
def outline_point():
ox=[]
oy=[]
random_cache=[]
random_num=[]
rox=[]
roy=[]
c1=0
c2=0
for i in range(-100,101,1):
x1=i
y1=0.1*(6*abs(x1)+(36*x1**2-4*5*(5*x1**2-32000))**0.5)
if abs(x1-c1)>=4 or abs(y1-c2)>=5:
ox.append(x1)
oy.append(y1)
c1=x1
c2=y1
for i in range(100,-101,-1):
x2=i
y2=0.1*(6*abs(x2)-(36*x2**2-4*5*(5*x2**2-32000))**0.5)
if abs(x2-c1)>=4 or abs(y2-c2)>=5:
ox.append(x2)
oy.append(y2)
c1=x2
c2=y2
for i in range(0,len(ox)):
random_cache.append(i)
random_num=random.sample(random_cache,99)
for i in random_num:
rox.append(ox[i])
roy.append(oy[i])
return rox,roy
#定义心形线母本
def hswl(ii):
xdata, ydata = [], []
for i in range(0,183,5):
xi=(182-i)/100
xdata.append(0.01*i-1.82)
yi=(xi**(2/3))+(0.9*(3.3-xi**2)**0.5)*np.cos(ii*(np.pi)*xi)
if type(yi)=='complex':
yi=np.around(abs(yi),decimals=4)
yi=np.around(yi, decimals=3)
ydata.append(yi)
for i in range(0,182,5):
xi=i/100
xdata.append(xi)
yi=(xi**(2/3))+(0.9*(3.3-xi**2)**0.5)*np.cos(ii*(np.pi)*xi)
if type(yi)=='complex':
yi=np.around(abs(yi),decimals=4)
yi=np.around(yi, decimals=3)
ydata.append(yi)
return xdata,ydata
#心形线移动子类
def mobile_hswl(xdata,ydata,i_rox,i_roy):
subhswl_x=np.array(xdata)+i_rox
subhswl_y=np.array(ydata)+i_roy
return subhswl_x,subhswl_y
def init():
ax.set_xlim(-110,111)
ax.set_ylim(-110,111)
return ln,
def update(ii):
subhswl_x=np.array([])
subhswl_y=np.array([])
while 1:
global number
i_rox,i_roy=rox[number],roy[number]
xdata,ydata=hswl(ii)
n_subhswl_x,n_subhswl_y=mobile_hswl(xdata,ydata,i_rox,i_roy)
subhswl_x=np.append(subhswl_x,n_subhswl_x)
subhswl_y=np.append(subhswl_y,n_subhswl_y)
number=number+1
if number>=99:
break
number=0
ln.set_data(subhswl_x,subhswl_y)
return ln,
fig, ax = plt.subplots()
ln, = ax.plot([], [], '-',color='red', lw=0.1)
number=0
rox,roy=outline_point()
ani = FuncAnimation(fig, update, np.linspace(0, 13.14, 100),init_func=init, interval=100)
ani.save('love.gif', writer='imagemagick', fps=10)
plt.show()