如何获取特定种植类型斑块的坐标并差异化着色?NetLogo技术问询
解决方案
1. 收集目标斑块的坐标到myland列表
你原来的代码是提取种植类型,要改成提取坐标的话,只需把[plantation]替换为[list pxcor pycor],这样列表中的每个元素都是包含x、y坐标的子列表。
如果locals是特定海龟种群,代码调整为:
ask locals [ set myland [list pxcor pycor] of patches in-radius 5 with [plantation != "Cows"] ]
如果myland是全局共享列表,直接去掉ask locals:
set myland [list pxcor pycor] of patches in-radius 5 with [plantation != "Cows"]
要是后续需要直接操作斑块(比如着色),更推荐把斑块对象本身存入列表,后续调用更便捷:
ask locals [ set myland patches in-radius 5 with [plantation != "Cows"] ]
2. 对目标斑块差异化着色
情况1:myland中存储的是斑块对象
直接遍历列表,根据种植类型设置对应颜色:
ask myland [ if plantation = "Wheat" [ set color yellow ] if plantation = "Corn" [ set color green ] if plantation = "Soybean" [ set color brown ] ; 可继续添加更多种植类型的颜色映射 ]
也可以用case语句让代码更简洁(NetLogo 6.0及以上版本支持):
ask myland [ case plantation [ "Wheat" -> set color yellow "Corn" -> set color green "Soybean" -> set color brown default -> set color gray ; 给未匹配的类型设置默认色 ] ]
情况2:myland中存储的是坐标列表
需要先将坐标转回斑块实例再着色:
foreach myland [ coords -> ask patch (first coords) (last coords) [ if plantation = "Wheat" [ set color yellow ] if plantation = "Corn" [ set color green ] if plantation = "Soybean" [ set color brown ] ] ]
内容的提问来源于stack exchange,提问作者bksnlow
相关产品推荐
相关产品推荐

