RandomPoints &condition distance
Mostra commenti meno recenti
Dear
I need to draw a figure that contains 20 randomized points in an area [-100 100]. If we have the distance between two points less than 6 meters .It is necessary to repeat the randamization of the points
Then each point must know its distance from the other points
close all;
clear all;
clc;
set(gca,'xtick',-100:20:100);
set(gca,'ytick',-100:20:100);
axis([-100 100 -100 100]);
X = 200 * rand(1,20) - 100;
Y = 200 * rand(1,20) - 100;
plot(X, Y, 'b*');
grid on
hold on
datacursormode
%calculate the distance between a point and the other points
%exemple the distance between (A and B) & (A and C) &(A and D) & (A and E)...
%also (B and A) & (B and C) &(B and D) & (B and E).......
distance=[];
for i=1:20
dist = sqrt((X(i)-X(:)).^2+(Y(i)-Y(:)).^2);
distance=[distance dist];
end
thank you
[Moved from asnwer section:]
My question is to display an imege of 20 randomized points and if we have the distance between two points <6meters I repeat the randomization of these two points. And after all if the condition is fulfilled. Each point must know the distance with the other 19 points so I want to find a distance matrix in the workspace that contains 20 row and 20 column as indicated my code.
It is varied that in my code I find in the workspace the matrix distance that is to say each point know its distance with respect to the other points but the condition so that if the distance between two points is <6meters is not realized
I would like my question to be clear
Risposta accettata
Più risposte (3)
John BG
il 31 Gen 2017
Modificato: John Kelly
il 31 Gen 2017
Marwen
please test the following
clear all;clc;
format bank
rng('Shuffle')
figure;ax=gca;ax.DataAspectRatio=[1 1 1]
ax.XLim=[-100 100];ax.YLim=[-100 100];
ax.XTick=[-100:20:100];ax.YTick=[-100:20:100];grid on;hold all;
R0=6;a=linspace(0,2*pi,20);xc=R0*cos(a);yc=R0*sin(a);
X=zeros(1,20);Y=zeros(1,20);
x_grid=[-100+R0:1:100-R0];y_grid=[-100+R0:1:100-R0]; % avoid circles hitting frame
[X_grid,Y_grid]=meshgrid(x_grid,y_grid);
X_grid0=X_grid;Y_grid0=Y_grid;
P=[X_grid(:)';Y_grid(:)'];
[sz1P sz2P]=size(P)
xc2_base=2*R0*cos(a);yc2_base=2*R0*sin(a);
xc1_base=R0*cos(a);yc1_base=R0*sin(a);
for k=1:1:20
[sz1P sz2P]=size(P);
nP = randi(sz2P,1,1);
X(k)=P(1,nP);Y(k)=P(2,nP);
xc1=xc1_base+X(k);yc1=yc1_base+Y(k);
in=inpolygon(P(1,:),P(2,:),xc1,yc1);
in=in(:)';
P(:,find(in>0))=[];
plot(xc1,yc1,'b'); % R0 radius
plot(X(k),Y(k),'r*'); % centre circle
end

.
now regardless of the amount of the amount of times tested, the points are further than R0 each other, any pair, first run, no repetitions needed.
This removes the 'gamble' factor that Simon wants you to include in your testing.
.
L2=combinator(20,2);
Dmatrix=reshape(((X(L2(:,2))-X(L2(:,1))).^2+(Y(L2(:,2))-Y(L2(:,1))).^2).^.5,[20 20]);
D2=Dmatrix+100*eye(20);
D2(D2<6)
testing with the linear distance vector that I use, no need for the diagonal nulls
L=combinator(20,2,'c');
relD2=((X(L(:,2))-X(L(:,1))).^2+(Y(L(:,2))-Y(L(:,1))).^2).^.5
find(relD2<R0)
therefore
I consider this to be a valid answer that does not include faith in not hitting a toss of points too close.
Appreciating time and attention,
John BG
Image Analyst
il 20 Feb 2017
If you have list of the circle centers and radii, you can simply use the viscircles() function to plot the circles.
viscircles(centers, radii);
2 Commenti
Marwen Tarhouni
il 21 Feb 2017
Modificato: Walter Roberson
il 22 Feb 2017
Walter Roberson
il 22 Feb 2017
centers=[X,Y];
radii = 15 * ones(size(centers,1), 1);
viscircles(centers, radii);
Manik K
il 20 Feb 2022
0 voti
Hi, @John BG
I would like to to this for multiple species ( particles of different sizes ) in box.
I have succesfully implementes this with one by one random points generation and selection condition. but it is very slow.
could you check weather it can be done with your combinatorics method
thanks
manik
1 Commento
Image Analyst
il 20 Feb 2022
John BG hasn't been seen here in a long time. Not sure what you mean by different species, but you can pass a 'MarkerSize' property into plot(), otherwise you should start a new question and include your code and explain what you need that is different than your (hopefully very well commented) code.
Categorie
Scopri di più su Nearest Neighbors in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!