一個程序教你花式示愛 2——99顆閃耀的真愛鑽石

希望大家使用這個程序時回覆一句:願南宮大大賜予我幸福(滑稽)。和我一樣的恩愛狗,我會賜予你們幸福,單身狗的話我會賜予你一個女朋友(當然是虛擬的,滑稽)。當然咯,函數生成的圖片我纔不會給你們看呢,這是我和我女票的。

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()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章