Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣的作用詳解
先新建一組散點(diǎn)充當(dāng)坐標(biāo)軸
為了比較直觀地展示旋轉(zhuǎn)過(guò)程,這里通過(guò)散點(diǎn)來(lái)新建三個(gè)坐標(biāo)軸,通過(guò)對(duì)這三個(gè)坐標(biāo)軸的轉(zhuǎn)動(dòng),來(lái)直觀地展現(xiàn)轉(zhuǎn)動(dòng)矩陣對(duì)坐標(biāo)變換的影響。
import numpy as np
import matplotlib.pyplot as plt
def setAxis(N, axis=0):
xs = np.arange(N)
ys = np.zeros_like(xs)
zs = np.zeros_like(xs)
if axis==0 : return [xs, ys, zs]
elif axis==1 : return [ys, xs, zs]
else: return [ys, zs, xs]
def drawAxis(X,Y,Z):
ax = plt.subplot(projection='3d')
ax.scatter(*X, c='r')
ax.scatter(*Y, c='g')
ax.scatter(*Z, c='b')
plt.show()
X = setAxis(10, 0)
Y = setAxis(10, 1)
Z = setAxis(10, 2)
drawAxis(X, Y, Z)
效果為

旋轉(zhuǎn)矩陣與初步演示
歐拉角是用來(lái)唯一地確定定點(diǎn)轉(zhuǎn)動(dòng)剛體位置的三個(gè)一組獨(dú)立角參量,由章動(dòng)角θ、進(jìn)動(dòng)角ψ和自轉(zhuǎn)角φ組成,為L(zhǎng).歐拉首先提出,故得名。
為了盡快進(jìn)入演示部分,故對(duì)原理的介紹從略,僅從二維平面上的旋轉(zhuǎn)矩陣出發(fā),做一個(gè)簡(jiǎn)單的推導(dǎo),而三維旋轉(zhuǎn)矩陣,至少在形式上與二維是雷同的。
假設(shè)坐標(biāo)系中有一個(gè)向量 ( x , y ),其模長(zhǎng)為
,角度為
。若將其圍繞坐標(biāo)原點(diǎn)逆時(shí)針旋轉(zhuǎn) θ \theta θ,則其坐標(biāo)變?yōu)?/p>

由于
,則上式可以寫(xiě)為

寫(xiě)成矩陣形式即為

也就是說(shuō),在平面直角坐標(biāo)系上,向量繞原點(diǎn)順時(shí)針旋轉(zhuǎn) θ \theta θ,相當(dāng)于左乘一個(gè)旋轉(zhuǎn)矩陣。
推廣到三維,為了限制 x y xy xy坐標(biāo)平面上的旋轉(zhuǎn),要將其旋轉(zhuǎn)中心從原點(diǎn)擴(kuò)展為繞著 z z z軸旋轉(zhuǎn),從而三維旋轉(zhuǎn)矩陣可推廣為

同理可得到繞三個(gè)軸轉(zhuǎn)動(dòng)的旋轉(zhuǎn)矩陣,為了書(shū)寫(xiě)方便,記
,可列出下表。

下面用lambda表達(dá)式來(lái)實(shí)現(xiàn),用以描述單個(gè)軸的旋轉(zhuǎn)過(guò)程。
import numpy as np
# 將角度轉(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)]
])
Rz = lambda th : np.array([
[cos(th) , sin(th), 0],
[-sin(th), cos(th), 0],
[0 , 0, 1]])
有了旋轉(zhuǎn)矩陣,就可以旋轉(zhuǎn),接下來(lái)讓坐標(biāo)軸沿著三個(gè)軸分別旋轉(zhuǎn)30°,其效果如下

代碼如下
def drawAxis(X, Y, Z, fig, i):
ax = fig.add_subplot(1,3,i,projection='3d')
ax.plot(*X, c='r')
ax.plot(*Y, c='g')
ax.plot(*Z, c='b')
Xx, Yx, Zx = Rx(30) @ X, Rx(30) @ Y, Rx(30) @ Z
Xy, Yy, Zy = Ry(30) @ X, Ry(30) @ Y, Ry(30) @ Z
Xz, Yz, Zz = Rz(30) @ X, Rz(30) @ Y, Rz(30) @ Z
fig = plt.figure("rotate")
drawAxis(Xx, Yx, Zx, fig, 1)
drawAxis(Xy, Yy, Zy, fig, 2)
drawAxis(Xz, Yz, Zz, fig, 3)
plt.show()
轉(zhuǎn)動(dòng)次序?qū)πD(zhuǎn)的影響
由于旋轉(zhuǎn)被建模成了矩陣,而眾所周知矩陣乘法是不可交換的,也就是說(shuō),就算繞著三個(gè)坐標(biāo)軸旋轉(zhuǎn)相同的角度,也會(huì)因?yàn)檗D(zhuǎn)動(dòng)次序不同而引發(fā)不同的結(jié)果。
XYZ = [X, Y, Z]
R_xyz = [Rz(30) @ Ry(30) @ Rx(30) @ R for R in XYZ]
R_zyx = [Rx(30) @ Ry(30) @ Rz(30) @ R for R in XYZ]
R_yxz = [Rz(30) @ Rx(30) @ Ry(30) @ R for R in XYZ]
fig = plt.figure("rotate")
drawAxis(*R_xyz, fig, 1)
drawAxis(*R_zyx, fig, 2)
drawAxis(*R_yxz, fig, 3)
plt.show()
得到下圖

動(dòng)態(tài)演示旋轉(zhuǎn)過(guò)程
30°的轉(zhuǎn)動(dòng)之后,坐標(biāo)軸變得面目全非,接下來(lái)要做的就是動(dòng)態(tài)繪制這三個(gè)坐標(biāo)軸的旋轉(zhuǎn)過(guò)程
from numpy.random import rand
from matplotlib import animation
Rot = [Rx, Ry, Rz]
# 根據(jù)指定坐標(biāo)軸順序來(lái)以指定角度旋轉(zhuǎn)向量
def rotVec(vec, axis, degs):
for i in range(len(axis)):
vec = Rot[axis[i]](degs[i]) @ vec
return vec
# 若x在[a,b]區(qū)間,則對(duì)a取模,若小于a置0,大于b為b-a
def truncMod(x, a, b):
if x < a : return 0
elif x >= b : return b-a
else : return x%(b-a)
# 三個(gè)坐標(biāo)軸
XYZ = [setAxis(10,i) for i in range(3)]
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(projection='3d')
ax.grid()
lines = [ax.plot([],[],[], '-', lw=0.5, c=c)[0]
for c in 'rgb']
def animate(n):
# 按照xyz順序旋轉(zhuǎn)
axis = [2,1,0]
degs = [truncMod(n, st, st + 30) for st in [0,30,60]]
newXYZ = [rotVec(x, axis, degs) for x in XYZ]
for i in range(3):
lines[i].set_data(newXYZ[i][0],newXYZ[i][1])
lines[i].set_3d_properties(newXYZ[i][2])
return lines
ani = animation.FuncAnimation(fig, animate,
range(90), interval=50, blit=True)
#plt.show()
ani.save("zyx.gif")
效果如下
x-y-z

z-y-x

總結(jié)
到此這篇關(guān)于Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣作用的文章就介紹到這了,更多相關(guān)Python動(dòng)態(tài)演示旋轉(zhuǎn)矩陣內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python中sorted()和sort()的使用與區(qū)別
眾所周知,在Python中常用的排序函數(shù)為sorted()和sort()。本文將詳細(xì)介紹sorted()和sort()方法的代碼示例,并解釋兩者之間的區(qū)別,感興趣的可以了解一下2022-03-03
Python實(shí)現(xiàn)爬取百度貼吧帖子所有樓層圖片的爬蟲(chóng)示例
這篇文章主要介紹了Python實(shí)現(xiàn)爬取百度貼吧帖子所有樓層圖片的爬蟲(chóng),涉及基于urllib的網(wǎng)頁(yè)訪問(wèn)與正則匹配相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
在keras中實(shí)現(xiàn)查看其訓(xùn)練loss值
keras model.fit 解決validation_spilt=num 的問(wèn)題
Python數(shù)據(jù)分析numpy文本數(shù)據(jù)讀取索引切片實(shí)例詳解
Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)

