function circle_image = circle( N );
% CIRCLE
% Returns a NxN square grid.
% Pixels inside a circle are set to 1;
% pixels outside the circle are set to 0.
% CIRCLE creates the *biggest* possible circle
% such that all the "1"s are inside a
% NxN square grid.
% example
%   circle_image = circle( 11 )
% Note: some image processing tools
% only work right with *odd* sized circles.
% Pixels outside that circle are zeroed.
% For N < 6, this is (surprisingly)
% equivalent to
%   circle_image = ones(N);
% .
% N is assumed to be integer greater than 0.
%
% See also CIRCLE_MASK for more control over the exact radius.

% Change Log
% 1999-02-15:DAV: fixed documentation.
% 1998-09-16:DAV: Works now.
% 1998-09-16:DAV: Modularized to use circle_mask.
% 1998-04-02:DAV: David Cary <d.cary@ieee.org> Started

even = rem(N,2) == 0;

% Calculate radius of next pixel just outside the
% NxN square. (circle_mask considers pixels equal
% to the given radius as *outside* the circle).
% This gives the *maximum* radius.

if(even)
    % Largest radius that can be inscribed
    r2 = 0.5^2 + (N/2 + 1/2)^2;
else
    % N is odd
    r2 = ( (N+1)/2 )^2;
end;

% Use "+2" to create a ring of 0s around the outer rim of the circle.
% circle_image = circle_mask(N+2, r2);
circle_image = circle_mask(N, r2);
