Azzera filtri
Azzera filtri

RandomPoints &condition distance

7 visualizzazioni (ultimi 30 giorni)
Marwen Tarhouni
Marwen Tarhouni il 29 Gen 2017
Commentato: Image Analyst il 20 Feb 2022
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

Jan
Jan il 30 Gen 2017
Modificato: Jan il 14 Feb 2017
Current version, [Last EDITED, 01.02.2017 16:52 UTC]
function [X, Y, D] = GetPointsRandom(nWant, XWidth, YWidth, R)
X = zeros(nWant, 1);
Y = zeros(nWant, 1);
dist_2 = R ^ 2; % Squared once instead of SQRT each time
iLoop = 1; % Security break to yoid infinite loop
nValid = 0;
while nValid < nWant && iLoop < 1e6
newX = XWidth * (rand - 0.5);
newY = YWidth * (rand - 0.5);
if all(((X(1:nValid) - newX).^2 + (Y(1:nValid) - newY).^2) > dist_2)
% Success: The new point does not touch existing points:
nValid = nValid + 1; % Append this point
X(nValid) = newX;
Y(nValid) = newY;
end
iLoop = iLoop + 1;
end
% An error is safer than a warning:
if nValid < nWant
error('Cannot find wanted number of points in %d iterations.', iLoop)
end
% [Edited start] "figure" inserted, 'Parent' used, 'XGrid' inserted:
FigH = figure;
AxesH = axes('XTick', -100:20:100, 'YTick', -100:20:100, ...
'XLim', [-100, 100], 'YLim', [-100, 100], ...
'XGrid', 'on', 'YGrid', 'on', 'NextPlot', 'add', ...
'Parent', FigH);
plot(X, Y, 'b*', 'Parent', AxesH);
% [EDITED end]
if nargout > 2
% D = pdist([X, Y]); % Faster with statistics toolbox
D = sqrt(bsxfun(@minus, X, X.') .^ 2 + bsxfun(@minus, Y, Y.') .^ 2);
end
end
Call this e.g. as:
[X, Y, D] = GetPointsRandom(1000, 1e6, 1e6, 6)
The list of coordinates is expanded, when a new random point does not touch any exitsing points.
If you do not have the Statistics Toolbox for pdist, use e.g. http://www.mathworks.com/matlabcentral/fileexchange/15145-distance-matrix.
The suggested method for the distance matrix computes the full matrix, although it is symmetric. For in the number of points is small (< 10000), this is not tragic.
This rejection method works reliably and fast up to a number of about 700 points for a side length of 200 and a diameter of 6. If it fails due to reaching the iteration limit, check if increasing the limit helps. If not, the area is nearly filled up by circles and there will not be a solution for your problem.
  2 Commenti
cheulhee kwon
cheulhee kwon il 7 Mar 2019
hello sir. it is very helpful to my project.
But, i'm very poor at this matlab.
can you answer my question?
if you can't
how to measure two point distance
A(fixed) and B(random)
i wanna fix A point and
make random B point
and wanna find the distance
thank u for reading this
Image Analyst
Image Analyst il 7 Mar 2019
Try
distance = hypot(A, B)
or
distance = sqrt((ax-bx).^2+(ay-by).^2)

Accedi per commentare.

Più risposte (3)

John BG
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
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
Marwen Tarhouni il 21 Feb 2017
Modificato: Walter Roberson il 22 Feb 2017
@ Image Analyst
Error in GetPointsRandom (line 37)
centers=[X,Y];
radii=15;
viscircles(centers, radii);
Walter Roberson
Walter Roberson il 22 Feb 2017
centers=[X,Y];
radii = 15 * ones(size(centers,1), 1);
viscircles(centers, radii);

Accedi per commentare.


Manik K
Manik K il 20 Feb 2022
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
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.

Accedi per commentare.

Categorie

Scopri di più su Marine and Underwater Vehicles in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by