matlab制作圆摆线动画
程序员文章站
2022-05-22 15:50:08
...
前言
matlab是一款非常著名的科学计算和仿真软件,以其强大的数学库、数据可视化功能和扩展工具箱而闻名,这里利用matlab绘制了圆的四种摆线的形成动画,话不多言,直接上代码。
圆滚线
%画圆滚线
t=0:pi/100:2*pi;
r=1; %圆的半径
v=1.5; %圆心滚动的速度
w=v/r; %圆滚动的角速度
s=0;
figure;
title('圆滚线');
hold on;
axis equal;
axis([-r 2*pi*r+r -r r]);
for time=0:0.1:2*pi/w
s=v*time;
x=s+r*cos(t);
point_x = s+r*cos(-pi/2-w*time);
y=r*sin(t);
point_y = r*sin(-pi/2-w*time);
h=plot(x,y,'r');
l=plot([s point_x],[0 point_y],'-r');
if(abs(time-0)<1e-3)
plot(point_x,point_y,'.g');
res_x=point_x;
res_y=point_y;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'圆滚线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x point_x],[res_y point_y],'-g');
res_x=point_x;
res_y=point_y;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'圆滚线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.1); %制作gif时建议去掉时延
delete(h);
delete(l);
end
hold off;
运行后生成的gif图片:
螺旋线
%画螺旋线
v=2; %圆心的增长速度
w=1.5; %旋转的角速度
figure;
title('螺旋线');
hold on;
axis equal;
axis([-15 15 -15 15]);
for time=0:0.1:2*pi
x=v*time*cos(w*time);
y=v*time*sin(w*time);
l=plot([0 x],[0 y],'-r');
if(abs(time-0)<1e-3)
plot(x,y,'.g');
res_x=x;
res_y=y;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'螺旋线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x x],[res_y y],'-g');
res_x=x;
res_y=y;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'螺旋线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.1); %制作gif时建议去掉时延
delete(l);
end
hold off;
执行后生成的gif图片:
外圆摆线
%画外圆摆线
w1 = 3; %外圆圆心旋转的角速度
r1 = 4; %内圆的半径
r2 = 1; %外圆的半径
w2 = 9; %外圆滚动的角速度
pp = 0:pi/100:2*pi;
figure;
title('外圆摆线');
hold on;
axis equal;
axis([-(r1+2*r2) r1+2*r2 -(r1+2*r2) r1+2*r2]);
x=r1*cos(pp);
y=r1*sin(pp);
plot(x,y,'r');
for time=0:0.01:2*pi/w1
xx = (r1+r2)*cos(w1*time);
yy = (r1+r2)*sin(w1*time);
x = xx+r2*cos(pp);
y = yy+r2*sin(pp);
px = xx+r2*cos(w2*time);
py = yy+r2*sin(w2*time);
h=plot(x,y,'r');
l=plot([xx,px],[yy,py],'-r') ;
if(abs(time-0)<1e-3)
plot(px,py,'.g');
res_x=px;
res_y=py;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'外圆滚线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x px],[res_y py],'-g');
res_x=px;
res_y=py;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'外圆滚线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.01); %制作gif时建议去掉时延
delete(h);
delete(l);
end
hold off;
执行后生成的图片:
内圆摆线
%画外圆摆线
w1 = 3; %内圆圆心旋转的角速度
r1 = 4; %外圆的半径
r2 = 1; %内圆的半径
w2 = 9; %内圆滚动的角速度
pp = 0:pi/100:2*pi;
figure;
title('内圆摆线');
hold on;
axis equal;
axis([-(r1+2*r2) r1+2*r2 -(r1+2*r2) r1+2*r2]);
x=r1*cos(pp);
y=r1*sin(pp);
plot(x,y,'r');
for time=0:0.01:2*pi/w1
xx = (r1-r2)*cos(w1*time);
yy = (r1-r2)*sin(w1*time);
x = xx+r2*cos(pp);
y = yy+r2*sin(pp);
px = xx+r2*cos(w2*time);
py = yy+r2*sin(w2*time);
h=plot(x,y,'r');
l=plot([xx,px],[yy,py],'-r');
if(abs(time-0)<1e-3)
plot(px,py,'.g');
res_x=px;
res_y=py;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'内圆滚线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x px],[res_y py],'-g');
res_x=px;
res_y=py;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'内圆滚线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.01); %制作gif时建议去掉时延
delete(h);
delete(l);
end
hold off;
执行后生成的gif图片: