## How can we provide minimum distance i.e. 20 between each random generated node?

SHUAB KHAN

### SHUAB KHAN (view profile)

さんによって質問されました 2017 年 11 月 8 日

### Jan (view profile)

さんによって 編集されました 2019 年 5 月 10 日
John D'Errico

### John D'Errico (view profile)

さんの 回答が採用されました
No_of_clusters = 25
no_sensor_box = 8
for i=1:no_of_clusters
for j=1:no_sensor_box
Xloc(s)=randi([x_loc1(i)+1,x_loc2(i)-1],1,1);
Yloc(s)=randi(([y_loc1(i)+1,y_loc3(i)]-1),1,1);
s=s+1;
end
end
This code generates nodes at some random locations, but I want the minimum distance (i.e.=20) between two nodes that means the minimum distance between each node are not less than 20. How to provide minimum distance between two nodes in this?

Torsten

### Torsten (view profile)

2017 年 11 月 8 日
How is the distance between two nodes s1 and s2 defined ?
d(s1,s2) = sqrt((Xloc(s1)-Xloc(s2))^2+(Yloc(s1)-Yloc(s2))^2)
?
Best wishes
Torsten.

サインイン to comment.

## 2 件の回答

### John D'Errico (view profile)

2017 年 11 月 8 日
採用された回答

Um, there isn't a way. Well, not to do it by one simple call. You will need to write code.
Given the set of nodes already generated, just generate a new random point, subject to no constraints. Use a uniform random generator, like rand. Compute the distance to every other node. This is trivial of course.
Then if the new node is too close to any other node, toss it away, into the bit bucket. Repeat until you find a new node that is acceptable.
Eventually, you will find that it becomes really difficult to choose new nodes. You are done then.
Sorry, but that is what you will need to do. Could you do it more efficiently? Well, yes. I suppose in theory, you could do so. You would need to build a triangulation of the region that would define the region where new nodes may be validly chosen, because they are sufficiently faraway from any existing node. Then choose a new point uniformly from the valid domain. Given the new node, since it was in the valid set, exclude all portions of the existing triangulation that are within a radius of r from the new node, reducing the valid set.
This may become somewhat computationally intensive to do the exclusion, because that triangulation may become a rather messy one. But it would get smaller all the time too.
So, COULD you solve your problem without using a simpler rejection scheme? Yes. But not worth the effort unless you really understand how to work with triangulations of the plane. Just use the first scheme I proposed.

#### 0 件のコメント

サインイン to comment.

2017 年 11 月 8 日

### Jan (view profile)

2019 年 5 月 10 日

I think Jan Simon's answer will at least partially solve your problem:
Best wishes
Torsten.

SHUAB KHAN

### SHUAB KHAN (view profile)

2017 年 11 月 9 日
Thanks Torsten, It'll help me a lot.

サインイン to comment.

Translated by