function render(Constr,varargin)
%RENDER sets the vertices, X and Y data properties of the patch and markers.

%   Author(s): N. Hickey, A. Stothert
%   Copyright 1986-2004 The MathWorks, Inc. 
%   $Revision: 1.1.6.3 $  $Date: 2004/12/26 21:51:46 $

HostAx = handle(Constr.Parent);
HostFig = HostAx.Parent;
Xlim = HostAx.Xlim;   Xextent = Xlim(2)-Xlim(1);
Ylim = HostAx.Ylim;   Yextent = Ylim(2)-Ylim(1);

if ~Constr.Activated
   % Preset for Activated->1: initialize graphics
   % Construct the constraint patch
   Patch = patch( ...
      'Parent', double(Constr), ...
      'XlimInclude','off',...
      'YlimInclude','off',...        
      'LineStyle', 'none', ...
      'CDataMapping','Direct', ...
      'FaceColor', Constr.PatchColor, ...
      'FaceAlpha', 0.75, ...
      'HelpTopicKey','settlingtimeconstraint',...
      'UIContextMenu', Constr.addmenu(HostFig),...
      'ButtonDownFcn', Constr.ButtonDownFcn, ...
      'Tag','ConstraintPatch');
   
   % Constraint 'inside edge'
   EdgeInfeasible = line(...
      'Parent', double(Constr), ...
      'Color', Constr.EdgeColor, ...
      'LineWidth', 2, ...
      'Tag','ConstraintInfeasibleEdge', ...
      'Visible','on',...
      'XlimInclude','on',...
      'YlimInclude','on',...
      'HitTest','on',...
      'ButtonDownFcn', Constr.ButtonDownFcn);
   
   % Construct the constraint patch end markers
   Props = struct(...
      'Parent', double(Constr),...
      'XlimInclude','off',...
      'YlimInclude','off',...        
      'LineStyle','none', ...
      'Marker','s', ...
      'MarkerSize',4, ... 
      'MarkerFaceColor','k', ...
      'MarkerEdgeColor','k', ...
      'Visible',Constr.Selected,...
      'Tag', 'ConstraintMarkers',...
      'HitTest','off');
   Markers = [line(Props);line(Props)];
   
end

% Constraint exp(Re(p)*st)<0.02
if Constr.Ts,
   % Discrete time: for z=rho*exp(i*theta), Re(p)=log(rho)/Ts<alpha -> rho_crit = exp(Ts*alpha)
   rho = Constr.geometry;
   theta = -pi:2*pi/(80-1):pi;
   drho = 2 * min(Xextent,Yextent);
   % Construct new X, Y and Z data for the patch
   nP = length(theta);
   X = cos(theta);   Y = sin(theta);
   if strcmp(Constr.Type,'upper')
      %Infeasible region is 'outside' polygon
      PatchXData = [rho*X , (rho+drho)*fliplr(X)];
      PatchYData = [rho*Y , (rho+drho)*fliplr(Y)];
   else
      % Infeasible region is 'inside' polygon
      PatchXData = rho*X;
      PatchYData = rho*Y;
      nP = floor(nP/2);
   end
   
      % Plot left and right constraint selection markers in new position
   hChildren = Constr.Children;
   Tags = get(hChildren,'Tag');
   idx = strcmp(Tags,'ConstraintInfeasibleEdge');
   set(hChildren(idx),'XData',rho*X,'YData',rho*Y,...
      'ZData',Constr.Zlevel(ones(size(X)))+0.1,...
      'Color',Constr.EdgeColor,...
      'LineWidth', Constr.Weight(1)*2+eps)
   idx = strcmp(Tags,'ConstraintMarkers');
   if max(abs(Xlim))>Yextent/2,
      set(hChildren(idx),'XData',[-rho rho],'YData',[0 0],'ZData',Constr.Zlevel([1 1])+0.1);
   else
      set(hChildren(idx),'XData',[0 0],'YData',[-rho rho],'ZData',Constr.Zlevel([1 1])+0.1);
   end
else
   % Continuous time: vertical line x = alpha
   alpha = Constr.geometry;
   dX = Xlim(2)*(1+0.1*sign(Xlim(2)));
   % Construct new X, Y and Z data for the patch
   nP = 2;
   PatchXData = [alpha alpha dX dX];
   YWidth = [Ylim(1).*(1-0.1*sign(Ylim(1))), Ylim(2).*(1+0.1*sign(Ylim(2)))];
   PatchYData = [YWidth  fliplr(YWidth)];
   
   hChildren = Constr.Children;
   Tags = get(hChildren,'Tag');
   % Plot left and right constraint selection markers in new position
   idx = strcmp(Tags,'ConstraintInfeasibleEdge');
   set(hChildren(idx),'XData',[alpha alpha],'YData',Ylim,...
      'ZData',Constr.Zlevel([1 1])+0.1,...
      'Color',Constr.EdgeColor,...
      'LineWidth', Constr.Weight(1)*2+eps)
   idx = strcmp(Tags,'ConstraintMarkers');
   set(hChildren(idx),'XData',[alpha alpha],'YData',Ylim,'ZData',Constr.Zlevel([1 1])+0.1)
end

% Set patch parameters
idx = strcmp(Tags,'ConstraintPatch');
Vertices = [PatchXData(:) PatchYData(:) Constr.Zlevel(ones(2*nP,1),:)];
Faces = [1:nP-1 ; 2:nP ; 2*nP-1:-1:nP+1 ; 2*nP:-1:nP+2]';
set(hChildren(idx),'Faces',Faces,'Vertices',Vertices, ...
   'FaceColor',Constr.PatchColor)

