function m=dplots(x,y,n,g)

% DPLOTS      Density plots of 2D data based on simple box counting.
% 
%             Example: load survey.dat
%                      m=dplots(survey(:,1),survey(:,2),100)
% 
%             DPLOTS(X,Y,N,G) outputs various graphical representations 
%             of the data (X,Y).  
%
%             N is the dimension of the square density matrix which 
%             results.  
%
%             If the optional argument G=0 then no graphics are produced.
%
%             Christian C. Beardah 1993

if nargin==3,
  g=1;
end;

xmin=min(x);
xmax=max(x);
xa=linspace(xmin,xmax,n);
ymin=min(y);
ymax=max(y);
ya=linspace(ymin,ymax,n);

l=length(x);
m=zeros(n,n);
xs=(x-xmin)/(xmax-xmin);
ys=(y-ymin)/(ymax-ymin);

for i=1:l;
  ix=n*xs(i)-rem(n*xs(i),1)+1;
  if ix==(n+1),
 	  ix=n;
	end;	
  iy=n*ys(i)-rem(n*ys(i),1)+1;
	if iy==(n+1),
         iy=n;
	end;	
  m(ix,iy)=m(ix,iy)+1;
end;

if g~=0,
  figure;
  plot(x,y,'ro');
  xlabel('Component 1');
  ylabel('Component 2');

  pause;

  A=viewmtx(-45,60);

  figure;
  surf(xa,ya,m');
  shading interp;
  colormap(brighten(hot,0.5));
  xlabel('Component 1');
  ylabel('Component 2');
  view(A);

  pause;

  figure;
  pcolor(xa,ya,m');
  shading interp;
  xlabel('Component 1');
  ylabel('Component 2');
  hold on;
  contour(xa,ya,m','k');
  colormap(brighten(cool,-0.5));

  pause;

  figure;
  pcolor(xa,ya,m');
  colormap(brighten(hot,0.5));
  xlabel('Component 1');
  ylabel('Component 2');

  pause;

  m=rot90(m);
end;