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

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

% Get axes info
HostAx  = handle(Constr.Parent);
HostFig = HostAx.Parent;

if ~Constr.Activated
   % Initialize when constraint is not activated yet (preset for Activated=1)
   % Construct the constraint patch
   % Construct the constraint patch
   Patch = patch( ...
      'Parent', double(Constr), ...
      'XlimInclude','off',...
      'YlimInclude','off',...
      'LineWidth', 2, ....
      'LineStyle', 'none', ...
      'CDataMapping', 'Direct', ...
      'FaceColor', Constr.PatchColor, ...
      'FaceAlpha', 0.75, ...
      'HelpTopicKey', 'gainmarginconstraint', ...
      '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','off',...
      'YlimInclude','off',...
      'HitTest', 'on',...
      'ButtonDownFcn',Constr.ButtonDownFcn);

   % Construct the constraint patch end markers
   Markers = line(...
      'Parent', double(Constr),...
      'XlimInclude','off',...
      'YlimInclude','off',...
      'LineStyle','none', ...
      'Marker','s', ...
      'MarkerSize',4, ...
      'MarkerFaceColor','k', ...
      'MarkerEdgeColor','k', ...
      'HitTest','on',...
      'Visible',Constr.Selected,...
      'ButtonDownFcn',Constr.ButtonDownFcn,...
      'Tag', 'ConstraintMarkers');
end

% Get margin info
Gain   = Constr.PeakGain;  % in dB
Origin = Constr.OriginPha; % in deg

% Generate isogain lines for following gain values:
gain = Gain * [1 3];

% Phase points
phase = logspace(-3, log10(180), 35); % evently spaced points using approx.
phase = [phase, fliplr(360-phase(1:end-1))];

% Set number of points in rendered line
nP = length(phase);

% Convert data to open-loop gain and phase values
[g,p] = meshgrid(10.^(gain/20), (pi/180)*phase);  % mesh in H/(1+H) plane
z = g .* exp(j*p);
H = z ./ (1-z);

% Create a multi-point line for curved constraint boundaries
Angles = rem((180/pi)*angle(H(:)) + 360, 360) - 180; 
i90 = find(Angles>-90);
nM = max(30,i90(1));     % Marker position index
XData = Origin + Angles; % Phase in deg
YData = 20*log10(abs(H(:)));  % Magnitude in dB

XData = unitconv(XData, Constr.PhaseUnits, Constr.xDisplayUnits);
YData = unitconv(YData, Constr.MagnitudeUnits, Constr.yDisplayUnits);

XLims = HostAx.Xlim;
YLims = HostAx.Ylim;
% Construct new X, Y and Z data for the patch
if (Gain <= 0)
    Xright  = XLims(2)*(1+0.5*sign(XLims(2)))+0.01*diff(XLims);
    Xleft = XLims(1)*(1-0.5*sign(XLims(1)))-0.01*diff(XLims);
    Ytop   = YLims(2)*(1+0.5*sign(YLims(2)))+0.01*diff(YLims);
    PatchXData = [...
       XData(1:nP); ...
       Xright; ...
       Xright; ...
       Xleft; ...
       Xleft];
    PatchYData = [...
       YData(1:nP); ...
       YData(nP); ...
       Ytop; ...
       Ytop; ...
       YData(1)];
    nPPatch = nP+4;
else
    PatchXData = XData(1:nP);
    PatchYData = YData(1:nP);
    nPPatch = nP;
end
PatchZData = Constr.Zlevel(ones(nPPatch,1),:);

hChildren = Constr.Children;
Tags = get(hChildren,'Tag');

% Set the new patch data values
Vertices = [PatchXData(:) PatchYData(:) PatchZData(:)];
Faces = 1:nPPatch;
idx = strcmp(Tags,'ConstraintPatch');
set(hChildren(idx), 'Faces', Faces, 'Vertices', Vertices);

% Plot left and right constraint selection markers in new position
idx = strcmp(Tags,'ConstraintInfeasibleEdge');
set(hChildren(idx),...
   'XData', PatchXData,...
   'YData', PatchYData, ...
   'Zdata',Constr.Zlevel(ones(size(PatchXData)))+0.1,...
   'Color',Constr.EdgeColor, ...
   'LineWidth', Constr.Weight(1)*2+eps, ...
   'visible', 'on')
idx = strcmp(Tags,'ConstraintMarkers');
nM = [nM, nP-nM+1]; %marker positions
set(hChildren(idx), ...
   'XData', XData(nM), ...
   'YData', YData(nM), ...
   'Zdata', Constr.Zlevel(ones(size(nM)))+0.1);


