Search code examples
matlabanimationrotationgeometryangle

Animation of Circle angle rotation using MATLAB


I have a circle and partition this circle into 3 sector(120 angle each).Now I want to increase angle 50 degree to one part and decrease 50 degree to another part and keep constant third part (like 170 degree for first part,70 degree for second and 120 degree third).I want to make a animation for the whole process using MATLAB.

How can I get this? If anyone have any source code for this process using MATLAB, it helps me much.

I just draw a circle and divide it three equal sectors and polt some points into the circle.Here, i attached the following code:

x0=2;   
y0=1;    
r=1;    
teta=-pi:0.01:pi;    
x=r*cos(teta)+x0    
y=r*sin(teta)+y0    
plot(x,y)    
hold on    
scatter(x0,y0,'or')    
axis square 

%----------------------------------------
% divide your circle to n sectors

n=3    
tet=linspace(-pi,pi,n+1)    
xi=r*cos(tet)+x0    
yi=r*sin(tet)+y0    
for k=1:numel(xi)    
    plot([x0 xi(k)],[y0 yi(k)])    
    hold on    
    p1=[1.5,0.4];   
    p2=[2,0.8];    
    p3=[2.5,0.2];    
    p4=[2.5,1];   
    p5=[1.5,1.6];    
    p6=[1.5,0.8];    
    p7=[2,1.2];    
    p8=[2,1.4];    
    p9=[1.6,0.7];    
    p10=[2.5,0.6];    
    p11=[2.7,0.5];    
    p12=[2,0.9];
    p=[p1;p2;p3;p4;p5;p6;p7;p8;p9;p10;p11;p12]';     
    plot(p(1,:),p(2,:),'go')    
end

Solution

  • The following code will produce exactly what you want. I simplified some of your code and added comments to the sections. All the parameters can be set at the top. The script will create a movie file circle_anim.mp4 that can be viewed outside Matlab.

    % set options
    x0 = 2;     % origin x-coordinate
    y0 = 1;     % origin y-coordinate
    r  = 1;     % radius of circle
    n  = 3;     % number of pieces
    m  = 50;    % movement of separator in radians (+ACW / -CW)
    ts = 3;     % target separator (1...n)
    fs = 30;    % frame rate in fps
    T  = 2;     % duration in seconds
    s  = T*fs;  % movement step count
    
    % predefined points -> [x1,x2,xn;y1,y2,yn]
    p = [ 1.5, 2.0, 2.50, 2.5, 1.5, 1.5, 2.0, 2.0, 1.6, 2.5, 2.7, 2.0;
          0.4, 0.8, 0.20, 1.0, 1.6, 0.8, 1.2, 1.4, 0.7, 0.6, 0.5, 0.9];
    
    % calculate circle
    theta = -pi:0.01:pi;    
    cirx = r*cos(theta) + x0;    
    ciry = r*sin(theta) + y0;
    
    % initial plot
    figure; hold on;
    axis square;
    plot(x0,y0,'or');           % origin
    plot(cirx,ciry);            % circle
    plot(p(1,:),p(2,:),'go');   % predefined points
    
    % calculate and plot separations
    ciro = linspace(-pi,pi,n+1);
    for k = 1:(numel(ciro)-1)
        ph(k) = plot([x0,x0+r*cos(ciro(k))],[y0,y0+r*sin(ciro(k))]); %#ok<SAGROW>
    end
    
    % vary target separator and create frames
    clearvars myFrames;
    movo = linspace(ciro(ts),ciro(ts)+(m/180*pi),s);
    for k = 1:numel(movo)
        set(ph(ts), 'XData', [x0,x0+r*cos(movo(k))]);
        set(ph(ts), 'YData', [y0,y0+r*sin(movo(k))]);
        myFrames(k) = getframe;  %#ok<SAGROW>
    end
    
    % write frames to video
    myMovie = VideoWriter('circle_anim.mp4','MPEG-4');
    myMovie.FrameRate = fs;
    open(myMovie);
    for k = 1:length(myFrames)
        writeVideo(myMovie,myFrames(k));
    end
    close(myMovie);