matlab draws the movement process of a triangle and stores it as gif file or avi file

Posted by dwilson on Thu, 24 Oct 2019 16:19:09 +0200

The moving process of drawing a triangle with matlab

matlab draws the movement process of a triangle and stores it as gif file or avi file

  1. In the first step, the code of drawing a triangle motion process with matlab is given.
  2. Create the second step, modify the code, and store this process as a gif diagram.
  3. The third step is to modify the code further and store the procedure as avi file.

Realize triangle motion

Determine the position, direction and size of a triangle

function [x y]=Triangle(pos,vec,dis)
% pos Define triangle center position
% vec Define triangle direction
% dis Define Triangle Size
vec  = vec/norm(vec);
vecL = [-vec(2) vec(1)]/sqrt(3);
pos1 = pos + dis*vec;
pos2 = pos - dis*vec + 2*dis*vecL;
pos3 = pos - dis*vec - 2*dis*vecL;
x = [pos1(1) pos2(1) pos3(1)];
y = [pos1(2) pos2(2) pos3(2)];

Draw the movement process

pos = [-10,-10];
vec = [10,10];
dis = 8;

[x y] = Triangle(pos, vec, dis);
h = fill(x,y,'g'),hold on
axis([-30, 100, -30, 100]);
grid on

for k = 0:100
    pos = pos + [1 1];
    [x y] = Triangle(pos, vec, dis);
    set(h,'XData',x,'YData',y); %%Reset drawing objects
    
   pause(0.001);
end

Results obtained:

Store the motion process as a gif file

pos = [-10,-10];
vec = [10,10];
dis = 8;

[x y] = Triangle(pos, vec, dis);
h = fill(x,y,'g'),hold on
axis([-30, 100, -30, 100]);
grid on

pic_num = 1;%For storage gif file

for k = 0:100
    pos = pos + [1 1];
    [x y] = Triangle(pos, vec, dis);
    set(h,'XData',x,'YData',y); %%Reset drawing objects
    
    %%%%%%%%%%storage gif File snippet%%%start
    F=getframe(gcf);
    I=frame2im(F);
    [I,map]=rgb2ind(I,256);
    if pic_num == 1
        imwrite(I,map,'test0.gif','gif', 'Loopcount',inf,'DelayTime',0.2);
    else
        imwrite(I,map,'test0.gif','gif','WriteMode','append','DelayTime',0.2);
    end
    pic_num = pic_num + 1;
    %%%%%%%%%%storage gif File snippet%%%End
    
   pause(0.001);
end

Storing motion process as avi file

pos = [-10,-10];
vec = [10,10];
dis = 8;

[x y] = Triangle(pos, vec, dis);
h = fill(x,y,'g'),hold on
axis([-30, 100, -30, 100]);
grid on

%%%%%%%%%%%%%%%%%%%%%%%%%%Save as video file or not%%%%%%%%%%%%%%%%%%%%%start
bSaveAVI = 1;  %%Save redraw process to video file

if bSaveAVI
    aviname = input('input the file name for avi: ','s');
    aviobj=VideoWriter(aviname);%, 'Uncompressed AVI');   %Define a video file to save animation
    open(aviobj);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%Save as video file or not%%%%%%%%%%%%%%%%%%%%%End

for k = 0:100
    pos = pos + [1 1];
    [x y] = Triangle(pos, vec, dis);
    set(h,'XData',x,'YData',y); %%Reset drawing objects
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%Save as video file%%%%%%%%%%%%%%%%%%%%%start
    if bSaveAVI 
        frame=getframe(gca);   %Save image to video file
        im=frame2im(frame);
        writeVideo(aviobj,im)
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%Save as video file%%%%%%%%%%%%%%%%%%%%%End
    
   pause(0.001);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%Close video file handle%%%%%%%%%%%%%%%%%%%%%start
if bSaveAVI
    close(aviobj); %%Close video file handle
end
%%%%%%%%%%%%%%%%%%%%%%%%%%Close video file handle%%%%%%%%%%%%%%%%%%%%%End

Topics: MATLAB