During my ICPSR 2009 ABM Workshop a student asked me if there is a way to reveal the underlying grid structure in Netlogo. My response was, "Yes, but it's not built in." The point is that in certain models the patches don't do anything, but to check movement and interaction rules are working properly it would sometimes be helpful so be able to measure distances by sight. Of course this might also be helpful even if the patches are displaying values. So in this post I provide two code snippets that allow one to see the gridspace made up of patches.

The first technique is just one line entered into your go method that tells alternate patches to set their color to a dark gray.

ask patches with [(pxcor + pycor) mod 2 = 0] [set pcolor 2]

Of course it's easy to see how to make this any color. And if you want to specify two colors you can use the following.

ask patches [
  ifelse (pxcor + pycor) mod 2 = 0
    [ set pcolor 2 ]
    [ set pcolor 3 ]

That's pretty basic stuff and something like that can be found in the model library, but it only works if you don't need the patches to use their color to represent information.

Sometimes you might want both a grid and useful patches and that can be achieved by including the following method in your procedures. In the setup method just put the command "draw-gridlines" which calls the method of the same name written elsewhere in your code. Note that because the following method uses turtle pens it may interfere with you use of pens elsewhere in your code.

to draw-gridlines
  crt world-width [
    set ycor min-pycor
    set xcor who + .5
    set color 2
    set heading 0
    fd world-height
  crt world-height [
    set xcor min-pxcor
    set ycor who + .5
    set color 2
    set heading 90
    fd world-width

You can see that it creates agents that start on grid boundaries and using the pen function they draw lines as they move across the world. Inside the same command to create the drawing agents they do their work and then are killed so as not to interfere with the rest of your model. I have used a similar trick to create wire diagram landscapes in Netlogo 3D by having the drawing agents plot a function as they move across the world.