demo1.m
u=0:0.01:0.5;
x0=2-u.*sin(pi/6);
y0=zeros(size(u));
z0=u.*cos(pi/6);
P0=[x0;y0;z0];
plot3(P0(1,:),P0(2,:),P0(3,:),'b');
xlabel('x');
ylabel('y');
zlabel('z');
hold on;
omega=repmat([0;0;1],size(x0,1),size(x0,2));
theta=0:pi/180:2*pi;
for i=1:length(theta)
P=RotateAxisTheta(omega,P0,theta(i));
plot3(P(1,:),P(2,:),P(3,:),'b');
drawnow
f = getframe(gcf);
imind = frame2im(f);
[imind,cm] = rgb2ind(imind,256);
if i == 1
imwrite(imind,cm,'demo.gif','gif', 'Loopcount',inf,'DelayTime',0.1);
else
imwrite(imind,cm,'demo.gif','gif','WriteMode','append','DelayTime',0.1);
end
end
RotateAxisTheta.m
function Rn = RotateAxisTheta(omega,R,theta)
%Input:
% omega:旋转轴的矢量三维坐标 可以是3×1 vector,也可以是3×n的矩阵,每一列都是一个vector
% R:旋转前的矢量 可以是3×1 vector,也可以是3×n的矩阵,每一列都是一个vector(列数和omega相等)
% theta:旋转的角度,单位是rad
%Output:
% Rn:旋转后的矢量
%Rn=ones(3,1);
Rn=cos(theta)*R+sin(theta)*cross(omega,R)+(1-cos(theta))*dot(omega,R).*omega;
end
随便玩玩~后面有时间讲讲旋转函数RotateAxisTheta.m
的具体原理。