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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
function [x, y, h] = draw_graph(adj, labels, node_t, x, y)
% DRAW_LAYOUT Draws a layout for a graph
%
% [<X, Y>] = DRAW_LAYOUT(ADJ, <LABELS, ISBOX, X, Y>)
%
% Inputs :
% ADJ : Adjacency matrix (source, sink)
% LABELS : Cell array containing labels <Default : '1':'N'>
% ISBOX : 1 if node is a box, 0 if oval <Default : zeros>
% X, Y, : Coordinates of nodes on the unit square <Default : calls make_layout>
%
% Outputs :
% X, Y : Coordinates of nodes on the unit square
% H : Object handles
%
% Usage Example : [x, y] = draw_layout([0 1;0 0], {'Hidden','Visible'}, [1 0]');
%
% h(i,1) is the text handle - color
% h(i,2) is the circle handle - facecolor
%
% Note :
% See also MAKE_LAYOUT
% Uses :
% Change History :
% Date Time Prog Note
% 13-Apr-2000 9:06 PM ATC Created under MATLAB 5.3.1.29215a (R11.1)
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
adj = double(adj);
N = size(adj,1);
if nargin<2,
% labels = cellstr(char(zeros(N,1)+double('+')));
labels = cellstr(int2str((1:N)'));
end;
if nargin<3,
node_t = zeros(N,1);
% node_t = rand(N,1) > 0.5;
else
node_t = node_t(:);
end;
axis([0 1 0 1]);
set(gca,'XTick',[],'YTick',[],'box','on');
% axis('square');
%colormap(flipud(gray));
if nargin<4,
[x y] = make_layout(adj);
end;
idx1 = find(node_t==0); wd1=[];
if ~isempty(idx1),
[h1 wd1] = textoval(x(idx1), y(idx1), labels(idx1));
end;
idx2 = find(node_t~=0); wd2 = [];
if ~isempty(idx2),
[h2 wd2] = textbox(x(idx2), y(idx2), labels(idx2));
end;
wd = zeros(size(wd1,1)+size(wd2,1),2);
if ~isempty(idx1), wd(idx1, :) = wd1; end;
if ~isempty(idx2), wd(idx2, :) = wd2; end;
for i=1:N,
j = find(adj(i,:)==1);
for k=j,
if x(k)-x(i)==0,
sign = 1;
if y(i)>y(k), alpha = -pi/2; else alpha = pi/2; end;
else
alpha = atan((y(k)-y(i))/(x(k)-x(i)));
if x(i)<x(k), sign = 1; else sign = -1; end;
end;
dy1 = sign.*wd(i,2).*sin(alpha); dx1 = sign.*wd(i,1).*cos(alpha);
dy2 = sign.*wd(k,2).*sin(alpha); dx2 = sign.*wd(k,1).*cos(alpha);
if adj(k,i)==0, % if directed edge
arrow([x(i)+dx1 y(i)+dy1],[x(k)-dx2 y(k)-dy2]);
else
line([x(i)+dx1 x(k)-dx2],[y(i)+dy1 y(k)-dy2],'color','k');
adj(k,i)=-1; % Prevent drawing lines twice
end;
end;
end;
if nargout>2,
h = zeros(length(wd),2);
if ~isempty(idx1),
h(idx1,:) = h1;
end;
if ~isempty(idx2),
h(idx2,:) = h2;
end;
end;
%%%%%
function [t, wd] = textoval(x, y, str)
% TEXTOVAL Draws an oval around text objects
%
% [T, WIDTH] = TEXTOVAL(X, Y, STR)
% [..] = TEXTOVAL(STR) % Interactive
%
% Inputs :
% X, Y : Coordinates
% TXT : Strings
%
% Outputs :
% T : Object Handles
% WIDTH : x and y Width of ovals
%
% Usage Example : [t] = textoval('Visit to Asia?');
%
%
% Note :
% See also TEXTBOX
% Uses :
% Change History :
% Date Time Prog Note
% 15-Jun-1998 10:36 AM ATC Created under MATLAB 5.1.0.421
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
temp = [];
switch nargin,
case 1,
str = x;
if ~isa(str,'cell') str=cellstr(str); end;
N = length(str);
wd = zeros(N,2);
for i=1:N,
[x, y] = ginput(1);
tx = text(x,y,str{i},'HorizontalAlignment','center','VerticalAlign','middle');
[ptc wx wy] = draw_oval(tx, x, y);
wd(i,:) = [wx wy];
delete(tx);
tx = text(x,y,str{i},'HorizontalAlignment','center','VerticalAlign','middle');
temp = [temp ; tx ptc];
end;
case 3,
if ~isa(str,'cell') str=cellstr(str); end;
N = length(str);
wd = zeros(N,2);
for i=1:N,
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center','VerticalAlign','middle');
[ptc wx wy] = draw_oval(tx, x(i), y(i));
wd(i,:) = [wx wy];
delete(tx);
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center','VerticalAlign','middle');
temp = [temp; tx ptc];
end;
otherwise,
end;
if nargout>0, t = temp; end;
%%%%%%%%%
function [ptc, wx, wy] = draw_oval(tx, x, y)
% Draws an oval box around a tex object
sz = get(tx,'Extent');
wy = sz(4);
wx = max(2/3*sz(3), wy);
wx = 0.5*wx; % KPM
wy = 0.5*wy;
ptc = ellipse(x, y, wx, wy);
set(ptc, 'FaceColor','w');
%%%%%%%%%%%%%
function [p] = ellipse(x, y, rx, ry, c)
% ELLIPSE Draws Ellipse shaped patch objects
%
% [<P>] = ELLIPSE(X, Y, Rx, Ry, C)
%
% Inputs :
% X : N x 1 vector of x coordinates
% Y : N x 1 vector of y coordinates
% Rx, Ry : Radii
% C : Color index
%
%
% Outputs :
% P = Handles of Ellipse shaped path objects
%
% Usage Example : [] = ellipse();
%
%
% Note :
% See also
% Uses :
% Change History :
% Date Time Prog Note
% 27-May-1998 9:55 AM ATC Created under MATLAB 5.1.0.421
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
if (nargin < 2) error('Usage Example : e = ellipse([0 1],[0 -1],[1 0.5],[2 0.5]); '); end;
if (nargin < 3) rx = 0.1; end;
if (nargin < 4) ry = rx; end;
if (nargin < 5) c = 1; end;
if length(c)==1, c = ones(size(x)).*c; end;
if length(rx)==1, rx = ones(size(x)).*rx; end;
if length(ry)==1, ry = ones(size(x)).*ry; end;
n = length(x);
p = zeros(size(x));
t = 0:pi/30:2*pi;
for i=1:n,
px = rx(i)*cos(t)+x(i);
py = ry(i)*sin(t)+y(i);
p(i) = patch(px,py,c(i));
end;
if nargout>0, pp = p; end;
%%%%%
function [t, wd] = textbox(x,y,str)
% TEXTBOX Draws A Box around the text
%
% [T, WIDTH] = TEXTBOX(X, Y, STR)
% [..] = TEXTBOX(STR)
%
% Inputs :
% X, Y : Coordinates
% TXT : Strings
%
% Outputs :
% T : Object Handles
% WIDTH : x and y Width of boxes
%%
% Usage Example : t = textbox({'Ali','Veli','49','50'});
%
%
% Note :
% See also TEXTOVAL
% Uses :
% Change History :
% Date Time Prog Note
% 09-Jun-1998 11:43 AM ATC Created under MATLAB 5.1.0.421
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
% See
temp = [];
switch nargin,
case 1,
str = x;
if ~isa(str,'cell') str=cellstr(str); end;
N = length(str);
wd = zeros(N,2);
for i=1:N,
[x, y] = ginput(1);
tx = text(x,y,str{i},'HorizontalAlignment','center','VerticalAlign','middle');
[ptc wx wy] = draw_box(tx, x, y);
wd(i,:) = [wx wy];
delete(tx);
tx = text(x,y,str{i},'HorizontalAlignment','center','VerticalAlign','middle');
temp = [temp; tx ptc];
end;
case 3,
if ~isa(str,'cell') str=cellstr(str); end;
N = length(str);
for i=1:N,
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center','VerticalAlign','middle');
[ptc wx wy] = draw_box(tx, x(i), y(i));
wd(i,:) = [wx wy];
delete(tx);
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center','VerticalAlign','middle');
temp = [temp; tx ptc];
end;
otherwise,
end;
if nargout>0, t = temp; end;
function [ptc, wx, wy] = draw_box(tx, x, y)
% Draws a box around a tex object
sz = get(tx,'Extent');
wy = 2/3*sz(4);
wx = max(2/3*sz(3), wy);
ptc = patch([x-wx x+wx x+wx x-wx], [y+wy y+wy y-wy y-wy],'w');
set(ptc, 'FaceColor','w');
|