Home > matgraph > @graph > cdraw.m

cdraw

PURPOSE ^

cdraw(g,coloring) -- draw g with a given vertex coloring

SYNOPSIS ^

function cdraw(g,coloring,line_style,color_matrix)

DESCRIPTION ^

 cdraw(g,coloring) -- draw g with a given vertex coloring
 If no coloring is specified, the default is 'greedy'. 

 cdraw(g,coloring,line_style) --- lines have given line_style. If this is
 not given, the style '-' is used (solid lines). Try ':' for dotted lines.

 cdraw(g,coloring,line_style,color_matrix) --- specify the colors for the
 vertices. color_matrix is an nc-by-3 matrix of RGB values where nc is the
 number of colors in the coloring. The default is hsv(nc). 

 color_matrix can also be a string of MATLAB color specifiers, e.g., 
 cdraw(g,color(g),'-','kwrgb') will draw vertices in color class 1 with
 color 'k' (black), vertices in color class 2 'w' (white), etc. 

 In either case (matrix or letter string) the user must make sure the
 matrix contains sufficiently many colors.
 
 See also draw, ldraw, and ndraw.

 Original author: Brian Towne; modifications by ERS.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function cdraw(g,coloring,line_style,color_matrix)
0002 % cdraw(g,coloring) -- draw g with a given vertex coloring
0003 % If no coloring is specified, the default is 'greedy'.
0004 %
0005 % cdraw(g,coloring,line_style) --- lines have given line_style. If this is
0006 % not given, the style '-' is used (solid lines). Try ':' for dotted lines.
0007 %
0008 % cdraw(g,coloring,line_style,color_matrix) --- specify the colors for the
0009 % vertices. color_matrix is an nc-by-3 matrix of RGB values where nc is the
0010 % number of colors in the coloring. The default is hsv(nc).
0011 %
0012 % color_matrix can also be a string of MATLAB color specifiers, e.g.,
0013 % cdraw(g,color(g),'-','kwrgb') will draw vertices in color class 1 with
0014 % color 'k' (black), vertices in color class 2 'w' (white), etc.
0015 %
0016 % In either case (matrix or letter string) the user must make sure the
0017 % matrix contains sufficiently many colors.
0018 %
0019 % See also draw, ldraw, and ndraw.
0020 %
0021 % Original author: Brian Towne; modifications by ERS.
0022 
0023 edge_color = 'k';
0024 vertex_color = 'k';
0025 r = 0.15;
0026 
0027 if nargin < 3
0028     line_style = '-';
0029 end
0030 
0031 if nargin < 2
0032     coloring = color(g,'greedy');
0033 end
0034 
0035 n = nv(g);
0036 n2 = nv(coloring);
0037 
0038 if nargin < 4
0039     color_matrix = hsv(np(coloring));
0040 end
0041 
0042 if ~(n==n2)
0043     error('Graph and coloring must have equal number of vertices.')
0044 end
0045 
0046 if ~hasxy(g)
0047     embed(g);
0048 end
0049 
0050 xy = getxy(g);
0051 
0052 % first draw the edges
0053 elist = edges(g);
0054 for j=1:ne(g)
0055     u = elist(j,1);
0056     v = elist(j,2);
0057     x = xy([u,v],1);
0058     y = xy([u,v],2);
0059     line(x,y,'Color', edge_color,'LineStyle',line_style);
0060 end
0061 
0062 
0063 % Now draw the vertices by color class
0064 color_classes = parts(coloring);
0065 num_colors = np(coloring);
0066 
0067 for i=1:num_colors
0068     color_class_size = size(color_classes{i},2);
0069     if ischar(color_matrix)
0070         vertex_fill = color_matrix(i) ;
0071     else
0072         vertex_fill = color_matrix(i,:);
0073     end
0074     for j=1:color_class_size
0075         v = color_classes{i}(j);
0076         x = xy(v,1);
0077         y = xy(v,2);
0078         rectangle('Position', [x-r/2, y-r/2, r, r],...
0079                   'Curvature', [1 1], ...
0080                   'EdgeColor', vertex_color, ...
0081                   'FaceColor', vertex_fill);    
0082     end
0083 end    
0084 
0085 axis equal
0086 axis off

Generated on Thu 13-Mar-2008 14:23:52 by m2html © 2003