function [xn,yn] = randvertex(x,y,npix)
%RANDVERTEX Adds random noise to the vertices of a polygon.
%	[XN,YN] = RANDVERTEX[X,Y,NPIX] adds uniformly distributed noise to
%	the coordinates of vertices of a polygon. The coordinates of the
%	vertices are input in X and Y, and NPIX is the maximum number of
%	pixel locations by which any pair (X(i),Y(i)) is allowed to deviate.
%	For example, if NPIX = 1, the location of any X(i) will not deviate
%	by more than one pixel location in the x-direction, and similarly for
%	Y(i). Noise is added independently to the two coordinates.
%
%   Copyright 2002-2020 Gatesmark
%
%   This function, and other functions in the DIPUM Toolbox, are based 
%   on the theoretical and practical foundations established in the 
%   book Digital Image Processing Using MATLAB, 3rd ed., Gatesmark 
%   Press, 2020.
%
%   Book website: http://www.imageprocessingplace.com
%   License: https://github.com/dipum/dipum-toolbox/blob/master/LICENSE.txt

% Convert to columns.
x = x(:);
y = y(:);

% Preliminary calculations.
L = length(x);
xnoise = rand(L,1);
ynoise = rand(L,1);
xdev = npix*xnoise.*sign(xnoise - 0.5);
ydev = npix*ynoise.*sign(ynoise - 0.5);

% Add noise and round.
xn = round(x + xdev);
yn = round(y + ydev);

% All pixel locations must be no less than 1.
xn = max(xn,1);
yn = max(yn,1);
