function  [sys, x0]  = car2_plot(t,x,u,flag,parametri)
%SFUNXY an S-function which acts an X-Y scope using MATLAB's plotting functions.
%
%       This M-file is designed to be used in a SIMULAB S-function block.
%       It stores the last input point using discrete states 
%    	and then plots this against the most recent input. 
%
%	Set this M-file up in an S-function block with a MUX with 3
%	inputs feeding into this block. Set the function parameter
%	up as a four element vector which defines the axis of the graph.
%
%	

%	Copyright (c) 1991 by the MathWorks, Inc.
%	Andrew Grace 5-30-91.
%	Modificato da :
%	Stefano Panzieri 7-15-92.

sample=0.01;

if abs(flag) == 2   	% A real time hit 

       	figure(1)
    	%hold on
        
        [Xc1,Yc1]=get_car_line(u(1:3));
        [Xc2,Yc2]=get_car_line(u(4:6));
        
      	axis(parametri)			% Set up the axis
        axis('square')
        %grid
		%hold off
        h_cars=get(gca,'Userdata');
        tail1=h_cars(1);
        car1=h_cars(2);
        tail2=h_cars(3);
        car2=h_cars(4);
        carf1=h_cars(5);
        text1=h_cars(6);
                
        x_data1=[get(tail1,'xdata') u(1)];
        y_data1=[get(tail1,'ydata') u(2)];

        x_data2=[get(tail2,'xdata') u(4)];
        y_data2=[get(tail2,'ydata') u(5)];

        set(tail1,'xdata',x_data1,'ydata',y_data1);
        set(car1,'xdata',Xc1,'ydata',Yc1);
        set(carf1,'xdata',Xc1,'ydata',Yc1);
        set(text1,'position',[u(1)+0.3 u(2)+0.3],'visible','on');
        
        set(tail2,'xdata',x_data2,'ydata',y_data2);
        set(car2,'xdata',Xc2,'ydata',Yc2);

        
        pause(0)
        drawnow

		sys = [ 0 ];         % Stato successivo




elseif flag  == 0, 	% Initialization
        sys = [0;1;0;6;0;0]; 	% System sizes - 1 discrete states, 3 inputs
        x0 = [Inf]; 	% Flag to indicate first point
    
        % Initialize graph
        figure(1),clf
    	hold on
      	axis(parametri)			% Set up the axis
      	axis('square')
        %grid
        
        tail1=line('color','b','LineStyle',':','erase','none','xdata',[],'ydata',[]);
        car1=line('color','b','LineStyle','-','erase','none','xdata',[],'ydata',[]);
        carf1=fill([0 0 0],[0 0 0],'b');
        set(carf1,'erase','none');
        text1=text(0,0,'target');
        set(text1,'erase','none','visible','off');
        
        tail2=line('color','r','LineStyle',':','erase','none','xdata',[],'ydata',[]);
        car2=line('color','r','LineStyle','-','erase','none','xdata',[],'ydata',[]);
        set(gca,'Userdata',[tail1 car1 tail2 car2 carf1 text1]);
		hold off
        pause(0)
        drawnow




elseif flag == 4,  % Next discrete update
	
       	%sys=ceil(t/sample+sample/1e8)*sample;
        sys=t+sample;

else
        sys = [];
end
