comparison toolboxes/FullBNT-1.0.7/GraphViz/Old/draw_graph.m @ 0:e9a9cd732c1e tip

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