function t = demo_tree(w,p,h,n);

% function t = demo_tree(w,p,h,n);
% create and visualize random quad tree 
% w: density in [0,1)
% p: center 
% h: width 
% n: levels

% defaults
if nargin < 4;
   n = 5; h = 1; p = [0, 0]; w = 2/3;
end;

% empty tree for lowest level
if n == 0; 
   t = {};
   return;
end;

hold on;

% plot square p + h*[-1,1]^2/2 containing tree
c = [1 -1  0  0  0; 
     0  0  0  1 -1];
plot(p(1)+h*c(1,:), p(2)+h*c(2,:));

% create subtrees with centers pt
pk = h*[1 1; -1 1; -1 -1; 1 -1]/2;
for k=1:4;
   if rand(1) < w;
      pt = p+pk(k,:);
      t{k} = {pt, demo_tree(w, pt, h/2, n-1)};
   else;
      t{k} = {};
   end;
end;

