获取其他代理的相对坐标



以尊重世界包装的方式获取一个代理相对于另一个代理坐标的最佳方法是什么?

也就是说,朴素的解决方案是:

to-report relative-xcor [ other-agent ]
  report [ xcor ] of other-agent - xcor
end

(ycor类似(该代码的问题在于,例如,当主代理位于屏幕的最右侧而other-agent位于最左侧时,它将是错误的。

想法?到目前为止,我最好的主意是使用towards和三角学,但我想知道是否有更好的方法。

编辑:

举个例子来说明为什么上面是错误的,假设你的世界有min-pxcor = -5max-pxcor = 5,并且有包装。如果我们在 xcor = 5 处有turtle 0,在 xcor = -5 时有turtle 1,那么[ relative-xcor turtle 1 ] of turtle 0会给出-10,而正确答案是 1(由于世界包装(。我想我的问题中隐含的是它应该给出最小的(按绝对值(相对坐标。

(OP 在这里( 所以到目前为止,我想出的唯一解决方案是:

to-report rel-xcor [ other-agent ]
  report ifelse-value (self = other-agent) [
    0
  ] [
    0 - (sin towards other-agent) * distance other-agent
  ]
end
to-report rel-ycor [ other-agent ]
  report ifelse-value (self = other-agent) [
    0
  ] [
    0 - (cos towards other-agent) * distance other-agent
  ]
end

(回想一下,由于网络徽标角度是翻转的,cos给出 y 坐标,sin给出 x(

但这似乎过于复杂。但是,如果有人偶然发现这个问题想知道同样的事情,这就是我目前正在使用的。不过,我很乐意接受一个更好的答案。

最新更新