% 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 (containing row-wise direction vectors)
% 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:
% Plots the reachable sets from "from" to "to" in dimensions x and y to figure handle if results is not empty.
% Otherwise the sets from omegas are evaluated to. The first set starts
% green and the color changes to red with time proceeding. Sets with
% indices in intersectionindices are plotted in orange color. Sets with
% indices in fixpointindices are plotted in turquoise color.

% Beschreibung:
% Plottet die erreichbaren Mengen zwischen from und to (repräsentiert durch results und L) in
% den Dimensionen x und y in der Figur handle mit Frabverlauf von grün nach
% rot (rot = zuletzt erreichbare Menge) und gibt die Menge der
% Auswertungen(Input) results ggnf. um neue Auswertungen ergänzt zurück
% Die Mengen mit indizes in intersectionindices werden orange gefärbt.
% Die Mengen mit indizes in fixpointindices werden türkis gefärbt.

function [results,time] =  plotanalysisEXT_animated(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  isempty(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);
        drawnow;
    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);
    drawnow;
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);
    drawnow;
    
end;
fprintf('\t done. \n');
fprintf('Plot reachable set for fixpoints ... ');
color = [69 255 246]./256;
% plot intersections
%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);
    drawnow;
end;
fprintf('\t done. \n');
time = toc(start);
fprintf('Plot in %f\n\n', time);
hold(handle, 'off');