function [Maze_Bounds]=Boundary(Maze,Row,Col)
%MAZE ALGORITHM
%CHECK the existence of left boundary
%IF next step is boundary then turn right
%ELSE move forward
Maze_Bounds=[];
Length_Bounds=[];
count=0;
for ii=2:Row-1,
for jj=2:Col-1,
if Maze(ii,jj)==0 && count_neigh(Maze,Row,Col,ii,jj),
flag=1;
getout=0;
Bounds=[ii jj];
dirX=0; dirY=1;%Right (default)
dispX=0; dispY=0;
%Looping for Bounds
while flag==1,
if Maze(ii+dispX+dirX,jj+dispY+dirY)==1,%Right Boundary
Bounds(1+size(Bounds,1),:)=[ii+dispX jj+dispY];
if Bounds(1+getout,:)==Bounds(size(Bounds,1),:),
getout=getout+1;
if getout==2,
count=count+1;
Length_Bounds(count)=length(Bounds(:,1));
Maze_Bounds(1:Length_Bounds(count),2*count-1)=Bounds(:,1);
Maze_Bounds(1:Length_Bounds(count),2*count)=Bounds(:,2);
if Length_Bounds(count)<1, count=count-1; end
break;
end
end
temp=[0 1;-1 0]*[dirX dirY]';
dirX=temp(1); dirY=temp(2);
else
dispX=dispX+dirX;
dispY=dispY+dirY;
temp=[0 -1;1 0]*[dirX dirY]';
bndX=temp(1); bndY=temp(2);
if Maze(ii+dispX+bndX,jj+dispY+bndY)~=1,%Left Boundary
dirX=bndX; dirY=bndY;
Bounds(1+size(Bounds,1),:)=[ii+dispX jj+dispY];
if Bounds(1+getout,:)==Bounds(size(Bounds,1),:),
getout=getout+1;
if getout==2,
count=count+1;
Length_Bounds(count)=length(Bounds(:,1));
Maze_Bounds(1:Length_Bounds(count),2*count-1)=Bounds(:,1);
Maze_Bounds(1:Length_Bounds(count),2*count)=Bounds(:,2);
if Length_Bounds(count)<1, count=count-1; end
break;
end
end
end
end
end
end
if count>0, break; end
end
if count>0, break; end
end
% Maze_Bounds
% Length_Bounds