function [xg yg pp]=ginzoompan_freeform
%free form = not trying to link to specific points
xg=[];yg=[];b=0; pp=[];
while~isempty(b)
    [x y b]=ginput(1);
    
    if ~isempty(b)
        pp(length(pp)+1)=plot(x,y,'.m');
        
        xlabel({'Left-click to add pt'; 'Right-click to remove pt'; 'Backspace to undo all'; 'z to zoom'; 'p to pan'; 'Enter when done'},...
            'Units', 'normalized', 'Position', [1 1], 'HorizontalAlignment', 'left')
        
        if ~isempty(b)
            if b<10 %Edit existing trace
                if b==1 %add pt
                    xg=[xg;x]; yg=[yg;y];
                elseif b==3 %remove pt
                    d=distance([xg yg],[x y]); %Finding nearest coordinate on the skeleton
                    [val ind]=min(d);
                    xg(ind)=[]; yg(ind)=[];
                    set(pp(ind),'visible','off')
                elseif b==8 %undo all and start over
                    xg(:)=[]; yg(:)=[];
                    set(pp(:),'visible','off')
                end
                
            else %Zoom or pan
                set(gcf,'Tag','Original')
                gcfp=get(gcf,'Position');
                h = uicontrol('String', 'Resume', 'Position',[gcfp(3)-120 gcfp(4)-80 80 30], 'Callback', 'set(gcf,''Tag'',''Mod'')','BackgroundColor',[.7 1 .7] );
                if b==122
                    zoom on
                    drawnow
                    waitfor(gcf, 'Tag');
                elseif b==112
                    pan on
                    drawnow
                    waitfor(gcf, 'Tag');
                end
                set(h,'Visible','off')
            end
        end
    end
end

% %Zoom or pan
% set(gcf,'Tag','Original')
% gcfp=get(gcf,'Position');
% h = uicontrol('String', 'Resume', 'Position',[gcfp(3)-120 gcfp(4)-80 80 30], 'Callback', 'set(gcf,''Tag'',''Mod'')','BackgroundColor',[.7 1 .7] );
%
% if b==122
%     zoom on
%     drawnow
%     waitgcfr(gcf, 'Tag');
% elseif b==112
%     pan on
%     drawnow
%     waitgcfr(gcf, 'Tag');
% end
% set(h,'Visible','off')
