您好,登錄后才能下訂單哦!
這篇“Python如何實(shí)現(xiàn)繪制3D地球旋轉(zhuǎn)效果”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Python如何實(shí)現(xiàn)繪制3D地球旋轉(zhuǎn)效果”文章吧。
想畫一個(gè)轉(zhuǎn)動(dòng)的地球,那么首先要有一個(gè)球,或者說(shuō)要有一個(gè)球面,用參數(shù)方程可以表示為
x=rcos?cosθ
y=rcos?sinθ
z=rsin?
然后要有一個(gè)地球,或者說(shuō)要有一個(gè)地圖,用來(lái)作為貼圖,映射到球面上。
import numpy as np import matplotlib.pyplot as plt path = "earth2.jpg" img = plt.imread(path) h, w, c = img.shape ys, xs = np.indices([h, w]) th = xs/w*np.pi*2 phi = np.pi/2 - ys/h*np.pi x = np.cos(phi)*np.cos(th) y = np.cos(phi)*np.sin(th) z = np.sin(phi) cs = [tuple(c/255) for c in img.reshape(-1,3)] ax = plt.subplot(projection='3d') ax.scatter(x, y, z, marker='.', c=cs) plt.axis('off') plt.show()
其中scatter
畫的是散點(diǎn)圖,c=cs
為顏色映射參數(shù),所以溫馨提示,選取的地球圖片不宜過(guò)大,否則點(diǎn)太多會(huì)讓電腦爆掉。
最后得到的效果如下
三維空間中的旋轉(zhuǎn)矩陣如下表所示,具體講解可參考這兩篇博客:旋轉(zhuǎn)坐標(biāo)軸????旋轉(zhuǎn)正方體
有了旋轉(zhuǎn)矩陣,接下來(lái)就是讓地球轉(zhuǎn)起來(lái)。
import numpy as np import matplotlib.pyplot as plt from matplotlib import animation cos = lambda th : np.cos(np.deg2rad(th)) sin = lambda th : np.sin(np.deg2rad(th)) Rz = lambda th : np.array([ [cos(th) , -sin(th), 0], [sin(th), cos(th), 0], [0 , 0, 1]]) xyz = np.array([x,y,z]).reshape(3,-1) fig = plt.figure(figsize=(5,5)) ax = fig.add_subplot(projection='3d') ax.grid() lines = ax.scatter(x, y, z, marker='.', c=cs) def animate(n): # 按照xyz順序旋轉(zhuǎn) axis = [2,1,0] shape = xyz.shape lines._offsets3d = Rz(n)@xyz return lines, ani = animation.FuncAnimation(fig, animate, range(0, 360, 2), interval=25, blit=True) #plt.show() ani.save("zyx.gif")
效果如下
以上就是關(guān)于“Python如何實(shí)現(xiàn)繪制3D地球旋轉(zhuǎn)效果”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。