function G = clickgraph(N,Connection) % N is the number of nodes in your graph % G is the matrix that will be output in the end % Initialize matrix for output G = zeros(N,N); % Create new figure window and make it the right size figure axis([0 N+2 0 N+2]) hold on % Get the points for the nodes, draw the circles, plot the labels for i=1:N [xy(1,i) xy(2,i)] = ginput(1); hold on drawcircle(xy(:,i),.5,100); hold on A = num2str(i); text(xy(1,i), xy(2,i), A,'HorizontalAlignment','center','VerticalAlignment','middle') end % Prompt user to begin clicking for connections disp('Start making connections, click inside the nodes.') % Get the connection points, plot them, form matrix for K=1:Connection % Connection counter, shows up as an x label conn = num2str(K); totconn = num2str(Connection); Title = cat(2,'Connection # ',conn,' out of ',totconn); xlabel(Title) % Get first input, the FROM node [fst(1) fst(2)] = ginput(1); % Determine whice node that point is in for i=1:N if ((fst(1) - xy(1,i))^2 + (fst(2) - xy(2,i))^2) <= (.5)*(.5) Y = i; end end % Get second input, the TO node [scnd(1) scnd(2)] = ginput(1); % Determine which node that point is in for i=1:N if ((scnd(1) - xy(1,i))^2 + (scnd(2) - xy(2,i))^2) <= (.5)*(.5) X = i; end end % Update the matrix G with the existence of that connection G(X,Y) = 1; % Plot the connection hold on plot_arrow(fst(1),fst(2),scnd(1),scnd(2)) ; end end function H=drawcircle(center,radius,NOP,style) %--------------------------------------------------------------------------------------------- % H=CIRCLE(CENTER,RADIUS,NOP,STYLE) % This routine draws a circle with center defined as % a vector CENTER, radius as a scaler RADIS. NOP is % the number of points on the circle. As to STYLE, % use it the same way as you use the rountine PLOT. % Since the handle of the object is returned, you % use routine SET to get the best result. % % Usage Examples, % % circle([1,3],3,1000,':'); % circle([2,4],2,1000,'--'); % % Zhenhai Wang % Version 1.00 % December, 2002 %--------------------------------------------------------------------------------------------- if (nargin <3), error('Please see help for INPUT DATA.'); elseif (nargin==3) style='b-'; end; THETA=linspace(0,2*pi,NOP); RHO=ones(1,NOP)*radius; [X,Y] = pol2cart(THETA,RHO); X=X+center(1); Y=Y+center(2); H=plot(X,Y,style); axis square; end function handles = plot_arrow( x1,y1,x2,y2,varargin ) % % plot_arrow - plots an arrow to the current plot % % format: handles = plot_arrow( x1,y1,x2,y2 [,options...] ) % % input: x1,y1 - starting point % x2,y2 - end point % options - come as pairs of "property","value" as defined for "line" and "patch" % controls, see matlab help for listing of these properties. % note that not all properties where added, one might add them at the end of this file. % % additional options are: % 'headwidth': relative to complete arrow size, default value is 0.07 % 'headheight': relative to complete arrow size, default value is 0.15 % (encoded are maximal values if pixels, for the case that the arrow is very long) % % output: handles - handles of the graphical elements building the arrow % % Example: plot_arrow( -1,-1,15,12,'linewidth',2,'color',[0.5 0.5 0.5],'facecolor',[0.5 0.5 0.5] ); % plot_arrow( 0,0,5,4,'linewidth',2,'headwidth',0.25,'headheight',0.33 ); % plot_arrow; % will launch demo % ============================================= % for debug - demo - can be erased % ============================================= if (nargin==0) figure; axis; set( gca,'nextplot','add' ); for x = 0:0.3:2*pi color = [rand rand rand]; h = plot_arrow( 1,1,50*rand*cos(x),50*rand*sin(x),... 'color',color,'facecolor',color,'edgecolor',color ); set( h,'linewidth',2 ); end hold off; return end % ============================================= % end of for debug % ============================================= % ============================================= % constants (can be edited) % ============================================= alpha = 0.15; % head length beta = 0.07; % head width max_length = 22; max_width = 10; % ============================================= % check if head properties are given % ============================================= % if ratio is always fixed, this section can be removed! if ~isempty( varargin ) for c = 1:floor(length(varargin)/2) try switch lower(varargin{c*2-1}) % head properties - do nothing, since handled above already case 'headheight',alpha = max( min( varargin{c*2},1 ),0.01 ); case 'headwidth', beta = max( min( varargin{c*2},1 ),0.01 ); end catch fprintf( 'unrecognized property or value for: %s\n',varargin{c*2-1} ); end end end % ============================================= % calculate the arrow head coordinates % ============================================= den = x2 - x1 + eps; % make sure no devision by zero occurs teta = atan( (y2-y1)/den ) + pi*(x2