function [landward_xintersect seaward_xintersect islandwidth] = getIntersections(varargin)

OPT = struct(...
    'ncol', 100,...
    'profile1', 1,...
    'profile2', 1,...
    'xas', 1,...
    'orientation','LS');

OPT = setproperty(OPT, varargin{:});

C = OPT.profile1 - OPT.profile2;

% Sloping upward from left to right
pospoints = (C>=0);
[underest row] = find(diff(pospoints)==-1);
ninter = size(underest,1);

% second step

for i = 1:ninter
    xinter = linspace(OPT.xas(underest(i,1)),OPT.xas(underest(i,1)+1),OPT.ncol);
    zinter = linspace(OPT.profile2(underest(i,1)),OPT.profile2(underest(i,1)+1),OPT.ncol);

    temp = OPT.profile1(1,1)*ones(1,length(xinter));
    temp = (temp - zinter);
    pospoints = temp>=0;

    [row xint] = find(diff(pospoints)==-1,1);
    
    xintersect_up(i) = xinter(xint);
    zintersect_up(i) = zinter(xint);
end

% Sloping downward from left to right.
negpoints = (C<0);
[upperest row] = find(diff(negpoints)==-1);

ninter = size(upperest,1);
for i = 1:ninter
    xinter = linspace(OPT.xas(upperest(i,1)),OPT.xas(upperest(i,1)+1),OPT.ncol);
    zinter = linspace(OPT.profile2(upperest(i,1)),OPT.profile2(upperest(i,1)+1),OPT.ncol);

    temp = OPT.profile1(1,1)*ones(1,length(xinter));
    temp = (temp - zinter);
    negpoints = temp<=0;

    [row xint] = find(diff(negpoints)==-1,1);
    
    xintersect_down(i) = xinter(xint);
    zintersect_down(i) = zinter(xint);
end

if OPT.orientation == 'LS';
   landward_xintersect = xintersect_up(1);
   seaward_xintersect = xintersect_down(end);
else if OPT.orientation == 'SL';
   seaward_xintersect = xintersect_up(1);
   landward_xintersect = xintersect_down(end);
    end
end

islandwidth = abs(landward_xintersect - seaward_xintersect);


end
