NetLogo技术问询:如何为指定区域的Patches设置颜色并命名
在NetLogo中为指定区域的补丁设置颜色并命名
你提供的代码存在逻辑错误:pycor < -11 and pycor > 11 没有任何补丁能同时满足这个矛盾条件,pxcor的判断同理。以下是修正后的实现方案:
一、修正指定区域的颜色设置
根据需求选择对应条件:
- 选择四个角落区域(pxcor和pycor均超出-11~11范围):
ask patches with [ (pxcor < -11 or pxcor > 11) and (pycor < -11 or pycor > 11) ] [ set pcolor orange ]
- 选择中心区域(pxcor和pycor均在-11~11范围内):
ask patches with [ pxcor > -11 and pxcor < 11 and pycor > -11 and pycor < 11 ] [ set pcolor orange ]
二、为指定区域补丁命名
NetLogo没有默认的补丁名称属性,需先自定义补丁变量,再为目标区域赋值:
- 在代码开头定义补丁专属变量:
patches-own [ patch-name ] ; 每个补丁独立的名称属性
- 设置颜色的同时为补丁命名:
- 给指定区域统一命名:
ask patches with [ pxcor > -11 and pxcor < 11 and pycor > -11 and pycor < 11 ] [ set pcolor orange set patch-name "中心区域" ]
- 为不同子区域设置不同名称:
; 左下角落 ask patches with [ pxcor < -11 and pycor < -11 ] [ set pcolor orange set patch-name "左下角落" ] ; 右下角落 ask patches with [ pxcor > 11 and pycor < -11 ] [ set pcolor orange set patch-name "右下角落" ] ; 左上角落 ask patches with [ pxcor < -11 and pycor > 11 ] [ set pcolor orange set patch-name "左上角落" ] ; 右上角落 ask patches with [ pxcor > 11 and pycor > 11 ] [ set pcolor orange set patch-name "右上角落" ]
内容的提问来源于stack exchange,提问作者Jaco
相关产品推荐
相关产品推荐

