1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| function StepAnimation(HannoiStep) [r,~] = size(HannoiStep); for index = 1 : 1 : r step = HannoiStep(index,:); pause_times = 1; DrawFrame(step) pause(pause_times); if index < r clf; end end end
function DrawFrame(step) [~,n] = size(step);
coArray = [ [0 0.4470 0.7410]; [0.8500 0.3250 0.0980]; [0.4660 0.6740 0.1880]; [0.9290 0.6940 0.1250]; [0.3010 0.7450 0.9330]; [0.4940 0.1840 0.5560]; [0.6350 0.0780 0.1840]; ];
xArray = linspace(0.5,1.5,n);
for i = 1:3 subplot(1,3,i); axis([-1.5,1.5,0,3]); line([0 0],[0.5 3],'LineWidth',10,'Color',[0.3 0.3 0.3]); line([-1.5 1.5],[0.5 0.5],'LineWidth',10,'Color',[0.3 0.3 0.3]); for index = n : -1 : 1 if step(index) == i Ynum = 0; for j = n : -1 : index+1 if step(j) == i Ynum = Ynum+1; end end line([-xArray(index) xArray(index)],[0.7+0.3*Ynum... 0.7+0.3*Ynum],'LineWidth',30,'Color',coArray(index,:)); end end axis off; end end
|