function  W = cobweb(X,Y,spokes,rings,l,rng)

%COBWEB -- Create a matrix depicting a polar coordinate grid.
%
%  W = cobweb(X,Y,spokes,rings,linewidth)
%  W = cobweb(X,Y,spokes,rings,linewidth,Rmax)
%  W = cobweb(X,Y,spokes,rings,linewidth,[Rmin Rmax])
%
%  X and Y are matrices defining the "canvas". They must be monotonic and
%  plaid (as if created by MESHGRID) but need not be uniformly spaced.
%  SPOKES is a set of angles [in degrees] such as [0:30:330].
%  RINGS is a set of radii [in the same units as X and Y].
%  LINEWIDTH defines the width of the cobweb lines.
%  The optional RMIN (default=0) and RMAX (default=Inf) can be
%  used to cut the spokes off.
%  The result W is a matrix of 0s (black) and 1s (transparent).
%  size(W)==size(X)==size(Y).
%
%  Example 1:
%   x = [-5:.01:5] ; y = x ; [X,Y] = meshgrid(x,y) ;
%   W = cobweb(X,Y,[-180:30:180],[1:5],.05,5.1) ;
%   imagesc(x,y,W,[0 1]) ; axis image;axis xy;colormap gray;
%
%  Example 2: (Show the grid on top of an existing image)
%   img=rand(size(X));imagesc(img.*W,[0 1]); axis square;
%      
%  See also POLAR, MESHGRID, CART2POL, CART2POL2.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2004/11/22 14:24 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

if (nargin==5)
   Rmin = 0 ; Rmax = Inf ;
elseif (length(rng)==1)
   Rmin = 0 ; Rmax = rng ;
elseif (length(rng)==2)
   Rmin = rng(1) ; Rmax = rng(2) ;
else
   error('RNG should be either RMAX or [RMIN RMAX].') ;
end

W = ones(size(X)) ;    % all transparent pixels

%-- Transform to polar coordinates
[TH,R] = cart2pol(X,Y) ;

%-- Identify the ring pixels and paint them black
for k = rings
   idx = find(abs(R-k) <= l/2) ;
   W(idx) = 0 ;
end

%-- Identify the spoke pixels and paint them black
spokes = spokes .* (pi/180) ;     % degrees --> radians

for k = spokes
   idx = find((abs(TH-k).*R <= l/2) & R>=Rmin & R<=Rmax) ;
   W(idx) = 0 ;
end

%--- Return W
%%%%% End of file COBWEB.M
