马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
本帖最后由 meiyongyuandeze 于 2011-4-22 23:16 编辑
其实一直都很学习用matlab做动画,但却一直是没有大把的时间,平时事情太多了。最近的一个项目可能要用的到动画的编写,所以最近一直在看动画编程的一些资料,自己昨天晚上编写了一个很简单的动画,主要是关于理论力学中的“运动合成定理”的一个动画演示。编写这个程序主要是学习动画命令和一些功能,自己的水平实在是有限,很多功能都还是很熟练,所以动画只是粗糙的编写了下,凑合真看看吧,也算是交流吧,高手们可别笑话哈。
最近我也将继续就这个程序进行修改,不断添加一些新的功能,算是一起学习吧!
- clc;
- clear;
- close all;
- %设置图像
- movie=figure('name','运动合成演示');
- set(movie,'color',[0.75,0.75,0.75])
- set(gcf,'menubar','none','toolbar','none');
- fill([0,10,10,0],[9,9,10,10],'r');
- text(2.5,9.5,'运动合成演示 ','color','k');
- axis([-5,15,-1,10]);
- grid on;
- %变量设置
- x0_wheel1=0.65;
- y0_wheel1=-0.6;
- x0_wheel2=4;
- y0_wheel2=-0.6;
- x0_ball=0.0;
- y0_ball=5;
- x0_body=0.0;
- y0_body=5.0;
- Dong1=line(x0_wheel1,y0_wheel1,'color','r','linestyle','.','erasemode','xor','markersize',60);
- Dong2=line(x0_wheel2,y0_wheel2,'color','r','linestyle','.','erasemode','xor','markersize',60);
- Dong4=line([0,5],[0,0],'color','g','linestyle','-','linewidth',10);
- Dong5=line([0,0],[-0.15,5],'color','g','linestyle','-','linewidth',10);
- Dong6=line([0,5],[5,0],'color','g','linestyle','-','linewidth',10);
- Dong7=line([0,2.5],[0,2.5],'color','g','linestyle','-','linewidth',10);
- Dong3=line(0.3,5.0,'color','r','linestyle','.','erasemode','none','markersize',30);
- orbit1=line('xdata',x0_ball,'ydata',y0_ball,'color','k','linewidth',2);
- orbit2=line('xdata',x0_body,'ydata',y0_body,'color','k','linewidth',2);
- control_flag=1;
- pausetime=0.5;
- %设置按钮
- bon1=uicontrol('parent',movie,'units','points','tag','b2','style','pushbutton','string','停止','backgroundcolor',[0.75,0.75,0.75],'position',[150 0 50 20],'callback','control_flag=0;');
- bon2=uicontrol('parent',movie,'units','points','tag','b3','style','pushbutton','string','退出','backgroundcolor',[0.75,0.75,0.75],'position',[250 0 50 20],'callback',['control_flag=1;,','close']);
- %运动设置
- t=0;
- dt=0.01;
- while t<10
- if control_flag==0
- break
- end
- t=t+dt;
- drawnow;
- x_wheel1=x0_wheel1+0.05*t^2;
- y_wheel1=y0_wheel1;
- set(Dong1,'xdata',x_wheel1,'ydata',y_wheel1);
- x_body=0.05*t^2;
- x_wheel2=x0_wheel2+x_body;
- y_wheel2=y0_wheel2;
- set(Dong2,'xdata',x_wheel2,'ydata',y_wheel2);
- x0_body=[x0_body x_body];
- y0_body=[y0_body 5];
- set(orbit2,'xdata',x0_body,'ydata',y0_body);
- x_ball=0.1*t^2;
- y_ball=5-0.05*t^2;
- set(Dong3,'xdata',x_ball,'ydata',y_ball);
- x0_ball=[x0_ball x_ball];
- y0_ball=[y0_ball y_ball];
- set(orbit1,'xdata',x0_ball,'ydata',y0_ball);
- x_body1=0.05*t^2-0.15;
- y_body1=0;
- set(Dong4,'xdata',[x_body1;x_body1+5],'ydata',[y_body1;y_body1]);
- x_body2=0.05*t^2-0.15;
- y_body2=0;
- set(Dong5,'xdata',[x_body2;x_body2],'ydata',[y_body2;y_body2+5]);
- x_body3=0.05*t^2-0.15;
- y_body3=5;
- set(Dong6,'xdata',[x_body3;x_body3+5],'ydata',[y_body3;y_body3-5]);
- x_body4=0.05*t^2-0.15;
- y_body4=0;
- set(Dong7,'xdata',[x_body4;x_body4+2.5],'ydata',[y_body4;y_body4+2.5]);
- end
- %添加标识
- if control_flag~=0
- line([5,10],[5,0],'color','k','linestyle','-','linewidth',2.5);
- hold on;
- pause(pausetime);
- fill([0,5,5,0],[5.5,5.5,6.5,6.5],'r');
- text(1,6,'牵连运动 ','color','k');
- pause(pausetime);
- fill([8,13,13,8],[3,3,4,4],'b');
- text(9,3.5,'相对运动 ','color','k');
- pause(pausetime);
- fill([-2.5,2.5,2.5,-2.5],[2,2,3,3],'m');
- text(-1,2.5,'绝对运动 ','color','k');
- pause(10*pausetime)%无操作5s后关闭并退出
- close all
- end
复制代码 |