MATLAB: Creating random points along polygon boundary

polygon point random generation

Hello. I am trying to write a script that will create random points along the boundary of an input polygon.
Right now I have a script that is able to generate random points within a polygon – it uses the same process here:
What it essentially does is employs a loop that creates random points and then checks if they are within the polygon, if not then it continues until it finds a point that is within and loops over it again.
The function inpolygon is able to identify whether or not a point is inside a polygon, on the border of a polygon, or niether. From its helpfile:
[IN ON] = inpolygon(X,Y,XV,YV) returns a second matrix, ON, which is the size of X and Y.
ON(p,q) = 1 if the point (X(p,q), Y(p,q)) is on the edge of the polygonal region; otherwise ON(p,q) = 0.
Using the second output, ON, to check if one of the randomly generated points is on the border is not very efficient.
Can anyone think of a better way to generate random points along the border of a polygon?
My polygon is essentially a 2 x 894 array with the first row being all of the x coordinates and the second row being all of the y coordinates with each column making up a point.
Here is the script for generating random points within a polygon. M.X and M.Y serve as the X and Y vectors of the polygon points.
for j=1:iterate
n=2;
PX = zeros(1,n);
PY = zeros(1,n);
for i=1:n
flagIsIn = 0;
while ~flagIsIn
PX(1,i) = (b-a).*rand(1,1) + a;
PY(1,i) = (d-c).*rand(1,1) + c;
[IN ON] = inpolygon(PX(1,i),PY(1,i),M.X,M.Y);
if ON == 1
flagIsIn = 1
end
end
end
end

Best Answer

The interparc function in the FEX makes this pretty easy:
pt = interparc(rand(10,1), x, y, 'linear');
plot(x,y,'b', pt(:,1), pt(:,2), 'r.');