% This program draws a simple robotic arm in a figure window

% Cleaning tasks
close all;  % Close previously open figures if any
clear;      % Clear the workspace variables
clc;        % Clear the console

% Initialize values
L1 = 110.0; % Link Length in mm
L2 = 140.0; % Link Length in mm

% initalize Arm Coordinates
lx(1) = 0.0;
ly(1) = 0.0;

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

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

% print Arm Coordinates;
fprintf(1, '\n lx[1] = %-7.2f   lx[2] = %-7.2f   lx[3] = %-7.2f', lx(1), lx(2), lx(3));
fprintf(1, '\n ly[1] = %-7.2f   ly[2] = %-7.2f   ly[3] = %-7.2f', ly(1), ly(2), ly(3));
fprintf(1, '\n');

hf = figure('color','white');
% maximize(hf); %Once you run this program sucessfully
                % uncomment this function, if u have 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('Robotic Arm','Color',[.6 0 0])

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

% Plot the robot arm components one by one:
 plot(lx(1:2), ly(1:2), 'color', [.4 .4 .8],'LineWidth',3); % Draw Link-1
 plot(lx(2:3), ly(2:3), 'color', [.8 .4 .8],'LineWidth',3); % Draw Link-2

 % Mark the three points in the given color [R,G,B] and given "size"
 plot(lx(1),ly(1),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 1 .63], 'MarkerSize',6);
 plot(lx(2),ly(2),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[.99 1 .63], 'MarkerSize',6);
 plot(lx(3),ly(3),'--mo', 'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 0 .63], 'MarkerSize',6);

 % Prepare the legend
 str_1 = sprintf(' L1 = %06.2f mm', L1);
 str_2 = sprintf(' L2 = %06.2f mm', L2);
 str_3 = sprintf(' [ lx(1) = %06.2f,  ly(1) = %06.2f ]', lx(1),ly(1));
 str_4 = sprintf(' [ lx(2) = %06.2f,  ly(2) = %06.2f ]', lx(2),ly(2));
 str_5 = sprintf(' [ lx(3) = %06.2f,  ly(3) = %06.2f ]', lx(3),ly(3));
 legend(str_1, str_2, str_3, str_4, str_5); % Show the Legend

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

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 lx[1] = 0.00      lx[2] = 0.00      lx[3] = -140.00
 ly[1] = 0.00      ly[2] = 110.00    ly[3] = 110.00