%
%  This Program demonstrates the use of the function isreachable:
%  function [ reachabilityFlag ] = isReachable( dx, dy )
%
%  In order to reduce the execution time, the variables row and col
%  are incremented by y_step and x_step instead of 1. This considerably
%  increases the execution speed.
%
%
close all; clear; clc;

global debugLevel_01; debugLevel_01 = 1;
global L1;            L1 = 110.0; % Link Length in mm
global L2;            L2 = 140.0; % Link Length in mm
global SUM_L1_L2;     SUM_L1_L2 = L1 + L2;

% hf = figure('color','white');
% maximize(hf);
% axis equal
% axis off

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;


width  =  abs(minimum_x_limit) + abs(maximum_x_limit); % col
height =  abs(minimum_y_limit) + abs(maximum_y_limit); % row

I = zeros(height, width); % zeros( ROW, COL) : Try zeros(3,5) on command line to clarify doubt

debugLevel_01 = 0;

x_step = 4; % col
y_step = 4; % row
dy = maximum_y_limit;
for row =  1  : y_step : height % for all rows in I
    dx = minimum_x_limit;
    for col =  1  : x_step : width % for all cols in I

               if isReachable( dx, dy ) == 1
                  I(row, col) = 1;
               end
          dx = dx + x_step;
    end
    dy = dy - y_step;
end

imshow(I);
title('WHITE color shows the Reachable co-ordinates');
saveas(gcf,'output.jpg')

% The image below can be used as a background image in animation sometime,
% so that we dont have to calculate the envelope again
J=I + 0.9;
imwrite(J,'background_bw.bmp');