function [x,y] = getpts(fig,s)
%GETPTS	Track mouse movement with visible points.
%	[X,Y] = GETPTS(FIG) tracks the movement of the mouse in
%	the figure FIG.  The mouse movement is tracked with
%	visible points where mouse clicks occur.  Returns the 
%	selected points, X and Y in axes coordinates. Left mouse 
%	button adds a new points, right mouse button (shift-click
%	on the Macintosh) or <cr> finishes process.
%	XY = GETPTS(FIG) returns the data coordinates in a 2-column
%	matrix.
%
%	See also GETRECT, GETLINE.

%	Clay M. Thompson 1-28-93
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.4 $  $Date: 1993/09/09 15:51:35 $

% How this works: This function waits for buttonpresses.  After
% a button press the current point is saved.  When the button is 
% pressed again, the current point is saved and the process is 
% started all over.  This continues until a <cr> or right mouse 
% button is pressed.

global Pt1 hline fig ax

if nargin<1, fig = gcf; end

figure(fig), ax = gca;
c = [.9 .9 .91];

% Create an invisible line to preallocate the xor line color.
hline = line(min(get(ax,'xlim'))*ones(1,2),min(get(ax,'ylim'))*ones(1,2),...
               'erasemode','xor','linestyle','o','Color',c,'visible','off');

if any(get(ax,'view')~=[0 90]), error('Works only on 2-D plots'); end
curse = get(gcf,'pointer');
set(gcf,'pointer','crosshair');

btndown = get(gcf,'windowbuttondownfcn');
btnup = get(gcf,'windowbuttonupfcn');
btnmotion = get(gcf,'windowbuttonmotionfcn');
set(gcf,'windowbuttondownfcn','','windowbuttonupfcn','', ...
        'windowbuttonmotionfcn','')

Pts = []; handles = []; done = 0; first = 0;
while ~done
  done = waitforbuttonpress;
  if gcf==fig,
    axes(ax) % Protect against user clicking in another axis
    Pt1 = get(ax,'CurrentPoint'); 
    Pt1 = Pt1(1,1:2);
    Pts = [Pts;Pt1];
    if ~strcmp(get(fig,'selectiontype'),'normal'), 
      done = 1; 
    else
      set(hline,'xdata',Pts(:,1),'ydata',Pts(:,2))
      if ~first, set(hline,'visible','on'); first = 1; end
    end
    drawnow
  end
end
delete(hline)
set(fig,'pointer',curse);
clear global Pt1 hline fig ax
set(gcf,'windowbuttondownfcn',btndown,'windowbuttonupfcn',btnup, ...
        'windowbuttonmotionfcn',btnmotion)
if nargout < 2, x = Pts; return, end
if ~isempty(Pts), x = Pts(:,1); y = Pts(:,2); else x = []; y = []; end


