数学建模社区-数学中国

标题: 【MATLAB动画教程五】 [打印本页]

作者: 士心之约    时间: 2015-9-16 14:36
标题: 【MATLAB动画教程五】
  1. %将下列命令保存到M文件中,直接运行
  2. %Matlab时钟动画演示
  3. %rewrite by dynamic
  4. %more information please go to http://www.matlabsky.cn
  5. try
  6. close all
  7. hfig=figure('NumberTitle','off','name',...
  8. 'Clock Animation Demo--by MatlabSky','MenuBar','none');
  9. theta=linspace(0,6.3,1000);
  10. x1=8*cos(theta);y1=8*sin(theta);
  11. plot(x1,y1,'b','linewidth',1.4)%绘制外表盘
  12. hold on
  13. axis equal
  14. x2=7*cos(theta);y2=7*sin(theta);
  15. plot(x2,y2,'y','linewidth',3.5)%绘制内表盘
  16. fill(0.4*cos(theta),0.4*sin(theta),'r');%绘制指针转轴
  17. axis off
  18. axis([-10 10 -10 10])
  19. set(gca,'position',[[0.13 0.05 0.775 0.815]])
  20. title(date,'fontsize',18)
  21. for k=1:12;
  22. xk=9*cos(-2*pi/12*k+pi/2);
  23. yk=9*sin(-2*pi/12*k+pi/2);
  24. plot([xk/9*8 xk/9*7],[yk/9*8 yk/9*7],'color',[0.3 0.8 0.9]);
  25. text(xk,yk,num2str(k),'fontsize',16,'color',...
  26. [0.9 0.3 0.8],'HorizontalAlignment','center');%表盘时刻标度
  27. end
  28. % 计算时针位置
  29. ti=clock;
  30. th=-(ti(4)+ti(5)/60+ti(6)/3600)/12*2*pi+pi/2;
  31. xh3=4.0*cos(th);
  32. yh3=4.0*sin(th);
  33. xh2=xh3/2+0.5*cos(th-pi/2);
  34. yh2=yh3/2+0.5*sin(th-pi/2);
  35. xh4=xh3/2-0.5*cos(th-pi/2);
  36. yh4=yh3/2-0.5*sin(th-pi/2);
  37. hh=fill([0 xh2 xh3 xh4 0],[0 yh2 yh3 yh4 0],[0.6 0.5 0.3]);
  38. % 计算分针位置
  39. tm=-(ti(5)+ti(6)/60)/60*2*pi+pi/2;
  40. xm3=6.0*cos(tm);
  41. ym3=6.0*sin(tm);
  42. xm2=xm3/2+0.5*cos(tm-pi/2);
  43. ym2=ym3/2+0.5*sin(tm-pi/2);
  44. xm4=xm3/2-0.5*cos(tm-pi/2);
  45. ym4=ym3/2-0.5*sin(tm-pi/2);
  46. hm=fill([0 xm2 xm3 xm4 0],[0 ym2 ym3 ym4 0],[0.6 0.5 0.3]);
  47. % 计算秒针位置
  48. ts=-(ti(6))/60*2*pi+pi/2;
  49. hs=plot([0 7*cos(ts)],[0 7*sin(ts)],'color','w','linewidth',2);
  50. set(gcf,'doublebuffer','on');
  51. while 1;
  52. ti=clock;%每次读取系统时间,并进行运算
  53. % 计算时针位置
  54. th=-(ti(4)+ti(5)/60+ti(6)/3600)/12*2*pi+pi/2;
  55. xh3=4.0*cos(th);
  56. yh3=4.0*sin(th);
  57. xh2=xh3/2+0.5*cos(th-pi/2);
  58. yh2=yh3/2+0.5*sin(th-pi/2);
  59. xh4=xh3/2-0.5*cos(th-pi/2);
  60. yh4=yh3/2-0.5*sin(th-pi/2);
  61. set(hh,'XData',[0 xh2 xh3 xh4 0],'YData',[0 yh2 yh3 yh4 0])
  62. % 计算分针位置
  63. tm=-(ti(5)+ti(6)/60)/60*2*pi+pi/2;
  64. xm3=6.0*cos(tm);
  65. ym3=6.0*sin(tm);
  66. xm2=xm3/2+0.5*cos(tm-pi/2);
  67. ym2=ym3/2+0.5*sin(tm-pi/2);
  68. xm4=xm3/2-0.5*cos(tm-pi/2);
  69. ym4=ym3/2-0.5*sin(tm-pi/2);
  70. set(hm,'XData',[0 xm2 xm3 xm4 0],'YData',[0 ym2 ym3 ym4 0])
  71. % 计算秒针位置
  72. ts=-(ti(6))/60*2*pi+pi/2;
  73. set(hs,'XData',[0 7*cos(ts)],'YData',[0 7*sin(ts)])
  74. drawnow;
  75. pause(0.09)
  76. end
  77. catch
  78. ['MatlabSky--打造最优、专业和权威的Matlab技术交流平台!更多信息参见:'...
  79. '<a href="matlab:web http://www.matlabsky.cn">http://www.matlabsky.cn</a>']
  80. return
  81. end
复制代码






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5