function hh=add_ellipse(varargin)
%add_ellipse
% click on left and right, (plots it)
%  bottom and top (plots it) 
% for an ellipse.
% crosshair is then deleted, handle for ellipse is returned
%
% varargin is
% 'c' color                        default=black
% 'w' linewidth                    default=1
% 'n' number of points (plot as discrete points, not a line).default is none
% 's' markersize for points, if using points (default=6)

cfg.c = 'k';
cfg.w = 1;
cfg.n = [];
cfg.s = 6;

cfg=fillstruct(cfg,varargin)

hold on

fprintf('click twice, for the vertical limits of the ellipse\n')
[x2,y2]=ginput(2);
h2 = plot(x2,y2,'color',cfg.c);


fprintf('click twice, for the horizontal limits of the ellipse\n')
[x1,y1]=ginput(2);
h1 = plot(x1,y1,'color',cfg.c);


%from the horizontal bar:
hr = abs(diff(x1))/2;
ellx = mean(x1);

%from the vertical bar:
vr = abs(diff(y2))/2;
elly = mean(y2);


if (isempty(cfg.n))
   hh = plot(ellx + hr*cos(0 : pi/64 : 2*pi), ...
             elly + vr*sin(0 : pi/64 : 2*pi),'k');
   set(hh,'color',cfg.c,'linewidth',cfg.w);
else
   hh = plot(ellx + hr*cos(0 : pi/64 : 2*pi), ...
             elly + vr*sin(0 : pi/64 : 2*pi),'k.');
   set(hh, 'color',cfg.c, 'markersize',cfg.s);
end

   
delete(h1)
delete(h2)
