Accessing patch through coordinates stored in a list

萝らか妹 提交于 2019-12-24 11:29:33

问题


I have a list which is storing the list of coordinates of patches.

let coor [ [0 1] [ 1 0] [1 -1] ]

Now I want to ask the patch at the location of the 2nd element of the list to change its color to red.

ask patch (item 1 coor) [ set pcolor red ] 

This gives an error that patch expects a number instead of a list or block. How to make NetLogo understand that the (item 1 coor) is a set of two numbers?

Any other way to approach this? I have also tried using 'table' extension for this but there also same issue was coming.


回答1:


Just use an appropriate reporter:

to-report patch-at-xy [#xy]
  let _x item 0 #xy
  let _y item 1 #xy
  report patch _x _y
end

This requires a tiny change in your code above:

to test
  let coor [ [0 1] [ 1 0] [1 -1] ]
  ask patch-at-xy (item 1 coor) [ set pcolor red ] 
end

That said, you almost certainly should be storing a list of patches rather than of their coordinates.



来源:https://stackoverflow.com/questions/41276117/accessing-patch-through-coordinates-stored-in-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!