xpos(i) in Polygonal domain
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Andrew Newell
2012-1-3
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 个评论
更多回答(5 个)
Jose Jeremias Caballero
2012-1-3
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 个评论
Jose Jeremias Caballero
2012-1-3
%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
2012-1-3
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 个评论
Jose Jeremias Caballero
2012-1-3
%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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Elementary Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!