function [grid] = ppmakegrid41(cent,xspacing,xangle,yspacing,yangle)
% Function PPMAKEGRID41.M
%
%    [grid] = ppmakegrid41(cent,xspacing,xangle,yspacing,yangle)
%
%    PPMAKEGRID41 creates a 41 x 41 grid of equally spaced points, using a
%    central point CENT, a horizontal tilt angle XANGLE, a vertical tilt
%    angle YANGLE and a point spacing SPACING.
%
%    [GRID] = PPMAKEGRID41(CENT,XSPACING,XANGLE) creates a [ 41 x 41 x 2 ]
%    array, GRID, that contains the location of each of the points:
%    GRID(:,:,1) contains the X coordinates and GRID(:,:,2) the Y
%    coordinates.  By definition, GRID(21,21,:) = CENT, since the points
%    are located by their distance from the central point.  By specifying a
%    single angle XANGLE, this assumes that the grid is uniform and all
%    points are equally spaced
%
%    [GRID] = PPMAKEGRID41(CENT,XSPACING,XANGLE,YSPACING,YANGLE) also creates
%    a [ 41 x 41 x 2 ] array, GRID, but this time the horizontal and
%    vertical spacings are different.
%
%    The input variables are as follows:
%
%    CENT - the central point of the grid, CENT = [X Y].  Must be a [ 2 x 1 ]
%    matrix.
%
%    XSPACING - the mean point spacing in pixels (for X if X & Y are different).
%
%    XANGLE - the angle of the image tilt in radians (for X if X & Y are different).
%
%    YSPACING - the mean point spacing in pixels for Y.
%
%    YANGLE - the angle of the image tilt in radians for Y.
%

    if nargin < 3
        error('Must specify at least 3 input variables: [grid] = ppmakegrid41(cent,xspacing,xangle)') ;
    elseif nargin > 5
            error('Can only specify 5 input variables: [grid] = ppmakegrid41(cent,xspacing,xangle,yspacing,yangle)') ;
    elseif nargin == 4
            error('Must specify spacing AND angle: [grid] = ppmakegrid41(cent,xspacing,xangle,yspacing,yangle)') ;
    elseif nargout ~= 1
        error('Must specify all input variables: [grid] = ppmakegrid41(cent,angle,spacing)') ;
    elseif ( length(cent) ~= 2 )
        error('CENT variable must be a 2x1 matrix with X and Y coordinates: CENT = [X Y]') ;
    end

    if nargin == 3
        yspacing = xspacing ;
        yangle = -xangle ;
    end

%    Set up variables

    coords = [-20:1:20] ;
    horzdist = [xspacing.*cos(xangle) xspacing.*sin(xangle)] ;
    verdist = [yspacing.*sin(yangle) yspacing.*cos(yangle)] ;
%    verdist = horzdist([2 1]) ;

%    Create spot matrix

    grid = zeros(41,41,2) ;

    for i = 1:41
        for j = 1:41
            grid(i,j,1) = cent(1) + ( horzdist(1).*coords(j) + verdist(1).*coords(i) ) ;
            grid(i,j,2) = cent(2) + ( horzdist(2).*coords(j) + verdist(2).*coords(i) ) ;
        end
    end

    return