annotate toolboxes/FullBNT-1.0.7/GraphViz/Old/draw_graph.m @ 0:cc4b1211e677 tip

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