Introduction to MATLAB making simple animation

1, Introduction
Matlab has powerful visual function, and it can also be used to make animation. The simplest way to realize animation is to set the action route of the target primitive, and then use the circular statement and pause command to update the position of the primitive, so that the animation is completed.
This article takes three swallows (which can be understood as a family of three, haha) flying in a certain area as an example, and takes you step by step to learn the production of simple animation.
2, Simple animation steps
1. Draw animated target graphics
1) To draw a swallow, you can use the sine curve splicing method (a little simple, like a simple hard stroke). The specific methods are as follows:
Take the sine curve between 0 and PI as the wing of a swallow
The curve between 0~pi/2 is regarded as the right wing
The curve between pi/2 and PI is regarded as the left wing
The code is as follows:

x1 = [ 0:0.01:1/2 ] * pi;%Abscissa of right wing
y1 = sin( x1 );          %Right wing ordinate
x2 = [ 1/2:0.001:1 ] * pi;%Abscissa of left wing
y2 = sin( x2 );           %Left wing ordinate
p1 = plot( (x1+pi)/2, (y1+3)/2, 'k' );%The right wing is on the right, so it needs“+pi"
hold on
p2 = plot( x2/2, (y2+3)/2, 'k' );
axis( [ -8, 10, 0, 10 ] )

Drawing result: (a little lonely)


2) Draw another two swallows and follow the above practice. The code is as follows:

p3 = plot( (x1+pi)/2+2, (y1+3)/3, 'k' );
p4 = plot( x2/2+2, (y2+3)/3 , 'k' );
p5 = plot( (x1+pi)/3-2, (y1+3)/3, 'k' );
p6 = plot( x2/3-2, (y2+3)/3, 'k' );

The results are shown in the following figure:

Did you find it so easy to draw a swallow here. Of course, it's more like a goose.
2. Set the scene
1) Add a background color to the rectangular area
%Add a background color to the rectangular area

axis( [ -8, 10, 0, 10 ] )
set( gca, 'color', [ 1, 1, 0.9 ] );%gca Represents the current drawing area

2) Draw another red sun

%Draw red sun
t = (0:0.1:2) * pi;
x = 0.5 * cos(t) + 8;
y = 0.5 * sin(t) + 9;
fill( x, y, 'r' ) 
axis equal
axis( [ -8, 10, 0, 10 ] )%This sentence is used to readjust the drawing area

The image window obtained at this time is shown in the following figure:


3) Draw two more (see if the background is richer in this way)
%Draw two mountains

xh = [ -8 : 0.2 : 8 ];
yh1 = 2 * exp( -(xh + 5).^2 / 2 );
fill( xh, yh1, 'b' )
hold on
yh2 = 1.5 * exp( -xh.^2 / 4 );
area( xh, yh2, 'FaceColor', [ 0.1, 0.1, 0.9 ] );

At this point, the background of the animation becomes as shown in the figure below. Does it look a little better than the figure in the previous step. Of course, you can also design more beautiful.

3. Plan your route
Suppose the swallow moves along a sine curve or cosine curve

path_x = [-2 : 0.01 : 2] *pi;%Abscissa
path_y1 = sin( dir );        %The flight path of the first swallow
path_y2 = cos( dir );        %The flight path of the second swallow
path_y3 = cos( fliplr(dir) );%The flight path of the third swallow

4. Sports design
Put the three swallows into the specified initial position, and then use the set command to modify the position coordinates of the image handles (p1,p2), (p3,p4) and (p5,p6) corresponding to each swallow to realize the position change of the swallows. Coupled with the loop statement and pause command, the flying animation of the swallow can be realized. The code is as follows (taking five flight cycles as an example, the value of the cycle can also be modified, or it can be directly changed to an endless cycle):

hight = 2;%Set the initial height of the swallow
period = 5; %Motor cycle
k = 0;
len = length( path_x );
while k < period
    for i = 1 : len
        set( p1, 'Xdata', path_x(i) +(x1+pi)/2, 'Ydata', path_y1(i)+2  + (y1+3)/2 + hight );
        set( p2, 'Xdata', path_x(i) + x2/2, 'Ydata', path_y1(i) + 2 + (y2+3)/2 + hight );
        set( p3, 'Xdata', path_x(i)-2 + (x1+pi)/2, 'Ydata', path_y2(i)+2  + (y1+3)/3 + hight );
        set( p4, 'Xdata', path_x(i)-2 + x2/2, 'Ydata', path_y2(i) + 2 + (y2+3)/3 + hight );
        set( p5, 'Xdata', path_x(len-i+1) + (x1+pi)/3, 'Ydata', path_y3(i) + 2  + (y1+3)/3 + hight );
        set( p6, 'Xdata', path_x(len-i+1) + x2/3, 'Ydata', path_y3(i) + 2 + (y2+3)/3 + hight );
        pause(0.02);
    end
    path_x = fliplr( path_x );%Reverse return flight
    k = k + 1;
end

At this point, the simple animation of the swallow flying in a scene is completed.
Two scenes in flight are as follows:


3, Complete code

%Take 0~pi The sine curve between the wings of swallows
%0~pi/2 Between the curves as the right wing
%pi/2~pi Between the curves as the left wing
clear all
clc
x1 = [ 0:0.01:1/2 ] * pi;%Abscissa of right wing
y1 = sin( x1 );          %Right wing ordinate
x2 = [ 1/2:0.001:1 ] * pi;%Abscissa of left wing
y2 = sin( x2 );           %Left wing ordinate
p1 = plot( (x1+pi)/2, (y1+3)/2, 'k' );%The right wing is on the right, so it needs“+pi"
hold on
p2 = plot( x2/2, (y2+3)/2, 'k' );
axis( [ -8, 10, 0, 10 ] ) %Rectangular area in the planning area
 
p3 = plot( (x1+pi)/2+2, (y1+3)/3 + 3, 'k' );
p4 = plot( x2/2+2, (y2+3)/3 + 3 , 'k' );
 
p5 = plot( (x1+pi)/3-2, (y1+3)/3 + 1, 'k' );
p6 = plot( x2/3-2, (y2+3)/3 + 1, 'k' );
 
%Add a background color to the rectangular area
axis( [ -8, 10, 0, 10 ] )
%set( gcf, 'doublebuffer', 'on');
set( gca, 'color', [ 1, 1, 0.9 ] );%gca Represents the current drawing area
 
%Draw red sun
t = (0:0.1:2) * pi;
x = 0.5 * cos(t) + 8;
y = 0.5 * sin(t) + 9;
fill( x, y, 'r' ) 
axis equal
axis( [ -8, 10, 0, 10 ] )
%Draw two mountains
xh = [ -8 : 0.2 : 8 ];
yh1 = 2 * exp( -(xh + 5).^2 / 2 );
fill( xh, yh1, 'b' )
hold on
yh2 = 1.5 * exp( -xh.^2 / 4 );
area( xh, yh2, 'FaceColor', [ 0.1, 0.1, 0.9 ] );
 
%Movement route
path_x = [-2 : 0.01 : 2] *pi;%Abscissa
path_y1 = sin( path_x );        %The flight path of the first swallow
path_y2 = cos( path_x );        %The flight path of the second swallow
path_y3 = cos( fliplr(path_x) );%The flight path of the third swallow
set( gcf, 'doublebuffer', 'on');
hight = 2;%Set the initial height of the swallow
period = 5; %Motor cycle
k = 0;
len = length( path_x );
while k < period
    for i = 1 : len
        set( p1, 'Xdata', path_x(i) +(x1+pi)/2, 'Ydata', path_y1(i)+2  + (y1+3)/2 + hight );
        set( p2, 'Xdata', path_x(i) + x2/2, 'Ydata', path_y1(i) + 2 + (y2+3)/2 + hight );
        set( p3, 'Xdata', path_x(i)-2 + (x1+pi)/2, 'Ydata', path_y2(i)+2  + (y1+3)/3 + hight );
        set( p4, 'Xdata', path_x(i)-2 + x2/2, 'Ydata', path_y2(i) + 2 + (y2+3)/3 + hight );
        set( p5, 'Xdata', path_x(len-i+1) + (x1+pi)/3, 'Ydata', path_y3(i) + 2  + (y1+3)/3 + hight );
        set( p6, 'Xdata', path_x(len-i+1) + x2/3, 'Ydata', path_y3(i) + 2 + (y2+3)/3 + hight );
        pause(0.02);
    end
    path_x = fliplr( path_x );%Reverse return flight
    k = k + 1;
end

Tags: MATLAB animation

Posted by tomfmason on Fri, 12 Aug 2022 02:16:37 +0930