function [NeighReturn]=getLatNeigh(vert,Visited,binary,r,c)
% This heavily unoptimized function finds the neighbors that have a 
% connection to the current vertex 'vert'. Because of the stack data type's
% last in, first out condition, and we want to primarily move "up", the
% order in which the vertices are checked becomes very important.
%
% There probably is a built in function that can do this more efficiently.
%
% INPUTS
%    vert = The current vertex, to find adjacent connected neighbors to
%          continue the path
% Visited = The global variable Visited, which is passed in to check
%           against so visited vertices are not added to 'NeighReturn'
%  binary = r x c size lattice which details the pairs of adjacent indices 
%          that are connected
%       r = Number of horizontal rows in the rectangular lattice
%       c = Number of vertical columns in the rectangular lattice
%
% OUTPUTS
% NeighReturn = The array of vertices returned that are connected to the
%               input vertex vert and have not been visited already
%
% ===========================================================

Neigh = [];

%Determine if each pair (vert1,vert2) for "constant" input vert1 and vert2
%for the vertices immediately around vert1 (e.g. 24,26,15,35 for vert1=25 

%Test the vertex in the row below it
if vert-c <= 0 
    %If the new vertex is not in the array, do nothing
else
   if (find(binary(vert,(vert-c))) == 1)
        if any(Visited==(vert-c));
        else
            Neigh = cat(2,Neigh,vert-c);
        end
   elseif(find(binary(vert-c,vert)) == 1)
        if any(Visited==(vert-c));
        else
            Neigh = cat(2,Neigh,vert-c);
        end
   end
end
                                            
%Test the vertex in the column the left of it
if vert-1 <= 0 
    %If the new vertex is not in the array, do nothing
else
   if (find(binary(vert,(vert-1))) == 1) 
        if any(Visited==(vert-1));
        else
            Neigh = cat(2,Neigh,vert-1);
        end
   elseif (find(binary(vert-1,vert)) == 1)
        if any(Visited==(vert-1));
        else
            Neigh = cat(2,Neigh,vert-1);
        end
   end   
end

%Test the vertex in the column to the right
if vert+1 > r*c 
    %If the new vertex is not in the array, do nothing
else
    if (find(binary(vert,(vert+1))) == 1) 
        if any(Visited==(vert+1));
        else
            Neigh = cat(2,Neigh,vert+1);
        end
    elseif(find(binary(vert+1,vert)) == 1)
        if any(Visited==(vert+1));
        else
            Neigh = cat(2,Neigh,vert+1);
        end
    end
end

%Test the vertex in the row above it
if vert+c > r*c
    %If the new vertex is not in the array, do nothing
else
   if (find(binary(vert,(vert+c))) == 1)
        if any(Visited==(vert+c));
        else
            Neigh = cat(2,Neigh,vert+c);
        end
   elseif(find(binary(vert+c,vert)) == 1)
        if any(Visited==(vert+c));
        else
            Neigh = cat(2,Neigh,vert+c);
        end
   end
end

NeighReturn = Neigh;

end
