通过存储在列表中的坐标访问补丁 [英] Accessing patch through coordinates stored in a list
问题描述
我有一个列表,用于存储补丁的坐标列表.
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 ]
这会产生一个错误,要求修补程序使用数字而不是列表或块.如何使NetLogo理解(项目1的coor)是两个数字的集合?
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?
还有其他方法可以解决此问题吗?我也尝试过使用'table'扩展名,但是同样的问题来了.
Any other way to approach this? I have also tried using 'table' extension for this but there also same issue was coming.
推荐答案
只需使用适当的报告程序即可:
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.
这篇关于通过存储在列表中的坐标访问补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!