function [newnext]=nextwaypoint(next,EWP,NWP,EA,NA)
% function [newnext]=nextwaypoint(next,EWP,NWP,EA,NA)
% Function to determine if the aircraft has passed the 
% waypoint with index next. If so, next is incremented by 1.
% If not, next is unchanged.

if EWP(next)-EWP(next-1)==0
    % waypoint path line is vertical
    %disp('The waypoint line is vertical')
    m2=0;
    b2=NWP(next)-m2*EWP(next);
    xnextm1=NWP(next-1)-(m2*EWP(next-1)+b2);
    xa=NA-(m2*EA+b2);
    if sign(xnextm1)==sign(xa)
        %disp('Aircraft is in Region 1')
        newnext=next;
    else
        %disp('Aircraft is in Region 2')
        newnext=next+1;
    end
else
    % waypoint path line is not vertical
    if NWP(next)-NWP(next-1)==0
        % waypoint path is horizontal, the perpendicular is vertical
        %disp('The waypoint line is horizontal')
        if sign(EWP(next)-EWP(next-1))==sign(EWP(next)-EA)
            %disp('Aircraft is in Region 1')
            newnext=next;
        else
            %disp('Aircraft is in Region 2')
            newnext=next+1;
        end      
    else
        % waypoint path is not vertical
        m1=(NWP(next)-NWP(next-1))/(EWP(next)-EWP(next-1));
        b1=NWP(next)-m1*EWP(next);
        m2=-1/m1;
        b2=NWP(next)-m2*EWP(next);
        xnextm1=NWP(next-1)-(m2*EWP(next-1)+b2);
        xa=NA-(m2*EA+b2);
        if sign(xnextm1)==sign(xa)
            %disp('Aircraft is in Region 1')
            newnext=next;
        else
            %disp('Aircraft is in Region 2')
            newnext=next+1;
        end
    end
end