網(wǎng)站首頁 編程語言 正文
正二十面體的頂點(diǎn)
正20面體的12個(gè)頂點(diǎn)剛好可以分為三組,每一組都是一個(gè)符合黃金分割比例的長(zhǎng)方形,而且這三個(gè)長(zhǎng)方形是互相正交的。
所以,想繪制一個(gè)正二十面體是比較容易的
import numpy as np from itertools import product G = (np.sqrt(5)-1)/2 def getVertex(): pt2 = [(a,b) for a,b in product([1,-1], [G, -G])] pts = [(a,b,0) for a,b in pt2] pts += [(0,a,b) for a,b in pt2] pts += [(b,0,a) for a,b in pt2] return np.array(pts) xs, ys zs = getVertex().T ax = plt.subplot(projection='3d') ax.scatter(xs, ys, zs) plt.show()
得到頂點(diǎn)
繪制棱
接下來將這些頂點(diǎn)連接成線,由于總共只有12個(gè)頂點(diǎn),所以兩兩相連,也不至于導(dǎo)致運(yùn)算量爆炸。另一方面,正二十面體的邊長(zhǎng)是相同的,而這些相同的邊連接的也必然是最近的點(diǎn),所以接下來只需建立頂點(diǎn)之間的距離矩陣,然后將距離最短的線抽出來即可。
def getDisMat(pts): N = len(pts) dMat = np.ones([N,N])*np.inf for i in range(N): for j in range(i): dMat[i,j] = np.linalg.norm([pts[i]-pts[j]]) return dMat pts = getVertex() dMat = getDisMat(pts) # 由于存在舍入誤差,所以得到的邊的數(shù)值可能不唯一 ix, jx = np.where((dMat-np.min(dMat))<0.01)
接下來,繪制正二十面體的棱
edges = [pts[[i,j]] for i,j in zip(ix, jx)] ax = plt.subplot(projection='3d') for pt in edges: ax.plot(*pt.T) plt.show()
效果如圖所示
繪制面
當(dāng)然,只是有棱還顯得不太好看,接下來要對(duì)正二十面體的面進(jìn)行上色。由于三條棱構(gòu)成一個(gè)面,所以只需得到所有三條棱的組合,然后判定這三條棱是否可以組成一個(gè)三角形,就可以獲取所有的三角面。當(dāng)然,這一切的前提是,正二十面體只有30個(gè)棱,即使遍歷多次,也無非27k的計(jì)算量,是完全沒問題的。
def isFace(e1, e2, e3): pts = np.vstack([e1, e2, e3]) pts = np.unique(pts, axis=0) return len(pts)==3 from itertools import combinations faces = [es for es in combinations(edges, 3) if isFace(*es)]
接下來繪制一下
ax = plt.subplot(projection='3d') for f in faces: pt = np.unique(np.vstack(f), axis=0) try: ax.plot_trisurf(*pt.T) except: pass plt.show()
如圖所示
由于plot_trisurf
的畫圖邏輯是,先繪制xy坐標(biāo)系上的三角形,然后再以此為三角形建立z軸坐標(biāo)。所以這會(huì)導(dǎo)致一個(gè)Bug,即所繪制的三角面不能垂直于xy坐標(biāo)系,為了讓正二十面體被完整地繪制出來,可以對(duì)其繞著x和y軸旋轉(zhuǎn)一下,當(dāng)然首先要建立一個(gè)旋轉(zhuǎn)矩陣。三維空間中的旋轉(zhuǎn)矩陣如下表所示。詳情可參考博客:Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣的作用
寫成代碼為
# 將角度轉(zhuǎn)弧度后再求余弦 cos = lambda th : np.cos(np.deg2rad(th)) sin = lambda th : np.sin(np.deg2rad(th)) # 即 Rx(th) => Matrix Rx = lambda th : np.array([ [1, 0, 0], [0, cos(th), -sin(th)], [0, sin(th), cos(th)]]) Ry = lambda th : np.array([ [cos(th), 0, sin(th)], [0 , 1, 0], [-sin(th), 0, cos(th)] ])
然后繪圖函數(shù)
ax = plt.subplot(projection='3d') for f in faces: pt = np.unique(np.vstack(f), axis=0) pt = Rx(1)@Ry(1)@pt.T ax.plot_trisurf(*pt) for pt in edges: pt = Rx(1)@Ry(1)@pt.T ax.plot(*pt, lw=2, color='blue') plt.show()
效果如下
總結(jié)
原文鏈接:https://blog.csdn.net/m0_37816922/article/details/128258800
相關(guān)推薦
- 2022-07-29 Linux文件系統(tǒng)介紹_linux shell
- 2021-12-01 docker網(wǎng)絡(luò)配置過程詳解介紹_Android
- 2023-01-12 React?useCallback鉤子的作用方法demo_React
- 2023-02-06 C語言實(shí)現(xiàn)文件讀寫功能流程_C 語言
- 2022-09-08 Go語言怎么使用變長(zhǎng)參數(shù)函數(shù)_Golang
- 2022-03-13 C語言實(shí)現(xiàn)求最大公約數(shù)的三種方法_C 語言
- 2024-01-15 idea 折疊代碼塊技巧 關(guān)于<editor-fold>
- 2022-05-21 服務(wù)發(fā)現(xiàn)與負(fù)載均衡機(jī)制Service實(shí)例創(chuàng)建_服務(wù)器其它
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支