xpos(i) in Polygonal domain
3 views (last 30 days)
Show older comments
Hi, I need to define random point positions within a polygonal domain. So far, I am only able to realize the random positions of my points in the quadratic plot window with
for i=1:6000 xpos(i)=100*rand; ypos(i)=100*rand; end
So I want to arrange these points arbitrary within a polygon. I would really appreciate any help.
0 Comments
Accepted Answer
Andrew Newell
on 3 Jan 2012
You could generate random points in a rectangle enclosing the polygon and then choose the points that lie inside using inpolygon. The documentation for this function even provides an example that is similar to what you want to do.
0 Comments
More Answers (5)
Jose Jeremias Caballero
on 3 Jan 2012
xpos=100*rand(1,5);
ypos=100*rand(1,5);
plot([xpos xpos(1)],[ypos ypos(1)])
for i=1:length(xpos)
text(xpos(i),ypos(i),[num2str(xpos(i)),',',num2str(ypos(i))])
end
0 Comments
Jose Jeremias Caballero
on 3 Jan 2012
%random_polygon
xpos=100*rand(1,6000);
ypos=100*rand(1,6000);
xmin=min(xpos)-2; ymin=min(ypos)-2;
xmax=max(xpos)+2; ymax=max(ypos)+2;
X=[xmin xmin xmax xmax xmin];
Y=[ymin ymax ymax ymin ymin];
plot(X,Y)
for i=1:length(xpos)
text(xpos(i),ypos(i),'*')
end
Jose Jeremias Caballero
on 3 Jan 2012
a=100;
xpos=a*rand(1,6000);
ypos=a*rand(1,6000);
X5 = [10 30 41 50 65 70 40 15 5 10];
Y5 = [37 35 15 10 15 37 58 60 50 37];
figure(gcf)
T=xpos<max(X5) & min(X5)<=xpos;
M=xpos(T);
N=ypos(T);
n=length(M);
for i=1:n
for j=1:n-i
if M(j)>=M(j+1)
aux=M(j);
auy=N(j);
M(j)=M(j+1);
N(j)=N(j+1);
M(j+1)=aux;
N(j+1)=auy;
end
end
end
U=N<max(Y5) & min(Y5)<N;
M1=M(U);
N1=N(U);
for i=1:length(X5)
normas(i)=norm([X5(i) Y5(i)]);
end
k=1;
for i=1:length(M1)
if (min(normas)<norm([M1(i) N1(i)]) && norm([M1(i) N1(i)])<max(normas))
M2(k)=M1(i);
N2(k)=N1(i);
k=k+1;
end
end
plot(X5,Y5)
for i=1:length(M2)
text(M2(i),N2(i),'*')
end
grid
axis('image')
axis([min(X5)-20 max(X5)+20 min(Y5)-20 max(Y5)+20])
0 Comments
Jose Jeremias Caballero
on 3 Jan 2012
%thaks Andrew Newell.
a=100;
xpos=a*rand(1,6000);
ypos=a*rand(1,6000);
X5 = [10 30 41 50 65 70 40 15 5 10];
Y5 = [37 35 15 10 15 37 58 60 50 37];
interno = inpolygon(xpos,ypos,X5,Y5);
plot(X5,Y5,xpos(interno),ypos(interno),'.r')
0 Comments
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!