Points and Lines Drawing Tools

by

 

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

DrawGUI(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 DrawGUI(varargin) % I/stackdraw
if nargin>0; I = varargin{1}; end;
if evalin('base',['exist(' char(39) 'msDraw' char(39) ')']); 
    ms=evalin('base','msDraw'); 
else; 
    ms = 16; 
end;
if evalin('base',['exist(' char(39) 'lwDraw' char(39) ')']); 
    lw=evalin('base','lwDraw'); 
else; 
    lw = 1.5; 
end;

hf=figure('name','Draw GUI','toolbar','figure');
hp = uipanel('Parent',hf,'FontSize',10,'Units','normalized','Position',[0 .95 1 .05]);
hg=imshow(I);
hsp = imscrollpanel(hf,hg);
set(hsp,'Units','normalized','Position',[0 0 1 .9])
uicontrol('Parent',hp,'Style','pushbutton','String','Points','CallBack',@pts,'Units','normalized','Position', [0 0 .1 1])
uicontrol('Parent',hp,'Style','pushbutton','String','Function','CallBack',@funcpts,'Units','normalized','Position', [.1 0 .1 1])
uicontrol('Parent',hp,'Style','pushbutton','String','Inverse Function','CallBack',@invfuncpts,'Units','normalized','Position', [.2 0 .1 1])

%% CallBack functions
    function pts(src,evnt)
        deletePlots();
        gpoints(hf,'base','pts','.','color',[1 0 1],'MarkerSize',ms,'tag','hpts');
    end
    function funcpts(src,evnt)
        deletePlots();
        gsortline(gcf,'base','funcpts','func','horizontal','pchip',1,'.g','-g','MarkerSize',ms,'linewidth',lw,'tag','hfunc');
    end
    function invfuncpts(src,evnt)
        deletePlots();
        gsortline(gcf,'base','invfuncpts','invfunc','vertical','pchip',1,'.g','-g','MarkerSize',ms,'linewidth',lw,'tag','hinvfunc');
    end
    function deletePlots()
        try
            delete(findobj(hf,'tag','hpts'));
            delete(findobj(hf,'tag','hfunc'));
            delete(findobj(hf,'tag','hinvfunc'));
        end
    end
    function showAll(src,evnt)
        hold on;
        try
            plot(evalin('base','pts(:,1)'),evalin('base','pts(:,2)'),'.','color',[1 0 1],'MarkerSize',ms,'linewidth',lw,'tag','hpts');
        end
        try
            plot(evalin('base','func(:,1)'),evalin('base','func(:,2)'),'-g','MarkerSize',ms,'linewidth',lw,'tag','hfunc');
            plot(evalin('base','funcpts(:,1)'),evalin('base','funcpts(:,2)'),'.g','MarkerSize',ms,'linewidth',lw,'tag','hfunc');
        end
        try
            plot(evalin('base','invfunc(:,1)'),evalin('base','invfunc(:,2)'),'-g','MarkerSize',ms,'linewidth',lw,'tag','hinvfunc');
            plot(evalin('base','invfuncpts(:,1)'),evalin('base','invfuncpts(:,2)'),'.g','MarkerSize',ms,'linewidth',lw,'tag','hinvfunc');
        end
    end

end

Contact us