function equation(varargin)
%EQUATION  Draw an equation

%   Author(s): A. DiVergilio
%   Copyright 1986-2002 The MathWorks, Inc.
%   $Revision: 1.8 $  $Date: 2002/04/10 06:41:45 $

%---Default properties
p = struct(...
   'Parent',[],...
   'Position',[1 1],...
   'LineWidth',0.5,...
   'Color','k',...
   'FontName','',...
   'FontSize',12,...
   'FontWeight','normal',...
   'Name','',...
   'Gain','',...
   'Gain2','',...
   'Numerator','',...
   'Denominator','',...
   'Anchor','rcenter',...
   'Bracket',0,...
   'Sigma',0,...
   'SigmaBounds',{{'',''}},...
   'Interpreter','tex',...
   'Tag','',...
   'Clipping','off');
plist = fieldnames(p);

%---Merge user-specified properties
for i=1:2:nargin-1
   Property = pnmatch(varargin{i},plist);
   Value = varargin{i+1};
   p.(Property) = Value;
end
if isempty(p.Parent), p.Parent = gca; end
if isempty(p.FontName), p.FontName = get(p.Parent,'DefaultTextFontName'); end

x = p.Position(1);
y = p.Position(2);
exn = [0 0 0 0]; %--Num extent
exd = [0 0 0 0]; %--Den extent
exg = [0 0 0 0]; %--Gain extent
exs = [0 0 0 0]; %--Sum extent
ext = [0 0 0 0]; %--Name extent
wrhs = 0;
wlhs = 0;
xeq = x;
xl = x;
xr = x;
te=[];tt=[];tn=[];td=[];tg=[];sb1=[];sb2=[];ls=[];ln=[];

%---Numerator
tn = text(...
   'Parent',p.Parent,...
   'Position',[x y],...
   'String',p.Numerator,...
   'Color',p.Color,...
   'FontName',p.FontName,...
   'FontSize',p.FontSize,...
   'FontWeight',p.FontWeight,...
   'HorizontalAlignment','center',...
   'VerticalAlignment','middle',...
   'Interpreter',p.Interpreter,...
   'Tag',p.Tag,...
   'Clipping',p.Clipping);
exn = get(tn,'Extent');
wrhs = exn(3);
xeq = x-wrhs/2;
xl = xeq;
xr = xeq+wrhs;

%---Denominator
if ~isempty(p.Denominator)
   set(tn,'VerticalAlignment','bottom','Position',[x y+.12*exn(4)]);
   exn = get(tn,'Extent');
   td = text(...
      'Parent',p.Parent,...
      'Position',[x y-.24*exn(4)],...
      'String',p.Denominator,...
      'Color',p.Color,...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','center',...
      'VerticalAlignment','top',...
      'Interpreter',p.Interpreter,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
   exd = get(td,'Extent');
   wrhs = max(exn(3),exd(3));
   xeq = x-wrhs/2;
   xl = xeq;
   xr = xeq+wrhs;
   ln = line(...
      'Parent',p.Parent,...
      'XData',[x-wrhs/2 x+wrhs/2],...
      'YData',[y y],...
      'LineWidth',p.LineWidth,...
      'Color',p.Color,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end

%---Gain
if ~isempty(p.Gain)
   tg = text(...
      'Parent',p.Parent,...
      'Position',[x-wrhs/2 y],...
      'String',sprintf('%s ',p.Gain),...
      'Color',p.Color,...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','right',...
      'VerticalAlignment','middle',...
      'Interpreter',p.Interpreter,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
   exg = get(tg,'Extent');
   set(tg,'pos',[x-wrhs/2 2*y-exg(2)-exg(4)/2]);
   wrhs = wrhs+exg(3);
   xeq = xeq-exg(3);
   xl = xeq;
   xr = xeq+wrhs;
end

%---Summation
if p.Sigma
   tmp = text(...
      'Parent',p.Parent,...
      'Position',[xeq y],...
      'String','S',...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','right',...
      'VerticalAlignment','middle',...
      'Visible','off');
   exs = get(tmp,'Extent');
   delete(tmp);
   w = exs(3);
   h = exs(4);
   xeq = xeq-w/2;
   ls = line(...
      'Parent',p.Parent,...
      'XData',xeq+[0 0 -2*w -w -2*w 0 0],...
      'YData',y+[h/1.5 h h 0 -h -h -h/1.5],...
      'LineWidth',p.LineWidth,...
      'Color',p.Color,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
   if ~isempty(p.SigmaBounds)
      sb1 = text(...
         'Parent',p.Parent,...
         'Position',[xeq-w y-h*1.1],...
         'String',p.SigmaBounds{1},...
         'Color',p.Color,...
         'FontName',p.FontName,...
         'FontSize',p.FontSize-2,...
         'FontWeight',p.FontWeight,...
         'HorizontalAlignment','center',...
         'VerticalAlignment','top',...
         'Interpreter',p.Interpreter,...
         'Tag',p.Tag,...
         'Clipping',p.Clipping);
      sb2 = text(...
         'Parent',p.Parent,...
         'Position',[xeq-w y+h*1.1],...
         'String',p.SigmaBounds{2},...
         'Color',p.Color,...
         'FontName',p.FontName,...
         'FontSize',p.FontSize-2,...
         'FontWeight',p.FontWeight,...
         'HorizontalAlignment','center',...
         'VerticalAlignment','bottom',...
         'Interpreter',p.Interpreter,...
         'Tag',p.Tag,...
         'Clipping',p.Clipping);
   end
   wrhs = wrhs+2*w;
   xeq = xeq-2*w;
   xl = xeq;
   xr = xeq+wrhs;
end

%---Name
if ~isempty(p.Name)
   te = text(...
      'Parent',p.Parent,...
      'Position',[xeq y],...
      'String','  =  ',...
      'Color',p.Color,...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','right',...
      'VerticalAlignment','middle',...
      'Interpreter',p.Interpreter,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
   exe = get(te,'Extent');
   tt = text(...
      'Parent',p.Parent,...
      'Position',[xeq-exe(3) y],...
      'String',sprintf('%s',p.Name),...
      'Color',p.Color,...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','right',...
      'VerticalAlignment','middle',...
      'Interpreter',p.Interpreter,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
   ext = get(tt,'Extent');
   set(tt,'pos',[xeq-exe(3) 2*y-ext(2)-ext(4)/2]);
   wlhs = exe(3)+ext(3);
   xl = xeq-wlhs;
end

%---Make adjustments if a different anchor point is given
switch p.Anchor
case 'left'
   dx = x-xl;
case 'right'
   dx = x-xr;
case 'equal'
   dx = x-xeq;
case 'center'
   dx = x-(xl+(xr-xl)/2);
otherwise
   dx = 0;
end
if dx
   for n=[te tt tn td tg sb1 sb2]
      if ~isempty(n)
         pos = get(n,'Position');
         set(n,'Position',[pos(1)+dx pos(2)]);
      end
   end
   for n=[ls ln]
      if ~isempty(n)
         xd = get(n,'XData');
         set(n,'XData',xd+dx);
      end
   end
end

%---Bracket
if p.Bracket
   tmp = text(...
      'Parent',p.Parent,...
      'Position',[xeq y],...
      'String','S',...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','right',...
      'VerticalAlignment','middle',...
      'Visible','off');
   exs = get(tmp,'Extent');
   delete(tmp);
   exn = get(tn,'Extent');
   w = exn(3)+2*exs(3)/3;
   h = exn(4)+2*exs(4)/4;
   x = exn(1)-exs(3)/3;
   y = exn(2)-exs(4)/4;
   lb = line(...
      'Parent',p.Parent,...
      'XData',[x+exs(3)/3 x x x+exs(3)/3 NaN x+w-exs(3)/3 x+w x+w x+w-exs(3)/3],...
      'YData',[y y y+h y+h NaN y y y+h y+h],...
      'LineWidth',p.LineWidth,...
      'Color',p.Color,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end

%---This element does not affect alignment !
if ~isempty(p.Gain2)
   tg2 = text(...
      'Parent',p.Parent,...
      'Position',[xr+dx y],...
      'String',sprintf(' %s',p.Gain2),...
      'Color',p.Color,...
      'FontName',p.FontName,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','left',...
      'VerticalAlignment','middle',...
      'Interpreter',p.Interpreter,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end
