【问题标题】:select all patches at distance x from turtle选择距离海龟 x 处的所有补丁
【发布时间】:2016-12-06 15:15:48
【问题描述】:

我想从模拟中的所有海龟随机生成距离 eps 处选择所有补丁,并将它们的颜色重置为黄色。这实际上在模拟中围绕每个海龟绘制了一圈补丁。我尝试了几种不同的选择,但都没有成功。通过阅读这个论坛,我发现了一些看起来很有希望但仍然存在一些问题的代码(在此处发布)。我感谢任何有关调整此代码或使用其他方法来解决此问题的建议。

let eps2 eps
foreach [ eps2 ]
  [
      ask patches with 
  [
        distance myself > eps2 - 0.5 and
        distance myself < eps2 + 0.5
  ]
  [
    set pcolor yellow
  ]
]

eps 是一个海龟变量,因此使用 let 命令可以规避在补丁上下文中使用海龟变量。

foreach 命令无法识别 eps,因为它不是常量,我可以在这里使用其他命令吗?

【问题讨论】:

    标签: distance netlogo patch


    【解决方案1】:

    你可以使用list(见下文),但是......你为什么想要一个列表?就目前而言,没有必要使用列表。

    to setup
      ca
      crt 1
      ask turtle 0 [test]
    end
    to test
    let eps2 10
    foreach (list eps2 )  ;you can use `list`
      [
          ask patches with 
      [
            distance myself > eps2 - 0.5 and
            distance myself < eps2 + 0.5
      ]
      [
        set pcolor yellow
      ]
    ]
    end
    

    附录:

    由于您表明您实际上不需要该列表,您可以尝试以下方式:

    to test2
      ca
      crt 1
      ask encirclingPatches turtle 0 10 1 [set pcolor yellow]
    end
    
    to-report encirclingPatches [#t #dist #width]
      let _w2 (#width / 2)
      report patches with [
        distance #t > #dist - _w2
        and
        distance #t < #dist + _w2
      ]
    end
    

    【讨论】:

    • 谢谢,这很好用。要回答您的问题,我不需要创建 eps2 列表。我主要关心的是选择海龟距离 eps (+/- 0.5) 内的补丁。我愿意接受其他方式来编写本节的代码,但这是我能找到的最接近我正在尝试做的事情的例子。以前我使用半径内但找不到只选择周边点的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多