Points and Lines Drawing Tools

by

 

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

H=gsortline(fig,env,xy,xyi,direction,interp1method,bin,pty,lty,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=gsortline(fig,env,xy,xyi,direction,interp1method,bin,pty,lty,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]);
sortpts(); smoothpts();
assignin(env,xyi,[xi,yi]);

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

    function sortpts()
        if (strcmp(direction,'vertical'))
            [y,si] = sort(y); x = x(si);
        else
            [x,si] = sort(x); y = y(si);
        end
    end
    function smoothpts()
        if length(x)<2
            xi = x; yi = y;
        else
            if (strcmp(direction,'vertical'))
                yi = [y(1):bin:y(end)]'; if (yi(end)<y(end)); yi=[yi;y(end)]; end;
                xi = interp1(x,y,yi,interp1method);
            else
                xi = [x(1):bin:x(end)]'; if (xi(end)<x(end)); xi=[xi;x(end)]; end;
                yi = interp1(x,y,xi,interp1method);
            end
        end
    end

    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
        sortpts(); smoothpts();
        if isempty(hi); hi = plot(xi,yi,lty,varargin{:}); end;
        if isempty(h); h = plot(x,y,pty,varargin{:}); end;
        set(hi,'XData',xi,'YData',yi); refreshdata(hi);
        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]);
            assignin(env,xyi,[xi,yi]);
            set(fig,'WindowButtonDownFcn',[],'WindowButtonUpFcn',[],'KeyPressFcn',[]);
            hold off;
        elseif (strcmp(pk,'s'))
            assignin(env,xy,[x,y]);
            assignin(env,xyi,[xi,yi]);
        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
        sortpts(); smoothpts();
        if isempty(hi); hi = plot(xi,yi,lty,varargin{:}); end;
        if isempty(h); h = plot(x,y,pty,varargin{:}); end;
        set(hi,'XData',xi,'YData',yi); refreshdata(hi);
        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