%
%  This Program demonstrates the use of the function isreachable:
%  function [ reachabilityFlag ] = isReachable( dx, dy )
%
%  The function is called about 90,000 times
%  so this program takes considerable amount of time to execute.
%
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;

% maximum_x_limit =   SUM_L1_L2;
% maximum_y_limit =   SUM_L1_L2;
% minimum_x_limit = - SUM_L1_L2;
% minimum_y_limit = - SUM_L1_L2;

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;

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

               if isReachable( dx, dy ) == 1
                  I(row, col) = 1;
               end
          dx = dx + 1;
    end
    dy = dy - 1;
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');