function coveragePath = coveragePath(intersectionPoints,unitlength,overlap)
%COMPUTECOVERAGEPATH compute clockwise forward sweep normal path.
%#codegen

%   Copyright 2022 The MathWorks, Inc.

%Compute padding. Displacement of path from edges of polygon.
padding=unitlength*(1-overlap)/2;
pathIndex=1;
rowIndex=1;
coveragePath=intersectionPoints;
while (pathIndex<size(intersectionPoints,1))
    if((2*padding)>=abs(intersectionPoints(pathIndex,1)-intersectionPoints(pathIndex+1,1)))
        %Edge case polygon thickness less than the path length in this
        %section. Don't displace 
        offset=0;
    else
        offset=padding;
    end

    if(mod(rowIndex,2)==1)
        %Swap in a left to right sweep if indices not in order.
        if(intersectionPoints(pathIndex,1)>intersectionPoints(pathIndex+1,1))
            coveragePath(pathIndex+1,:)=intersectionPoints(pathIndex,:);
            coveragePath(pathIndex,:)=intersectionPoints(pathIndex+1,:);
        end
        %Add padding.
        coveragePath(pathIndex,1)=coveragePath(pathIndex,1)+offset;
        coveragePath(pathIndex+1,1)=coveragePath(pathIndex+1,1)-offset;
    elseif(mod(rowIndex,2)==0)
        %Swap in a right to left sweep if indices not in order.
        if(intersectionPoints(pathIndex,1)<intersectionPoints(pathIndex+1))
            coveragePath(pathIndex+1,:)=intersectionPoints(pathIndex,:);
            coveragePath(pathIndex,:)=intersectionPoints(pathIndex+1,:);
        end
        %Offset from ends.
        coveragePath(pathIndex,1)=coveragePath(pathIndex,1)-offset;
        coveragePath(pathIndex+1,1)=coveragePath(pathIndex+1,1)+offset;
    end
    %Increment counters
    rowIndex=rowIndex+1;
    pathIndex=pathIndex+2;
end
end



