一种随机放置圆圈至少D距离的算法

时间:2011-09-11 17:25:24

标签: algorithm random

我正在尝试研究如何编写一个算法来随机放置R半径的圆,在任意尺寸的2d矩形中,这样每个放置的圆与矩形中的其他圆相距至少D距离< / p>

矩形不需要填充,更具体的旧圆圈可能会被破坏,所以我需要能够放置一个新的圆圈,尊重我已经放置的最后N个圆圈的位置(比如说)例如,如果它不能满足这些条件,那么我可以单独处理它。

任何人都可以帮助我推断出这样的算法,或者指出一些可能涵盖这种算法的研究吗?

4 个答案:

答案 0 :(得分:1)

1 Place circle at random location
2 Loop over previous circles
3     if too close
4        delete new circle
5        goto 1
6 if need more circles
7     goto 1

确定是否有空间

Choose resolution required, say delta = D/100
for( x = 0; x < rectangle_size x += delta )
   for( y = 0; y < rectangle_size y += delta )
      unset failed
      loop over circles
           if x,y less than 2D from circle
              set failed
              break from circle loop
       if not failed
            return 'yes there is room'
return 'no, there is no room'

如果您希望有这么多圈子,只剩下几个洞,有新圈子的空间,那么您可以这样做

clear candidates
Choose resolution required, say delta = D/100
for( x = 0; x < rectangle_size x += delta )
   for( y = 0; y < rectangle_size y += delta )
      unset failed
      loop over circles
           if x,y less than 2D from circle
              set failed
              break from circle loop
       if not failed
            add x,y to candidates
if no candidates
    return 'there is no room'
randomly choose location for new circle from candidates

答案 1 :(得分:0)

1. Pick random startingspot.
2. Place circle
3. Move in random direction at least D
4. Goto 2 until distance to edge is < D or the distance to another circles center is < 2D

答案 2 :(得分:0)

首先想到的算法是simulated annealing。基本上,你从最简单的解决方案开始,可能只是一个网格,然后你以随机的方式“摇动盒子”,看看你是否得到了更好的答案。首先你做大摇,然后逐渐变小。它听起来有点混乱,并不总能产生绝对最佳的解决方案,但是当计算密集的东西时,它通常在很短的时间内非常接近。

答案 3 :(得分:0)

这实际上取决于“随机”的含义。假设您希望尽可能接近均匀分布,您可能必须使用迭代解决方案,如建议的一个ravenspoint。将所有圆圈随机放置然后开始替换不符合距离条件的圆圈可能会稍快一些。

如果随机性不是那么重要 - 也就是说它只是必须“看起来”随机(如果你没有做一些科学的事情,这可能就好了),那么将你的空间格栅化并通过选择N来放置你的N个圆圈网格中的索引。您可以通过在将圆圈放置在网格中的位置添加一些噪声来使其稍微“随机”。这对于稀疏放置非常有用。