12. Animation

 

12.1     Animating by whiting out successive images

12.2     Watching comets

12.3     Animating by drawing now

12.4     Making movies

12.5     Saving movies

12.6     Reading and running previously saved movies

12.7     Practicing animation

 

 

top of page

12.1 Animating by whiting out successive images

 

 

Code 12.1.1

 

close all

clear all

 

Sx = 0;

Sy = 0;

 

moves = 6;

 

Sa = linspace(.15*pi,.45*pi,moves);

Ea = linspace(.75*pi,.55*pi,moves);

 

position = [];

 

figure(1)

hold on; grid on; box on;

xlim([-2.5 2.5]);

ylim([-2.5 2.5]);

 

for i = 1:moves

    Ex(i) = Sx + cos(Sa(i));

    Ey(i) = Sy + sin(Sa(i));

 

    Hx(i) = Ex(i) + cos(Ea(i));

    Hy(i) = Ey(i) + sin(Ea(i));

 

    position = [position; [Sx Ex(i) Hx(i)] [Sy Ey(i) Hy(i)]];

    plot(position(i,1:3),position(i,4:6),'ko-');

    if i == 1

        pause;

    end

    want_animation = 0;

    if want_animation

        pause(.1)

        if i < moves

            plot(position(i,1:3),position(i,4:6),'wo-');

        end

    end

end

 

Output 12.1.1

 

 

 

 

top of page

12.3 Animating by drawing now

 

Code 12.3.1

 

figure(2)

pause

hold off

for i = 1:moves

    plot(position(i,1:3),position(i,4:6),'ko-');

    grid on

    xlim([-2.5 2.5]);

    ylim([-2.5 2.5]);

    drawnow

end

pause

 

 

top of page

12.4 Making movies

 

 

Code 12.4.1

 

figure(3)

grid on

box on

xlim([-2.5 2.5]);

ylim([-2.5 2.5]);

hold on

F = moviein(moves);  % reserve n frames for the movie

for i = 1:moves

    plot(position(i,1:3),position(i,4:6),…

         'ko-','erasemode','normal');

    F(i) = getframe;

end

pause

movie(F,1)  % movie(F,0) is often preferable

 

 

 

top of page

12.5 Saving movies

 

Code 12.5.1

 

movie2avi(F,'myfirstmovie.avi')

 

 

top of page

12.6 Reading and running previously saved movies

 

 

Code 12.6.1

 

figure(4)

pause

mov = aviread('myfirstmovie.avi');

axis off

movie(mov,0)

 

Code 12.6.2

 

figure(5)

mov = aviread('track.avi');

axis off

movie(mov,0)

im = mov(4).cdata;

image(im);

axis image;

 

Output 12.6.2