function grid_els = LEM_grid_els2(grid, pt1, pt2, stepCounter, error)
% LEM_grid_els2 - find grid elements traversed from pt1 to pt2
% On input:
%     grid (grid struct): defines grid
%       .x_min (float): x min for grid
%       .y_min (float): y min for grid
%       .x_max (float): x max for grid
%       .y_max (float): y max for grid
%       .del_x (float): x axis spacing
%       .del_y (float): y axis spacing
%     pt1 (1x3 vector): 3D point
%     pt2 (1x3 vector): 3D point
%     stepCounter (float): The amount of steps along the path (can't be
%       less then 1). 
%     error (float): the amount of error around the path crossing the
%     cells.
% on output:
%     grid_els (vector struct): linear numbers of each cell the line
%     passes through.

% Author:
%     T. Henderson
%     Vista Marston
%     UU
%     Spring 2020
%

% Variables needed
y_min = grid.y_min;
y_max = grid.y_max;
del_x = grid.del_x;
del_y = grid.del_y;
xSteps;
ySteps = 0;
currentPt = pt1;
grid_els = [];
temp = [];
counter = 0;
linearNumber = -1;
totalRow = ((y_max - y_min)/del_y) + y_min;

% Check the slop of the line
if (pt2(1) - pt1(1) == 0)
    xSteps = 0;
    ySteps = (pt2(2) - pt1(2))/stepCounter;
else
    slope = (pt2(2) - pt1(2))/(pt2(1) - pt1(1));
    xSteps = (pt2(1) - pt1(1))/stepCounter;
    ySteps = xSteps * slope;
end

if (pt1 == pt2)
    linearNumber = LEM_find_linearNumber(currentPt, totalRow, del_y, del_x);
    temp = linearNumber;
else
    % Walk along the line
    while counter <= stepCounter
       checkX = mod(currentPt(1),del_x);
       checkY = mod(currentPt(2), del_y);
       if (currentPt == pt2)
           linearNumber = LEM_find_linearNumber(currentPt, totalRow, del_y, del_x);
           temp = linearNumber;
           break;
       end
       % Check to see if it is in the interception
       if ((checkX <= error || (del_x - checkX) <= error) && ...
               (checkY <= error || (del_y - checkY) <= error))
           % Add adjacent areas
           linearNumber1 = LEM_find_linearNumber([currentPt(1) + error, ...
               currentPt(2) + error], totalRow, del_y, del_x);
           linearNumber2 = LEM_find_linearNumber([currentPt(1) + error, ...
               currentPt(2) - error], totalRow, del_y, del_x);
           linearNumber3 = LEM_find_linearNumber([currentPt(1) - error, ...
               currentPt(2) + error], totalRow, del_y, del_x);
           linearNumber4 = LEM_find_linearNumber([currentPt(1) - error, ...
               currentPt(2) - error], totalRow, del_y, del_x);
           temp = [temp, linearNumber1, linearNumber2, ...
               linearNumber3, linearNumber4];
       else
           % Find Current Row and Column
           lNCheck = linearNumber;
           linearNumber = LEM_find_linearNumber(currentPt, totalRow, del_y, del_x);
           if not(lNCheck == linearNumber)
                temp = [temp, linearNumber];
           end
       end
       % Move Along the line
       currentPt(1) = currentPt(1) + xSteps;
       currentPt(2) = currentPt(2) + ySteps;
       counter = counter + 1;
    end
end

% Double check there is no duplicates
grid_els = unique(temp);
end
