% Animate a picture 
clear all
clf

disp(['Starting at: ' datestr(now)])

%Original drawing
%Note: nan is a "pen up" command, leaves a gap.
x0= [0 1 1 0 0 nan 1.5 .5 -.5];
y0= [0 0 1 1 0 nan .5  1.5 .5];
r_ref= [x0;y0]; %Write as an array for easy rotation


n = 300;  % number of drawings to make
t = linspace(0,2*pi, n);

%Loop for animation
for i = 1:n
pause(.01); %Time between plotting and replotting

%The next two lines would generally be the output of an ODE solution.
theta   = pi*sin(t(i));
displacey = 1*cos(10*t(i)); % displacement in y direction

R = [ cos(theta)  -sin(theta);
      sin(theta)  cos(theta)];

r = R* r_ref ;  %rotated and displaced drawing

%bsxfun lets you add a vector to a matrix
r = bsxfun(@plus, r, [0;displacey]);   %add a displacement

%Plotting command likes x and y comps separately
x=r(1,:);
y=r(2,:);

plot(x,y)
axis('equal'); 
axis([-4 4 -4 4]);
%hold on
shg
end

shg  % SHow Graph
disp(['Done at: ' datestr(now)])
