SAMPLEDISK - Sampling disks over a grid.

Contents

Description

Sample lattice points (ie. with integer coordinates) inside disks given by their respective centers and radii.

Syntax

  SAMPLEDISK(center, radius);
  [x, y, w] = SAMPLEDISK(center, radius, method, samp, disp);

Inputs

center : matrix (k,2) of the (X,Y) integer coordinates of the centers of k disks.

radius : matrix (k,1) of the radii of the k disks.

method : (optional) string defining the method used for sampling the lattice points inside the disks; it can be:

   default: |method='full'|.

samp : (optional) scalar or vector of size (k,1) giving (of no use when method='full' or 'scale'):

note that in the latter case ('radi'), the total number of points in the disk is estimated using Gauss formula; default: samp=0.5 for all chosen methods.

disp : (optional) flag for drawing (discrete) lines; default: disp=false.

Outputs

x, y : (X,Y) coordinates of the sampled points.

Reference

See discussions at http://mathworld.wolfram.com/DiskPointPicking.html http://mathworld.wolfram.com/GausssCircleProblem.html

See also

Ressembles: SAMPLETRIANGLE, BRESENHAMLINE. Requires: SAMPLEDISK_BASE.

Function implementation

%--------------------------------------------------------------------------
function varargout = sampledisk(center, radius, method, samp, disp)

settting/checking variables

% check errors in muber of input/output variables
error(nargchk(3, 5, nargin, 'struct'));
error(nargoutchk(0, 3, nargout, 'struct'));

% set default
if nargin<5
    if nargout==0,  disp = true;
    else            disp = false;     end
    if nargin<4,  samp = [];
        if nargin<3,  method = 'full';  end
    end
end

if ~any(strcmpi(method,{'full','grid','scale','radi','rand'}))
    error('sampledisk:methoderror',['unknown method ' method]);
end

if isempty(samp) && any(strcmpi(method,{'grid','radi','rand'}))
    % error('sampledisk:errorinput', ...
    %  'samp parameter must be entered with methods ''grid'', ''radi'' or ''rand''');
   samp = 0.5;
elseif ~isempty(samp) && any(strcmpi(method,{'full','scale'}))
    warning('sampledisk:warninginput', ...
        ['samp parameter ignored with method ' method]);
end

main computation

[x, y, w] = sampledisk_base(center, radius, method, samp);

if disp
    plotsampledisk(x, y, center, radius);
end

if nargin>=1,  varargout{1} = x;
    if nargin>=2,  varargout{2} = y;
        if nargin==3,  varargout{3} = w;  end
    end
end
end % end of sampledisk

Subfunction

%--------------------------------------------------------------------------
function plotsampledisk(x, y, center, radius)
ndisks = size(center,1);

for i=1:ndisks
    figure
    plot(x(i,:),y(i,:),'g.')
    axis([center(i,1)-1.1*radius(i) center(i,1)+1.1*radius(i) ...
        center(i,2)-1.1*radius(i) center(i,2)+1.1*radius(i)])
    hold on
    t = linspace(0,2*pi,1000);  r = ones(1,1000)*radius(i);
    [xx,yy] = pol2cart(t,r);
    xx=xx+center(i,1);   yy=yy+center(i,2);
    plot(xx,yy,'--');
    hold off
    axis square
end
end