生活随笔
收集整理的這篇文章主要介紹了
Python计算空间二面角
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定義代表三維笛卡爾坐標系上某個點的Point 類(包括x 、y 、z 三個屬性),為該類定義一個方法,可接收second 、third 、forth 三個參數,用于計算當前點、second 、third 組成的面與second 、third 、forth組成的面之間的夾角。
A,B,C,D分別對應于提示point1,point2,point3,point4,線段BC是面ABC與面DBC的公共邊,計算倆面空間夾角: cos (夾角) = (X·Y)/|X||Y|,其中X=AB×BC,Y=BC×CD,X·Y代表X與Y的點積,AB×BC代表AB與BC的叉乘
import math
class Point():def __init__(self
,x
,y
,z
):self
.x
= xself
.y
= yself
.z
= z
def distance(self
):xx
= (self
.x
) ** 2yy
= (self
.y
) ** 2zz
= (self
.z
) ** 2return (xx
+ yy
+ zz
) ** (0.5)def vector(self
,other
):xx
= (self
.x
- other
.x
)yy
= (self
.y
- other
.y
)zz
= (self
.z
- other
.z
)return Point
(xx
,yy
,zz
)def cross(self
,other
):return Point
(self
.x
*other
.z
-self
.z
*other
.y
, self
.z
*other
.x
-self
.x
*other
.z
, self
.x
*other
.y
-self
.y
*other
.x
)def dot(self
,other
):return (self
.x
*other
.x
+ self
.y
*other
.y
+ self
.z
*other
.z
)def angle(self
,second
,third
,forth
):vertor11
= point1
.vector
(point2
)vertor12
= point1
.vector
(point3
)vertor21
= point4
.vector
(point2
)vertor22
= point4
.vector
(point3
)vertor1
= vertor11
.cross
(vertor12
)vertor2
= vertor21
.cross
(vertor22
)return math
.acos
( (vertor1
.dot
(vertor2
)) / ((vertor1
.distance
())*(vertor2
.distance
())) )point1
= Point
(0,1,0)
point2
= Point
(1,0,0)
point3
= Point
(0,0,0)
point4
= Point
(0,0,1)angels
= point1
.angle
(point2
,point3
,point4
)
print(math
.degrees
(angels
))
總結
以上是生活随笔為你收集整理的Python计算空间二面角的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。