Search code examples
matlaboctave

Filling in plot animation (polar function)


enter image description here

I have a set of videos shown in an assignment. How is this animation created? I am trying to create something similar. Below is my attempt; it is only drawing lines because I believe it is not filling in the correct inputs. Guidance would be much appreciated

clear;close all;

th = 0:0.1:pi*2
r = cos(2*th) .* sin(2*th);
x = r .* cos(th);
y = r .* sin(th);
plot(x,y);
hold on

for th = 0:0.1:pi*2
  x0 = [r .* cos(th)];
  y0 = [r .* sin(th)];
  fill(x0,y0,'b');
  pause (0.5);
end
hold off

Solution

  • A similar animation can be drawn by filling from lists. Putting clf in the for-loop is optional.

    This following code animates the drawing process of a small flower. The flower will look proportionally bigger or smaller by changing the axis.

    X = 0:0.01:pi*2;
    r = cos(2*X).*sin(2*X);
    
    x=r.*(cos(X));
    y=r.*(sin(X));
    
    x_1 = []; y_1 = [];
    
    for i=1:629                    %629 is the size of x and of y
        x_1 = [x(i), x_1];
        y_1 = [y(i), y_1];
        
        fill(x_1,y_1,'b'); hold on;
        axis([-2 2 -2 2]); hold on;
        pause(0.01);
    end