-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfreehanddraw.m
112 lines (97 loc) · 3.16 KB
/
freehanddraw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function [lineobj,xs,ys] = freehanddraw(varargin)
% [LINEOBJ,XS,YS] = FREEHANDDRAW(ax_handle,line_options)
%
% Draw a smooth freehand line object on the current axis (default),
% or on the axis specified by handle in the first input argument.
% Left-click to begin drawing, right-click to terminate, or double-click
% to close contour and terminate.
%
%
% INPUT ARGUMENTS: First: axis handle (optional)
% Additional: valid line property/value pairs
%
% OUTPUT ARGUMENTS: 1) Handle to line object
% 2) x-data
% 3) y-data
% (Note that output args 2 and 3 can also be extracted from the first output
% argument.)
%
% Ex: [myobj,xs,ys] = freehanddraw(gca,'color','r','linewidth',3);
% freehanddraw('linestyle','--');
%
% Written by Brett Shoelson, PhD
% 3/29/05
% Modified:
% 10/05/05: Now allows double-click closing of contour
axdef = 0;
if nargin ~= 0 && ishandle(varargin{1})
try
axes(varargin{1});
axdef = 1;
catch
error('If the initial input argument is a handle, it must be to a valid axis.');
end
end
%Get current figure and axis parameters
oldvals = get(gcf);
oldhold = ishold(gca);
hold on;
set(gcf,'Pointer','crosshair','doublebuffer','on');
%Get the initial point
[xs,ys,zs] = ginput(1);
%Create and store line object
if axdef
lineobj = line(xs,ys,'tag','tmpregsel',varargin{2:end});
else
lineobj = line(xs,ys,'tag','tmpregsel',varargin{:});
end
setappdata(gcf,'lineobj',lineobj);
%Modify wbmf of current figure to update lineobject on mouse motion
set(gcf,'windowbuttonmotionfcn',@wbmfcn);
set(gcf,'windowbuttondownfcn',@wbdfcn);
%Wait for right-click or double-click
while ~strcmp(get(gcf,'SelectionType'),'alt') & ~strcmp(get(gcf,'SelectionType'),'open')
drawnow;
end
%Extract xyz data from line object for return in output variables
%(Also retrievable from first output argument)
if nargout > 1
xs = get(getappdata(gcf,'lineobj'),'xdata')';
end
if nargout > 2
ys = get(getappdata(gcf,'lineobj'),'ydata')';
end
%Clear temporary variables from base workspace
evalin('caller','clear tmpx tmpy tmpz done gca lineobj');
%Reset figure parameters
set(gcf,'Pointer',oldvals.Pointer,...
'windowbuttonmotionfcn',oldvals.WindowButtonMotionFcn,...
'windowbuttondownfcn',oldvals.WindowButtonDownFcn);
if isfield(oldvals, 'DoubleBuffer')
set(gcf,'doublebuffer',oldvals.DoubleBuffer);
end
%Reset hold value of the axis
if ~oldhold, hold off; end
function wbmfcn(varargin)
lineobj = getappdata(gcf,'lineobj');
if strcmp(get(gcf,'selectiontype'),'normal');
tmpx = get(lineobj,'xdata');
tmpy = get(lineobj,'ydata');
a=get(gca,'currentpoint');
set(lineobj,'xdata',[tmpx,a(1,1)],'ydata',[tmpy,a(1,2)]);
drawnow;
else
setappdata(gcf,'lineobj',lineobj);
end
function wbdfcn(varargin)
lineobj = getappdata(gcf,'lineobj');
if strcmp(get(gcf,'selectiontype'),'open')
tmpx = get(lineobj,'xdata');
tmpy = get(lineobj,'ydata');
a=get(gca,'currentpoint');
set(lineobj,'xdata',[tmpx,tmpx(1)],'ydata',[tmpy,tmpy(1)]);
setappdata(gcf,'lineobj',lineobj);
drawnow;
end
return