xpos(i) in Polygonal domain

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.

 Risposta accettata

Andrew Newell
Andrew Newell il 3 Gen 2012

0 voti

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.

Più risposte (5)

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
Hamid
Hamid il 3 Gen 2012

0 voti

Dear Jose,
I am not really sure, whether this is the solution for my problem. Probably I didn't phrase it well. I need to define 6000 points within a ploygon domain! The polygon should be fixed and predefined with 8 edges.
%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

1 Commento

Hamid
Hamid il 3 Gen 2012
Dear Jose, First let me thank you for your efforts to help me.
This answer gives me a random polygon. I already have a ploygon defined as
X=[10 30 41 50 65 70 40 15 5 10];
Y=[37 35 15 10 15 37 58 60 50 37];
plot(X,Y)
But I want to define my random points so that they are inside this polygon.

Accedi per commentare.

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])
%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')

Categorie

Scopri di più su Numerical Integration and Differential Equations in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by