% Input:
% results: Matrix (evaluations of support functions for directions L)
% omegas: cell Array (support functions to plot) -> if specified, the
% results input is ignored
% L: Matrix
% x,y: Int (dimensions to plot)
% from,to: Int (plot flowpipe from "from" to "to")
% intersectionindices: vector (indices in the vector will be plottet in
%                              another color)
% stepsize: int (plot every "stepsize" set in the flowpipe)
% handle: figure to handle for the figure to plot in
% fixpointindices: vector containg the indices of sets for which fixpoints
%                  have been detected

% Output:
% results: Matrix (may contain additional evaluations if these had to be computed during plot)

% Description:
% See plotanalysisEXT_animated. The difference is that plotanalysisEXT does
% not contain any drawnow() calls.

% Beschreibung:
% Siehe plotanalysisEXT_animated. plotanalysisEXT verwendet im Gegensatz zu
% plotanalysisEXT_animated nicht den drawnow() Befehl.

function [results,time] =  plotanalysisEXT(results,omegas,L,x,y,from,to,intersectionindices,stepsize,handle,fixpointindices)

hold(handle, 'on');
set(handle,'XGrid','on');
set(handle,'YGrid','on');
axis(handle,'square');

start = tic();

N = to - from+1;

% evaluation necessary?
if  isequal(results,[])
    fprintf('The support function will be evaluated during plot.\n');
    evaluate = true;
    results = zeros(size(L,1),N);
else
    evaluate = false;
end;

dim = size(L,2);
% Chose direction vectors which are really inside the plane to display
[L,results] = selectDirectionsInPlane(L,results,x,y);
[L,results] = sortL(L,results,x,y);
 
extendedL = [L zeros(size(L,1),dim-size(L,2))]; % only used to evaluate in case of function handle representation -> all field cutted of before are 0

%draw direction vectors (in blue)
for i=1:1:size(L,1)
    plot(handle, [0 L(i,1)],[ 0 L(i,2)],'Color',[0 0 1],'LineStyle','-');
end;

fprintf('Plot flowpipes ... ');
% Plot the computed sets

for i= from:stepsize:to

    if  evaluate% evaluation necessary?
        results(:,i-from+1) = sptotemplatepolyhedra(omegas{i},extendedL);
    end;

    % chose color
    if any(intersectionindices == i) 
        
    else        
        color = [(i-from)*(1/N) 1-(1/N)*(i-from) 0];
        
        % plot set
        plotconvexsetEXT(L,results(:,i-from+1),color,handle);
    end;
end;

% plot last set if not already plotted and this set is no intersection
if ~isempty(i) && ~isempty(intersectionindices) && i ~= to && isempty(to == intersectionindices)  
    plotconvexsetEXT(L,results(:,to-from+1),[1 0 0],handle);
end;
fprintf('\t done. \n');
fprintf('Plot reachable set for intersection ... ');
% plot intersections (orange)
color = [1 0.788 0.055];
for i=1:1:length(intersectionindices)
    
    if  evaluate% evaluation necessary?
        results(:,intersectionindices(i)-from+1) = sptotemplatepolyhedra(omegas{intersectionindices(i)},extendedL);
    end;
         
    % plot set
    plotconvexsetEXT(L,results(:,intersectionindices(i)-from+1),color,handle);  
   
end;
fprintf('\t done. \n');
fprintf('Plot reachable set for fixpoints ... ');
color = [69 255 246]./256;
% Schnitte zeichnen (violet)
%color = [0.4 0.11 0.68];
for i=1:1:length(fixpointindices)
    
    if  evaluate% evaluation necessary ?
        results(:,fixpointindices(i)-from+1) = sptotemplatepolyhedra(omegas{fixpointindices(i)},extendedL);
    end;
          
    % plot set
    plotconvexsetEXT(L,results(:,fixpointindices(i)-from+1),color,handle);
    
end;
fprintf('\t done. \n');
time = toc(start);
fprintf('Plot in %f\n\n', time);
hold(handle, 'off');