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.
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.
There is a somewhat silly but effective way to get patch outlines using a specific turtle shape and the stamp
primitive.
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.
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
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