%****************************************************
%       Copyright (C) 2008 Nasser Abbasi
%  Free to use and modify for academic and research only
%***************************************************
function I=nma_makeDisk(N,r,cx,cy,blackGrayLevel,whiteGrayLevel)
%function nma_makeDisk(N,r,cx,cy)
%
%function to make disk image
%r      %radius in pixels of disk
%N      %size of overall image in pixels, say 256
%cx,cy  %location of x,y of disk center relative to center of image
%       %so, to have the disk center in the center of the image use (0,0)
%
% by Nasser Abbasi June 9,2008

if nargin ~=6
    error('wrong number of arguments');
end

r = round(r);

[errmsg,success]=verifyParamters(N,r,cx,cy);
if ~success
    error(errmsg);
end

I=zeros(N);
I(:)=blackGrayLevel;

row=(-1)*cy+ceil(N/2); col=cx+ceil(N/2);  %adjust to center it in the image
for y=1:N
    for x=1:N
        d = sqrt((x-col)^2+(y-row)^2);
        if d<=r
            I(y,x)=whiteGrayLevel;
        end
    end
end

end
%%%%%%%%%%%%%%%%%%%
% Called from above function to validate input
%
%%%%%%%%%%%%%%%%%%%
function [errmsg,success]=verifyParamters(N,r,cx,cy)
errmsg='';
success=0;

if ~isnumeric(N) || ~isnumeric(r)||~isnumeric(cx)||~isnumeric(cy)
    errmsg='invalid argument type. Numerical values expected';
    return;
end

if ~(ceil(N) == floor(N))
    errmsg='N, size of image must be integer';
    return;
end

if ~(ceil(cx) == floor(cx))
    errmsg='cx, x-coordinates of disk must be integer';
    return;
end

if ~(ceil(cy) == floor(cy))
    errmsg='cy, y-coordinates of disk must be integer';
    return;
end

if abs(cx)>floor(N/2) || abs(cy)>floor(N/2)
    errmsg='coordinates of center of disk outside image';
    return;
end

if r>floor(N/2)
    errmsg='radius is too large, must be less than half the image size';
    return;
end

success=1;
end

