2

Is there a way to create outlines for patches? I want a simple grid with white background and black lines.

I managed to create a grid from a code I found but the turtles walked on the lines instead of inside the squares.

Noga
  • 21
  • 1
  • Can you please show your work first? – cconsta1 Jun 13 '23 at 12:46
  • Patches do not have shapes in NetLogo by design. If you'd like to draw a grid around patches, you would need to create a turtle, use the pen primitives, and walk over the edges of all patches with that turtle, which can be done with a simple loop. Alternatively, you can try to create a checkerboard with a code like `ask patches with [(pxcor + pycor) mod 2 = 0][set pcolor white]`. – umit1010 Jun 13 '23 at 13:36
  • Thanks. It just seemed such a trivial request to me that I was sure there would be such an option. – Noga Jun 14 '23 at 13:20

1 Answers1

4

There is a somewhat silly but effective way to get patch outlines using a specific turtle shape and the stamp primitive.

Option 1

The custom shape is a square outline that fills the full size of the turtle's area, which at a size of 1 (the default) is the same as a patch area. I called the shape "square-outline" in my sample model. Here is the relevant code:

to test
  clear-all
  ask patches [ sprout 1 [
    set shape "square-outline"
    if outline-color != black [
      set color outline-color 
    ]
    stamp
    die
  ]]
end

I use stamp and have the turtles die so you don't have to worry about them in the rest of your code. You could leave them alive if that made sense for your model.

And here is the sample model to play with on NetLogo Web and get the shape from if you want to try it out. You can export that nlogo file and use the Import from Model... feature in the turtle shapes editor to get the shape into your model.

Option 2

Edit: I updated the model with an option to hide/show each edge of the outline. I did this by adding a rotating "patch-edge" shape that only occupied one edge of the turtle shape. I then can set each edge by setting the turtle's heading to one of 0 (top), 90 (right), 180 (bottom), or 270 (left) before stamping it.

to test
  clear-all
  ask patches [ sprout 1 [
    if outline-color != black [
      set color outline-color
    ]
    ifelse top-edges? and right-edges? and bottom-edges? and left-edges? [
      stamp-square-outline
    ] [
      if top-edges?    [ stamp-edge 0   ]
      if right-edges?  [ stamp-edge 90  ]
      if bottom-edges? [ stamp-edge 180 ]
      if left-edges?   [ stamp-edge 270 ]
    ]
    die
  ]]
end

to stamp-square-outline
  set shape "square-outline"
  stamp
end

to stamp-edge [h]
  set shape "patch-edge"
  set heading h
  stamp
end

Option 2 Expanded Example

I wanted to make a more extensive example of outlining patch shapes so I extended the sample model with a make-shapes procedure to create a blob of grey patches, then to use the neighbors4 primitive and towards primitive to draw borders between different patches. The sample model code is updated, and here is the relevant portion:

breed [decors decor]

to make-shapes
  clear-all
  
  ; setup the decorator to use to draw edges
  create-decors 1
  let decorator one-of decors

  ; create some grey blobs of shapes we want to outline
  let start-count 0.1 * count patches
  ask n-of start-count patches [ set pcolor grey ]
  ask patches with [pcolor = black] [
    let odds (0.1 + count neighbors4 with [pcolor = grey]) / 5
    if (random-float 1) < odds [ set pcolor grey ]
  ]
  
  ; detect the edges between grey and black patches and outline them
  ask patches with [pcolor = grey] [
    ask decorator [ 
      move-to myself 
      ifelse outline-color != black [
        set color outline-color
      ] [
        set color one-of base-colors 
      ]
    ]
    ask neighbors4 with [pcolor = black] [
      ; `towards` gets a heading from the black patch back, so some
      ; simple math reverses it for stamping the edge.
      let h ((towards myself) + 180) mod 360
      ask decorator [ stamp-edge h ]
    ]
  ]
  ; darker grey just looks a little nicer
  ask patches with [pcolor = grey] [ set pcolor grey - 3 ]
  
  ; keep the decorator from interfering with the rest of the model
  ask decorator [ die ]
end
Jasper
  • 2,610
  • 14
  • 19
  • This is a very nice procedure for a common problem, thanks @Jasper. Question: is it possible to tweak it so that only one side of the turlte-patch gets outlined instead of all four ? (while keeping if possible the squared shape) – lomper Jun 15 '23 at 08:05
  • 1
    @lomper Sure, I updated the answer and sample model with a second option. – Jasper Jun 15 '23 at 16:05