關(guān)于在坐標(biāo)系中旋轉(zhuǎn)平移物體的編程實(shí)現(xiàn)Matlab, Python
寫在前面
上一篇文章中我已經(jīng)講了在坐標(biāo)系中旋轉(zhuǎn)或平移物體的理論,所以這一篇就直接開始搬代碼。
其實(shí)我寫代碼的邏輯很好懂,就是把旋轉(zhuǎn)和平移包裝成函數(shù),然后一個(gè)操作就相當(dāng)于調(diào)用函數(shù)即可。
代碼放出
平移物體
%%%MATLAB
function [X2, Y2, Z2] = Move(a, x, X1, Y1, Z1)
%確定坐標(biāo)軸與位移矩陣
if a == 1
move = [x; 0; 0];
else if a == 2
move = [0; x; 0];
else if a == 3
move = [0; 0; x];
end
end
end
%矩陣的大小與初始化操作后的矩陣
l = size(X1);
X2 = zeros(size(X1));
Y2 = zeros(size(Y1));
Z2 = zeros(size(Z1));
%對(duì)每一行進(jìn)行操作
for i = 1:l(1)
temp = [X1(i,:); Y1(i,:); Z1(i,:)] + move*ones(1, l(2));
X2(i,:) = temp(1,:);
Y2(i,:) = temp(2,:);
Z2(i,:) = temp(3,:);
end
end
以上是MATLAB的代碼,可以看出我主要分成了三個(gè)步驟,1)確定參數(shù),2)初始化,3)進(jìn)行操作。
Python的邏輯相同
###Python
def Move(a, x, X1, Y1, Z1):
'''對(duì)坐標(biāo)進(jìn)行平移操作'''
#確定坐標(biāo)軸與位移矩陣
if a == 1:
move = np.array([[x], [0], [0]])
elif a == 2:
move = np.array([[0], [x], [0]])
elif a == 3:
move = np.array([[0], [0], [x]])
#數(shù)組的大小
s = X1.shape
#初始化操作后的數(shù)組
X2, Y2, Z2 = [np.zeros(s) for i in range(3)]
for i in range(s[0]):
#進(jìn)行操作
temp = np.vstack((X1[i,:], Y1[i,:], Z1[i,:])) + move*np.ones(s[1])
X2[i,:] = temp[0,:]
Y2[i,:] = temp[1,:]
Z2[i,:] = temp[2,:]
return X2, Y2, Z2
旋轉(zhuǎn)物體
%%%MATLAB
function [X2, Y2, Z2] = Rotate(a, theta, X1, Y1, Z1)
%確定坐標(biāo)軸與旋轉(zhuǎn)矩陣
if a == 1
rotate = [1 0 0; 0 cos(theta) sin(theta); 0 -sin(theta) cos(theta)];
else if a == 2
rotate = [cos(theta) 0 -sin(theta); 0 1 0; sin(theta) 0 cos(theta)];
else if a == 3
rotate = [cos(theta) sin(theta) 0; -sin(theta) cos(theta) 0; 0 0 1];
end
end
end
%矩陣的大小與初始化操作后的矩陣
l = size(X1);
X2 = zeros(size(X1));
Y2 = zeros(size(Y1));
Z2 = zeros(size(Z1));
%對(duì)每一行進(jìn)行操作
for i = 1:l(1)
temp = rotate*[X1(i,:); Y1(i,:); Z1(i,:)];
X2(i,:) = temp(1,:);
Y2(i,:) = temp(2,:);
Z2(i,:) = temp(3,:);
end
end
過程一模一樣,只不過是旋轉(zhuǎn)矩陣不同以及操作方式不同而已。
###Python
def Rotate(a, theta, X1, Y1, Z1):
'''對(duì)坐標(biāo)進(jìn)行旋轉(zhuǎn)操作'''
if a == 1:
rotate = np.array([[1, 0, 0], [0, np.cos(theta), np.sin(theta)], [0, -np.sin(theta), np.cos(theta)]])
elif a == 2:
rotate = np.array([[np.cos(theta), 0, -np.sin(theta)], [0, 1, 0], [np.sin(theta), 0, np.cos(theta)]])
elif a == 3:
rotate = np.array([[np.cos(theta), np.sin(theta), 0], [-np.sin(theta), np.cos(theta), 0], [0, 0, 1]])
s = X1. shape
X2, Y2, Z2 = [np.zeros(s) for i in range(3)]
for i in range(s[0]):
temp = np.dot(rotate,np.vstack((X1[i,:], Y1[i,:], Z1[i,:])))
X2[i,:] = temp[0,:]
Y2[i,:] = temp[1,:]
Z2[i,:] = temp[2,:]
return X2, Y2, Z2
主函數(shù)
下面我就放MATLAB的主函數(shù)了,效果圖就是如下圖所示,采取的操作是繞z=y-1軸右手方向旋轉(zhuǎn)pi。

%%%MATLAB
clc, clear, close all
figure(1)
%做坐標(biāo)軸
plot3([0 0], [0 0], [0 2], 'k', 'LineWidth', 2)
axis([-2 2 0 2 -2 2])
hold on
grid on
plot3([0 2], [0 0], [0 0], 'k', 'LineWidth', 2)
plot3([0 0], [0 2], [0 0], 'k', 'LineWidth', 2)
xlabel('x')
ylabel('y')
zlabel('z')
%做旋轉(zhuǎn)軸
y_ = 0:0.1:2;
z_ = y_-1;
x_ = zeros(size(y_));
plot3(x_, y_, z_)
%初始化各種數(shù)據(jù)
x = 0:0.1:1;
y = 0:0.1:1;
[X, Y] = meshgrid(x, y);
Z = X+Y;
%畫原位置物體
surf(X, Y, Z)
%操作一,平移
[X1, Y1, Z1] = Move(2, -1, X, Y, Z);
%操作二,旋轉(zhuǎn)
[X2, Y2, Z2] = Rotate(1, pi/4, X1, Y1, Z1);
[X3, Y3, Z3] = Rotate(2, pi, X2, Y2, Z2);
[X4, Y4, Z4] = Rotate(1, -pi/4, X3, Y3, Z3);
[X5, Y5, Z5] = Move(2, 1, X4, Y4, Z4);
%畫旋轉(zhuǎn)后的物體
surf(X5, Y5, Z5)
Python的邏輯同理,但是因?yàn)槲襊ython作圖不是很熟,所以就沒有作圖了,有興趣的朋友可以自己做一下試試。
總結(jié)
以上就是所有內(nèi)容,從一點(diǎn)思考引出這么多內(nèi)容也算是受益匪淺了,希望我寫的這兩篇文章能夠幫助到更多的人理解這個(gè)咯。
以上所有代碼我都放在我的Github:https://github.com/HanpuLiang/Something-Small
如果喜歡的話麻煩點(diǎn)個(gè)喜歡噢~