Points and Lines Drawing Tools

by

 

A simple code to implement points and point-to-point lines drawing in a figure.

h=gpoints(fig,env,xy,varargin)
%%-------------------------------------------------------------------------------------------
% Copyright (C) 2012-2013 Marco Chak-Yan YU
% All rights reserved.
% 
% Redistribution and use in source and binary forms, with or without 
% modification, are permitted provided that the following conditions are met:
%    * Redistributions of source code must retain the above copyright 
%      notice, this list of conditions and the following disclaimer.
%    * Redistributions in binary form must reproduce the above copyright 
%      notice, this list of conditions and the following disclaimer in 
%      the documentation and/or other materials provided with the distribution
%      
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS WITHOUT ANY WARRANTY;
% WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
% POSSIBILITY OF SUCH DAMAGE.
% 
%%-------------------------------------------------------------------------------------------

function h=gpoints(fig,env,xy,varargin)
% mouse action - add (left click) / delete (right click)
% keyboard action - done (return) / s (save) / r (reset) / delete (delete)

if ~evalin(env,['exist(' char(39) xy char(39) ')'])
    assignin(env,xy,[]);
end
if evalin(env,['isempty(' xy ')']); x=[]; y=[];
else; x = evalin(env,[xy '(:,1)']); y = evalin(env,[xy '(:,2)']);
end
assignin(env,xy,[x,y]);

set(fig,'WindowButtonDownFcn',@wbd,'WindowButtonUpFcn',@wbu,'KeyPressFcn',@kp,'CloseRequestFcn',@cr);
ax = axis;
drag = false; dragPt=[];
hold on;
h = plot(x,y,varargin{:});
axis(ax);

    function wbd(src,evnt)
        ax = axis;
        if strcmp(get(src,'SelectionType'),'normal')
            cPt=get(gca,'CurrentPoint');
            for i=length(x):-1:1
                if max(abs(x(i)-cPt(1,1)),abs(y(i)-cPt(1,2)))<=3
                    dragPt=i;
                    drag=true;
                end
            end
        end
    end
    function wbu(src,evnt)
        cPt=get(gca,'CurrentPoint');
        if strcmp(get(src,'SelectionType'),'normal')
            if drag
                x(dragPt)=cPt(1,1); y(dragPt)=cPt(1,2);
                drag = false; dragPt=[];
            else
                x=[x;cPt(1,1)]; y=[y;cPt(1,2)];
            end
        elseif strcmp(get(src,'SelectionType'),'alt')
            for i=length(x):-1:1
                if max(abs(x(i)-cPt(1,1)),abs(y(i)-cPt(1,2)))<=3
                    x(i)=[]; y(i)=[];
                end
            end
        end
        if isempty(h); h = plot(x,y,varargin{:}); end;
        set(h,'XData',x,'YData',y); refreshdata(h);
        drawnow update; axis(ax);
    end
    function kp(src,evnt)
        pk=evnt.Key;
        if (strcmp(pk,'return'))
            assignin(env,xy,[x,y]);
            set(fig,'WindowButtonDownFcn',[],'WindowButtonUpFcn',[],'KeyPressFcn',[]);
            hold off;
        elseif (strcmp(pk,'s'))
            assignin(env,xy,[x,y]);
        elseif (strcmp(pk,'r'))
            if evalin(env,['isempty(' xy ')']);
                x = []; y = [];
            else
                x = evalin(env,[xy '(:,1)']); y = evalin(env,[xy '(:,2)']);
            end
        elseif (strcmp(pk,'delete'))
            x=[]; y=[];
        end
        if isempty(h); h = plot(x,y,varargin{:}); end;
        set(h,'XData',x,'YData',y); refreshdata(h);
        drawnow update; axis(ax);
    end
    function cr(src,evnt)
        set(fig,'WindowButtonDownFcn',[],'WindowButtonUpFcn',[],'KeyPressFcn',[]);
        hold off;
        delete(fig);
    end

end

Contact us