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.2.2.1 $ $Date: 2005/01/18 16:06:59 $

% Set number of points in rendered line
nP = 5;

% 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
    Patch = patch( ...
       'Parent', double(Constr), ...
       'XlimInclude','off',...
       'YlimInclude','off',...
       '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','on',...
       'YlimInclude','on',...
       '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
Margin = Constr.MarginPha; % in deg
Origin = Constr.OriginPha; % in deg

% Create a multi-point line for curved constraint boundaries
XData = linspace(Origin-Margin, Origin+Margin, nP); % Phase in deg
YData = linspace(0, 0, nP);                         % Magnitude in dB
XData = unitconv(XData, Constr.PhaseUnits, Constr.xDisplayUnits);
YData = unitconv(YData, Constr.MagnitudeUnits, Constr.yDisplayUnits);

% Thickness is already in YScale units so do not reconvert
YLims = HostAx.Ylim;
if strcmp(HostAx.Yscale, 'linear')
    YDataH = YData + (YLims(2) - YLims(1)) / 2 * 0.01;
    YDataL = YData - (YLims(2) - YLims(1)) / 2 * 0.01;
else  % YScale is log
    YDataH = YData * (YLims(2) / YLims(1))^(0.01 / 2);
    YDataL = YData / (YLims(2) / YLims(1))^(0.01 / 2);
end

% Construct new X, Y and Z data for the patch
PatchXData = [XData fliplr(XData) XData];
PatchYData = [YDataH YData YDataL];
PatchZData = Constr.Zlevel(ones(3*nP,1),:);

% Set the new patch data values
Vertices = [PatchXData(:) PatchYData(:) PatchZData(:)];
Faces1 = [1:nP-1 ; 2:nP ; 2*nP-1:-1:nP+1 ; 2*nP:-1:nP+2]';
Faces2 = [nP+1:2*nP-1 ; nP+2:2*nP ; 3*nP-1:-1:2*nP+1 ; 3*nP:-1:2*nP+2]';
Faces = [Faces1 ; Faces2];

hChildren = Constr.Children;
Tags = get(hChildren,'Tag');
idx = strcmp(Tags,'ConstraintPatch');
set(hChildren(idx), 'Faces', Faces, 'Vertices', Vertices, ...
   'FaceColor',Constr.PatchColor);

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