Python編程利用科赫曲線實(shí)現(xiàn)三維飄雪效果示例過程
隨機(jī)雪花
如果隨機(jī)生成一些點(diǎn),然后為每個(gè)點(diǎn)繪制一些枝杈,則可以畫出類似蒲公英這種結(jié)構(gòu),只是看上去不太好看而已
import numpy as np import matplotlib.pyplot as plt from numpy.random import rand,randint M,N = 10,100 x = rand(N)*100 y = rand(N)*100 plt.scatter(x,y,marker='.') for i in range(N): M = randint(5,15) r = rand()*3 for j in range(M): theta = np.pi*2/M*j plt.plot([x[i],x[i]+r*np.cos(theta)], [y[i],y[i]+r*np.sin(theta)]) plt.axis('off') plt.show()
當(dāng)然也可以畫成三維圖,果然還是很丑。
科赫雪花
所以,既然想飄雪,那就首先得有雪花。科赫曲線因?yàn)槭窒裱┗?,所以又叫雪花曲線,生成方式十分簡單,總共分兩步
畫一個(gè)正三角形將正三角形的每個(gè)邊三等分,然后以中間的那份為邊,再畫出個(gè)三角形。重復(fù)第二步。
那么難點(diǎn)無非是三等分后如何新畫一個(gè)三角形,更進(jìn)一步,新三角形的那個(gè)新頂點(diǎn)在哪里?
從而得到
代碼如下
import numpy as np import matplotlib.pyplot as plt from numpy.random import rand,randint # n>=1,生成科赫雪花的方法 def Koch(L,n=1): if n<1 : return newL = [] #(x,y)的列表 for i in range(len(L)-1): delta = (L[i+1]-L[i])/3 x = (L[i][0]+L[i+1][0])/2-np.sqrt(3)/2*delta[1] y = (L[i][1]+L[i+1][1])/2+np.sqrt(3)/2*delta[0] newL += [L[i],L[i]+delta,np.array([x,y]),L[i]+delta*2] newL.append(L[-1]) return newL if n==1 else Koch(newL,n-1) L0 = [ np.array([0,0]), np.array([0.5,np.sqrt(3)/2]), np.array([1,0]), np.array([0,0]) ] def plot_Koch(L): for i in range(len(L)-1): p = np.array(L[i:i+2]).T plt.plot(p[0],p[1],color='lightblue',lw = 1) plt.xlim(-0.25,1.25) plt.ylim(-0.5,1.25) plt.show() if __name__ == "__main__": plot_Koch(Koch(L0,3))
如果多畫一些,那么就是這樣
生成方法為
#n為雪花數(shù)量,low,high為最低和最高koch雪花階數(shù) def RandKoch(n,low,high): randKochs = [] rMax = np.sqrt(1/n) for _ in range(n): cx,cy,t0 = rand(3) r = rand()*rMax L0 = [np.array([np.cos(t),np.sin(t)])*r+[cx,cy] for t in (t0-np.arange(4)*np.pi*2/3)] randKochs.append(Koch(L0,randint(low,high))) return randKochs def plot_Kochs(Ls): for L in Ls: for i in range(len(L)-1): p = np.array(L[i:i+2]).T plt.plot(p[0],p[1],color='lightblue',lw = 1) plt.xlim(-0.2,1.2) plt.ylim(-0.2,1.2) plt.show()
當(dāng)然,如果用plt.fill(x,y)
,則可畫出實(shí)心的雪花
def plot_Kochs(Ls): for L in Ls: x,y = np.array(L).T plt.fill(x, y, color = 'lightblue') plt.xlim(-0.2,1.2) plt.ylim(-0.2,1.2) plt.show()
三維
我們想要的是那種飄雪的感覺,所以至少得有個(gè)3D的圖,這很簡單,只要加個(gè)三維的坐標(biāo)就可以了。
#導(dǎo)入PolyCollection繪制實(shí)心的3D圖形 from mpl_toolkits.mplot3d.art3d import PolyCollection def plot_Kochs_fill3d(Ls): fig = plt.figure() ax = fig.gca(projection='3d') p3d = PolyCollection(Ls,facecolors=np.repeat('lightblue',len(Ls)),alpha=0.7) ax.add_collection3d(p3d,zs=rand(len(Ls)),zdir='y') ax.set_xlim3d(0,1) ax.set_ylim3d(0,1) ax.set_zlim3d(0,1) plt.show()
以上就是Python編程實(shí)現(xiàn)三維飄雪效果示例過程的詳細(xì)內(nèi)容,更多關(guān)于Python編程實(shí)現(xiàn)三維飄雪的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
好的Python培訓(xùn)機(jī)構(gòu)應(yīng)該具備哪些條件
python是現(xiàn)在開發(fā)的熱潮,大家應(yīng)該如何學(xué)習(xí)呢?許多人選擇自學(xué),還有人會選擇去培訓(xùn)結(jié)構(gòu)學(xué)習(xí),那么好的培訓(xùn)機(jī)構(gòu)的標(biāo)準(zhǔn)是什么樣的呢?下面跟隨腳本之家小編一起通過本文學(xué)習(xí)吧2018-05-05Python動態(tài)可視化模塊Pynimate初體驗(yàn)
Pynimate是python第三方用于動態(tài)可視化的數(shù)據(jù)模塊,是一位專攻?Python?語言的程序員開發(fā)的安裝包。本文將通過幾個(gè)簡單的示例,講解一下Pynimate的使用方法,需要的可以參考一下2023-02-02pytorch的Backward過程用時(shí)太長問題及解決
這篇文章主要介紹了pytorch的Backward過程用時(shí)太長問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Python列表(list)、字典(dict)、字符串(string)基本操作小結(jié)
這篇文章主要介紹了Python列表(list)、字典(dict)、字符串(string)基本操作小結(jié),本文總結(jié)了最基本最常用的一些操作,需要的朋友可以參考下2014-11-11Python統(tǒng)計(jì)字符串中英文字母、空格、數(shù)字和其它字符的個(gè)數(shù)
這篇文章主要給大家介紹了關(guān)于Python統(tǒng)計(jì)字符串中英文字母、空格、數(shù)字和其它字符的個(gè)數(shù)的相關(guān)資料,本文實(shí)例講述了python統(tǒng)計(jì)字符串中指定字符出現(xiàn)次數(shù)的方法,需要的朋友可以參考下2023-06-06在pytorch中動態(tài)調(diào)整優(yōu)化器的學(xué)習(xí)率方式
這篇文章主要介紹了在pytorch中動態(tài)調(diào)整優(yōu)化器的學(xué)習(xí)率方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06