1/20/07

1.1.5 迴轉(Rotating)

對某特定軸作迴轉,在機構學中是常見的例子。若屬平面旋轉,迴轉座標之計算較為簡單,若屬不規則方向之迴轉,其運算較為複雜。因此,迴轉的動作常因迴轉中心的位置而不同,這裡我們僅討論以原點為轉動中心的例子。在二維中,迴轉可僅考慮垂直於xy面的方向,故僅有一種,三維者則分x-、y-與z-及三者兼具的轉動。茲分別說明如下:

二維:





在二維座標之迴轉,以兩座標間之夾角θ決定之。設原座標為(x,y),同一點P經座標旋轉一個角度θ後,其新座標為(x',y')。由圖中可以獲得下列關係式:

x'= xcosθ+ysinθ
y'=-xsinθ+ycosθ

上式為實際可用的關係式,利用MATLAB也可以直接求得解,例如:

>> x=[2 3 4 5];y=[-1 2 -4 5];
>> xprime=x*cosd(30)+y*sind(30)
xprime =
1.2321 3.5981 1.4641 6.8301
>> yprime=-x*sind(30)+y*cosd(30)
yprime =
-1.8660 0.2321 -5.4641 1.8301

上式中,係原座標x=[2 3 4 5];y=[-1 2 -4 5]經旋轉30度後之座標值。三角函數之參數若使用度數,可利用sind,cosd等函數可必經過轉換為弧度;然而若使用sin,cos則必須事先轉化為弧度才能得到正確的結果。

上式只是簡單的二維計算,若屬三維且轉軸的方向較為複雜時,仍然以採用矩陣乘法為佳。其型式可以轉換如下:


[x' y' 1]=[x y 1][A(3x3)];

A(3x3)=[ cosθ sinθ 0
 -sinθ cosθ 0
  0 0   1]

C =[x y 1];
C'=[x' y' 1];

C' = C A;



三維:


三維中之特性矩陣則依旋轉軸不同如下:

以x軸旋轉:



A(4x4)=[1 0 0 0
0 cosθx sinθx 0
0 -sinθx cosθx 0
0 0 0 1 ]

以y軸旋轉:



A(4x4)=[cosθy 0 sinθy 0
0 1 0 0
sinθy 0 cosθy 0
0 0 0 1 ]

以z軸旋轉:



A(4x4)=[ cosθ sinθ 0 0
-sinθ cosθ 0 0
0 0 1 0
0 0 0 1 ]

以y-, x-, x- 軸複合旋轉:


可以利用上述三個方向連續轉動。

計算得A矩陣後,即可逕行計算新座標如下:
C=[ x y z1 ];
C'=[x' y' z' 1];
C' = C A;

在本節後面所附的函數在MATLAB程式trans4函數是可以就上述不同的情況處理平移、放大及迴轉等問題。在此程式trans4(C, r, mod)中,利用其最後輸入項mod作為控制碼,mod=1時為平移;mod=2時為放大縮小;mod=3mod=4時為mod=5時則為迴轉。因此其對應r值為處理其方矩陣之參數。當mod=5時為迴轉問題,其參數r即為欲旋轉之角度[thetax thetay thetaz];若僅為二維為僅能使用一個參數,即[thetaz]:

例一、以一箭號旋轉z軸六次



>> c=[0 0;0 20;-3 4;3 4;0 20] %製作迴轉物件
c =
0 0
0 20
-3 4
3 4
0 20
>> hold on;
>> for theta=1:6,c=trans4(c,[60],5);line(c(:,1),c(:,2));end; %連續迴轉60度
>> axis equal;
>> grid on;


平面迴轉

例二、進行立體圖以Y軸迴轉



clf; %將前面的各圖清除
c1=[0 0 0;1 0 0;0 1 0;0 0 0;0 0 2;1 0 0;0 1 0;0 0 2]; %製造迴轉用的立體物件
hold on; %連續繪圖模式
for theta=0:30:360, %對Y軸旋作等級旋轉
c=trans4(c1,[0 theta 0],5); %轉換座標
line(c(:,1),c(:,2),c(:,3)); %進行繪圖線
end;
grid on;
xlabel('x'); %X-座標名稱
ylabel('y');
zlabel('z');


立體迴轉

trans4程式內容



function xprime=trans4(x,delx,mod)
%
%function xprime=trans4(x,delx,mod)
%For dimensional transformation
%mod indicates the type of transformation
% mod=1 for translation
% mod=2 for scaling(enlarging or shrinking)
% mod=3 for inversion delx=[-1 -1 -1]
% mod=4 for shearing delx=[xa ya za]
% mod=5 for rotation delx=[angx angy angz]
% written by D.S. Fon, Bime, NTU
%
[n,m]=size(x);
x=[x,ones(n,1)];
tt=eye(m+1);
switch mod,
case 1 %translation
tt(m+1,:)=[delx 1];
xx=x*tt;
case {2,3} %enlarging & inversion
tt(tt==1)=[delx 1];
xx=x*tt;
case 4 %shear
if m<3,
tt(2,1)=delx(1);
tt(1,2)=delx(2);
else
if size(delx,1)==1,
delx(2,:)=0;
end;
tt(2:3,1)=delx(1:2,1);%[1 d g 0]' for x
tt(1,2)=delx(1,2); %[b 1 0 0]' for y
tt(3,2)=delx(2,2); %[b 1 h 0]' for y again
tt(1:2,3)=delx(1:2,3);%[c f 1 0]' for z
end;
xx=x*tt;
case 5
d2g=pi/180;
theta=delx*d2g;
if m<3,
t=theta(1);
sint=sin(t);cost=cos(t);
tt(1,1)=cost;tt(1,2)=sint;
tt(2,1)=-sint;tt(2,2)=cost;
xx=x*tt;
else
tt0=tt;
if abs(theta(1))>0, % rotate with x-axis
cost=cos(theta(1));sint=sin(theta(1));
tt0(2,2:3)=[cost sint];
tt0(3,2:3)=[-sint cost];
x=x*tt0;
end;
if abs(theta(2))>0, % rotate with y-axis
cost=cos(theta(2));sint=sin(theta(2));
tt0=tt;
tt0(1,1:3)=[cost 0 -sint];
tt0(3,1:3)=[sint 0 cost];
x=x*tt0;
end;
if abs(theta(3))>0, % rotate with z-axis
cost=cos(theta(3));sint=sin(theta(3));
tt0=tt;
tt0(1,1:2)=[cost sint];
tt0(2,1:2)=[-sint cost];
x=x*tt0;
end;
xx=x;
end;
end
xprime=xx(:,1:m);

No comments:

Post a Comment