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

%   Author(s): P. Gahinet
%   Revised: A. Stothert
%   Copyright 1986-2004 The MathWorks, Inc. 
%   $Revision: 1.1.6.3 $  $Date: 2004/12/26 21:51:35 $

HostAx = handle(Constr.Parent);
HostFig = HostAx.Parent;
Xlim = get(HostAx,'Xlim');   
Ylim = get(HostAx,'Ylim');

if ~Constr.Activated
   % Initialize when constraint is not activated yet (preset for Activated=1)
   % Construct the constraint patch
   Props = struct(...
      'Parent', double(Constr), ...
      'XlimInclude','off',...
      'YlimInclude','off',... 
      'HitTest','on',...
      'LineStyle', 'none', ...
      'CDataMapping','Direct', ...
      'FaceColor', Constr.PatchColor, ...
      'FaceAlpha', 0.75, ...
      'HelpTopicKey','dampingratioconstraint',...
      'UIContextMenu', Constr.addmenu(HostFig),...
      'ButtonDownFcn', {Constr.ButtonDownFcn},...
      'Tag', 'ConstraintPatch');
   %Patch = [patch(Props);patch(Props)];
   Patch = patch(Props);
   
   % REVISIT: consider making edge visible when Z layering works properly
   EdgeInfeasible = line(...
      'Parent', double(Constr), ...
      'Color', Constr.EdgeColor, ...
      'LineWidth', 2, ...
      'Tag','ConstraintInfeasibleEdge', ...
      'Visible','on', ...
      'XlimInclude','off', ...
      'YlimInclude','off', ...
      'ButtonDownFcn', Constr.ButtonDownFcn, ...
      'HitTest','on');
   
   % Construct the constraint end markers
   Props = struct(...
      'Parent', double(Constr),...
      'LineStyle','none', ...
      'Marker','s', ...
      'MarkerSize',4, ... 
      'MarkerFaceColor','k', ...
      'MarkerEdgeColor','k', ...
      'Visible',Constr.Selected,...
      'Tag', 'ConstraintMarkers',...
      'XlimInclude','on',...
      'YlimInclude','on',...
      'HitTest','off');
   Markers = [line(Props);line(Props)]; 
end

% Draw
if Constr.Ts,
   % Discrete time: isodamping lines are 
   %      rho   = exp(-Zeta*t)
   %      theta = +/-sqrt(1-Zeta^2)*t
   % with theta in t in [0,pi/sqrt(1-Zeta^2)]
   zeta0 = Constr.Damping; 
   tmax = pi/sqrt(1-min(.99,zeta0)^2);
   t = tmax * (0:1/64:1);
   Z0 = exp((-zeta0 + 1i * sqrt(1-zeta0^2))*t);
   
   % Construct X, Y data for the patch
   if strcmp(Constr.Type,'upper')
      %Invalid region is 'inside' polygon
      PatchXData = [real(Z0) fliplr(real(Z0))];
      PatchYData = [imag(Z0) -1*fliplr(imag(Z0))];
   else
      %Invalid region is 'outside' polygon
      Xleft = Xlim(1); Xright = Xlim(2);
      Ybot = Ylim(1); Ytop = Ylim(2);
      Corners = [Xleft Ybot; Xleft Ytop; ...
         Xright Ytop; Xright Ybot; ...
         Xleft Ybot]';
      PatchXData = [real(Z0) Corners(1,:) fliplr(real(Z0))];
      PatchYData = [imag(Z0) Corners(2,:) -1*fliplr(imag(Z0))];
   end
   
   % Plot left and right constraint selection markers in new position
   hChildren = Constr.Children;
   Tags = get(hChildren,'Tag');
   
   Xm = -exp(-zeta0*tmax);
   Ze = [Z0 fliplr(conj(Z0))];
   idx = strcmp(Tags,'ConstraintInfeasibleEdge');
   set(hChildren(idx),'XData',real(Ze),'YData',imag(Ze),...
      'Zdata',Constr.Zlevel(ones(size(Ze)))+0.1, ...
      'Color',Constr.EdgeColor, ...
      'LineWidth', Constr.Weight(1)*2+eps);
   idx = strcmp(Tags,'ConstraintMarkers');
   set(hChildren(idx), 'XData',[Xm 1],'YData',[0 0],'Zdata',Constr.Zlevel([1 1])+0.1);
else
   % Continuous time: ray  x = -Zeta*t, y = sqrt(1-Zeta^2)*t
   rho = 2*(max(1,abs(Xlim(1)))+max(1,abs(Ylim(1))));
   c = Constr.Damping;   s = sqrt(1-c^2);
   X = -rho*c;   Y = rho*s;
   Xm = 0.25*X; Ym = 0.25*Y;
   
   % Construct X, Y data for the patch
   Xright = Xlim(2)*(1+0.1*sign(Xlim(2)));
   Ytop   = Ylim(2)*(1+0.1*sign(Ylim(2)));
   Ybot   = Ylim(1)*(1-0.1*sign(Ylim(1)));
   PatchXData = [0 X X    Xright Xright X     X 0];
   PatchYData = [0 Y Ytop Ytop   Ybot   Ybot -Y 0 ];
   
   % Plot left and right constraint selection markers in new position
   hChildren = Constr.Children;
   Tags = get(hChildren,'Tag');
   % Plot left and right constraint selection markers in new position
   XData = [0 X nan; X 0 nan]';
   YData = [0 Y nan; -Y 0 nan]';
   idx = strcmp(Tags,'ConstraintInfeasibleEdge');
   set(hChildren(idx),'XData',XData(:),'YData', YData(:), ...
      'ZData',Constr.Zlevel(ones(size(XData(:))))+0.1,...
      'Color',Constr.EdgeColor, ...
      'LineWidth', Constr.Weight(1)*2+eps)
   idx = strcmp(Tags,'ConstraintMarkers');
   set(hChildren(idx),'XData',[Xm Xm],'YData',[Ym -Ym],'ZData',Constr.Zlevel([1 1])+0.1,...
      'Visible',Constr.Selected)
end

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


