i want x,y coordinates which are randomly generated between 1 to 300 ,condition is distance between every coordinate is >20. how to proceed further

8 visualizaciones (últimos 30 días)
i=5 ; %% no. of random coordinates
xc = round((1 + (60-1)*rand(i,1)),0); %% random X- coordinates
yc = round((1 + (40-1)*rand(i,1)),0); %% random Y- coordinates
c = [xc,yc] ; %% storing random coordinates in a matrix
d = pdist(c); %% pdist finds distance between each and every coordinate (all possible distances)
z= squareform(d);%% it gives a 5*5 matrix containing distances
v = 500; %%
n = size(z,1);
z(1:(n+1):end) = v; %% replacing diagonal elements by some scalar because all diag elements wiil be zero
index = find(z<20) %% find which index number in matrix which are less than 20

Respuesta aceptada

Akira Agata
Akira Agata el 14 de Dic. de 2018
How about the following?
BWremain = true(300);
% Number of random coordinates
N = 5;
% Selected coordinates are stored these variables
row = nan(N,1);
col = nan(N,1);
for kk = 1:N
BW = false(300);
p = find(BWremain);
p = p(randperm(numel(p),1));
BW(p) = true;
BW = bwdist(BW) > 20;
BWremain = BWremain & BW;
[row(kk),col(kk)] = ind2sub(size(BWremain),p);
end
Selected coordinates and circles (R = 20) around them looks like this.
selectedPoints.png
  3 comentarios
Akira Agata
Akira Agata el 7 de En. de 2019
Sorry for my late response.
I believe "so that distance ... is < 20" in your question must be a typo of "... is > 20" (because answer becomes Inf). If so, the problem can be treated as an well-known "Packing problem".
Packing problem (Wikipedia)
Image Analyst
Image Analyst el 7 de En. de 2019
So it could be very easy - if your radii are a constant. Just do a normal circle packing in a honeycomb pattern.
If your radii vary, and they can go all the way down to zero, then you can fit an infinite number in the box because the circles can get infinitesimally small so an unlimited number will fit into the remaining spaces.
If you have some other case, like radii are variable and can range from 3 to 50, then it's much harder. To solve that, I'd probably use a Monte Carlo simulation though it will give a practical solution, not the solution with the most discs in it. You could do a simulation with a high resolution digital image where you call bwdist() after each disc is placed to find out what the max radius possible at that time is, and then place the next circle at the smallest location where that disc's radius could fit (which is at the place where the Euclidian distance transform equals that disc's radius). (If you're a beginner, I don't blame you if you don't understand a word I said.)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 14 de Dic. de 2018
You can use a strategy where you get a point, then use sqrt() to get the distances from all prior points. If it's farther away than 20 from all of them, then keep it, otherwise throw it away and try a new point.
pointsToPlace = 5 ; % # of random coordinates we need to place
% Preallocate points
x = zeros(1, pointsToPlace);
y = zeros(1, pointsToPlace);
loopCounter = 1;
maxIterations = 10000; % Number of tries before giving up.
numberPlaced = 0; % No points placed yet.
while numberPlaced < pointsToPlace && loopCounter < maxIterations
% Get new coordinate
xProposed = round((1 + (60-1)*rand()),0); %% random X- coordinates
yProposed = round((1 + (40-1)*rand()),0); %% random Y- coordinates
if loopCounter == 1
% First one automatically gets added of course.
numberPlaced = 1;
x(numberPlaced) = xProposed;
y(numberPlaced) = yProposed;
else
% Compute distance to all prior coordinates.
distances = sqrt((xProposed - x(1:numberPlaced)) .^ 2 + (yProposed - y(1:numberPlaced)) .^2);
% If less than 20, add it
if min(distances > 20)
numberPlaced = numberPlaced + 1;
x(numberPlaced) = xProposed;
y(numberPlaced) = yProposed;
end
end
loopCounter = loopCounter + 1;
end
fprintf('Placed %d points after %d iterations\n', numberPlaced, loopCounter-1);
plot(x, y, 'b*', 'LineWidth', 2, 'MarkerSize', 14);
grid on;
xlim([0, 60]);
ylim([0, 40]);
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
0001 Screenshot.png

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by