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