Code covered by the BSD License  

Highlights from
uidendrogram

uidendrogram

by

 

22 Aug 2010 (Updated )

Interactive dendrogram viewer using nested boxes

drawtree(link, cut, colours, iwidth, iheight)
function [ out box ] = drawtree(link, cut, colours, iwidth, iheight)
%DRAWTREE Draw the tree as graphics
%   link - linkage object
%   cut - level at which to stop drawing boxes
%   colours - leaf colours
%   iwidth - image width
%   iheight - image height
    out = uint8(zeros(iheight, iwidth, 3));
    out(:,:,:) = 255;
    [m n ] = size(link);
    Nleaves = m+1;
    box = zeros(m+m+1, 4);
    
    draw_r(m+m+1, 2, 2, iwidth-1, iheight-1); 
    

    %
    % Recursive draw function
    %
    function draw_r(index, x, y, width, height)
        box(index,1) = x;
        box(index,2) = y;
        box(index, 3) = width;
        box(index, 4) = height;
        idx = index - Nleaves;
        if(index <= Nleaves)
            x = fix(x + width / 2);
            y = fix(y + height / 2);
            out(y, x, :) = colours(index, :);
            out(y, x+1,:) = colours(index, :);
            out(y, x-1,:) = colours(index, :);
            out(y+1, x,:) = colours(index, :);
            out(y-1, x,:) = colours(index, :);
            return;
        end
        Nleft = sum(getleaves(link, link(idx, 1)));
        Nright = sum(getleaves(link, link(idx, 2)));
        p = Nleft / (Nleft + Nright);
        if(width > height)
            cwidth = fix(width * p);
            cx = x + cwidth;
            if(cut <= link(idx, 3))
                out(y:y+height-1, cx, :) = 127;
            end
            draw_r(link(idx, 1), x, y, cwidth, height);
            draw_r(link(idx, 2), cx, y, width -cwidth, height);
        else
            cheight = fix(height * p);
            cy = y + cheight;
            if(cut <= link(idx, 3))
                out(cy, x:x+width-1, :) = 127;
            end
            draw_r(link(idx, 1), x, y, width, cheight);
            draw_r(link(idx, 2), x, cy, width, height-cheight);
        end
    end
end




Contact us