% This script computes the areas under the curve for
% a movement from the offspring of the coordinate
% system (0,0) to the upper right (300,600) in four
% different scenarios with very simple geometry.
%
% Scenario 1: Movement in 101 steps (akin to common
%             time-normalized data), going straight
%             up first and then to the right.
% Scenario 2: Movement in 3 steps as in Scenario 1.
% Scenario 3: Movement in 3 steps but going right
%             first and then upward.
% Scenario 4: Zigzag movement with an initial drift
%             up, then to the right (crossing the
%             direct connection between start- and
%             endpoint), and then up again.
%
% The script returs plots of the actual trajectory
% (teal line) and the corresponding foot of the
% dropped perpendicular on the 'ideal' trajectory
% as represented by a straight line from start- to
% endpoint of the trajectory. Overall areas under
% the curve (AUC) are displayed as titles above
% the plots. Areas to the upper left of the line
% are counted as positive whereas areas to the
% lower right of the line are counted as negative.
%
% Note: The functions comp_ad and comp_auc need to
% be located in Matlab's search path to run the
% script!
%
% -------------------------------------------------
%                                    Roland Pfister
%                                        03.08.2016
% -------------------------------------------------

% Define straight line from start- to endpoint
slope = 2;
intercept = 0;

% Preallocate AUC array
aucs = zeros(1,4);

% Clear figure 1 if it exists
if ishandle(1)
    close(1)
end;
figure(1)

for points = 1:4
    
    % Set trajectory points
    if points==1

      xvals = [zeros(1,50),0:6:300];
      yvals = [0:12:600,repmat(600,1,50)];

    elseif points==2
        xvals = [0,0,300];
        yvals = [0,600,600];

    elseif points==3 
      xvals = [0,300,300];
      yvals = [0,0,600];

    elseif points==4
      xvals = [0,0,300,300];
      yvals = [0,300,300,600];

    end;
    %disp(xvals);
    %disp(yvals);

    % Plot trajectory
    subplot(2,2,points);
    plot(xvals,yvals,'co-',[0,300],[0,600],'k--')
    ylim([-100 700]);
    xlim([-50 350]);
    set(gca,'XTick',0:100:300);
    set(gca,'YTick',0:100:600);

    % Get absolute distances (ADs)
    [ad, direction, linecoords] = comp_ad(xvals,yvals,slope,intercept);

    hold on
    plot(linecoords.x,linecoords.y,'ko')
    daspect([1 1 1]) 
    
    % Get AUC
    auc = comp_auc(xvals,yvals,slope,intercept);
    %disp(auc(end));
    aucs(points) = auc(end);
    
    % Display AUC in plot
    title(['AUC = ' num2str(auc(end))]);
    hold off

    % Plot ADs?
    %figure(2)
    %plot(ad)

    %figure(3)
    %plot(ad.*direction)
    
end;
