% Basics of animation
% A very simple example: Growing Plant example

close all; clear; clc; % Clean-up tasks

% Initialize values
L1 = 110.0; % Plant's Stem     Length in mm
L2 = 140.0; % Plant's Sub-stem Length in mm

lx(1) = 0.0;
ly(1) = 0.0;

lx(2) = lx(1) + L1;
ly(2) = ly(1) ;

lx(3) = lx(2)  ;
ly(3) = ly(2) +L2;

hf = figure('color','white'); % Open a figure window with given bk color
% maximize(hf); % Once you run this program sucessfully
                % uncomment this function, if u have a Windows machine

axis equal
SUM_L1_L2 = L1 + L2;

% The size of the "graph paper" should be large enough to accomaodate the
% arm in any position, so calculate maximum streach of the arm along (X,Y)
maximum_x_limit =   ceil(SUM_L1_L2/100)*100;
maximum_y_limit =   ceil(SUM_L1_L2/100)*100;
minimum_x_limit = - ceil(SUM_L1_L2/100)*100;
minimum_y_limit = - ceil(SUM_L1_L2/100)*100;

xlim([minimum_x_limit maximum_x_limit]);
ylim([minimum_y_limit maximum_y_limit]);

% Show the "tick marks" after every 100 points
set(gca,'XTick',minimum_x_limit : 100 : maximum_x_limit);
set(gca,'YTick',minimum_y_limit : 100 : maximum_y_limit);

% Show Grid
grid on % Show major grid lines
set(gca, 'GridLineStyle', '-');
grid(gca,'minor') % Show the minor grid lines as well

% Show the tiltle in the given color [R,G,B]
title('Growing Plant: CLICK ON THE IMAGE TO ANIMATE once','Color',[0 0 1])

hold on % Do NOT clear a plot to draw a new plot

% Draw a tree and SAVE THE HANDLE FOR EACH PLOT:
         % Draw Link-1 that is stem
         ht(1) = plot(lx(1:2), ly(1:2), 'color', [0 .5 0],'LineWidth',3); % Dark Green
         % Draw Link-2 that is sub-stem
         ht(2) = plot(lx(2:3), ly(2:3), 'color', [0  1 0],'LineWidth',3); % Light Green
         % Draw a circle at the Point lx(1), ly(1)
         ht(3) = plot(lx(1),ly(1),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[0 1 0], 'MarkerSize',6);
         % Draw a circle at the Point lx(2), ly(2)
         ht(4) = plot(lx(2),ly(2),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[0 1 1], 'MarkerSize',6);
         % Draw a circle at the Point lx(3), ly(3)
         ht(5) = plot(lx(3),ly(3),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[1 0 0], 'MarkerSize',6);

% Wait for the user to start the animation by clicking the mouse
 waitforbuttonpress;

 pause on % Enable delays
 for j = 10:300

    lx(2) = j/3; % Modify the value of Second Point [lx(2), ly(2)]
    ly(2) = j/2;

    lx(3) = j;  % Modify the value of Third   Point [lx(3), ly(3)]
    ly(3) = j;

    % Update the plot: using the "handel(s)"
    set(ht(1),'XData',lx(1:2));    set(ht(1),'YData',ly(1:2));
    set(ht(2),'XData',lx(2:3));    set(ht(2),'YData',ly(2:3));
    set(ht(3),'XData',lx(1));      set(ht(3),'YData',ly(1));
    set(ht(4),'XData',lx(2));      set(ht(4),'YData',ly(2));
    set(ht(5),'XData',lx(3));      set(ht(5),'YData',ly(3));

    drawnow      % Just dont queue it for afterwards, draw it now

    pause(0.02); % pauses execution for n seconds before continuing,

 end

 % Save a copy on the disk, in the current working directory:
 saveas(gcf,'output.jpg');