Search code examples
netlogo

Is there a way to set patch color for multiple patches with a single line of code in NetLogo?


Right now i am using

ask patch 1 1 [set pcolor grey]
ask patch 1 -1 [set pcolor grey]
ask patch -1 1 [set pcolor grey]
ask patch -1 -1 [set pcolor grey]

Is there a way to set patch color for multiple patches with a single line of code in NetLogo?


Solution

  • Simplest way:

    ask (patch-set patch 1 1 patch 1 -1 patch -1 1 patch -1 -1) [ set pcolor grey ]
    

    (The following has been updated in response to Seth's comment:)

    If this is being run from the observer (that is, it's not being run by any agent) you can use at-points like so:

    ask patches at-points [[1 1] [1 -1] [-1 1] [-1 -1]] [ set pcolor grey ]
    

    However, if this code is run by a turtle or a patch, the list of coordinates will be treated as relative to that agent. So patches at-points [[1 0] [0 1]] will give the patch to the right and the patch above the current agent. You can easily make a version that uses absolute coordinates:

    to-report patches-at-coords [ coordinates ]
      report (patch-set map [patch first ? last ?] coordinates)
    end
    

    and then use it like so:

    ask patches-at-coords [[1 1] [1 -1] [-1 1] [-1 -1]] [ set pcolor grey ]
    

    Regardless, unless you're doing this a lot, I'd go with the first method.