首页 文章

如何将贴片移动到靠近原点的空贴片?

提问于
浏览
2

我希望我的代码可以根据滑块中给出的海龟数量进行更多压缩,我希望将其重点放在原点上 . 如果这是我的代码

to solid
  set color blue
  set xcor random sqrt number-of-particles - number-of-particles / 2
  set ycor random sqrt number-of-particles - number-of-particles / 2
  ifelse any? patches with [pcolor = black and count turtles-here = 0]
  [move-to one-of patches with [pcolor = black and count turtles-here = 0]]
  [die]
end

我将如何包括它以便乌龟移动到最接近原点的空补丁?到目前为止我有

if number-of-particles < volume * volume
  [move-to one-of patches with [

1 回答

  • 1

    假设"origin",你的意思是 patch 0 0 ,这是一种方法:

    to move-near-origin
      let free-patches patches with [pcolor = black and not any? turtles-here]
      ifelse any? free-patches [
        move-to min-one-of free-patches [ distance patch 0 0 ]
      ]
      [ die ]
    end
    

    这两个关键部分是min-one-ofdistance .

    还要注意我已经用 not any? turtles-here 替换了 count turtles-here = 0 ,它基本上做同样的事情,但更具可读性 . 我还在 free-patches 变量中存储了 patches with [pcolor = black and not any? turtles-here] ,因此您不必重复自己并测试该条件的所有补丁两次 .

相关问题