英文:
outline for patches in NetLogo
问题
有没有办法为补丁创建轮廓?我想要一个简单的白色背景和黑色线条的网格。
我设法从找到的代码中创建了一个网格,但乌龟走在线条上而不是在方块内。
英文:
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.
答案1
得分: 4
以下是您提供的内容的中文翻译:
有一种有点傻但有效的方法可以使用特定的乌龟形状和 stamp
原语 来获取补丁轮廓。
选项1
自定义形状是一个填满乌龟区域的正方形轮廓,在 size
为 1(默认值)时与补丁区域相同。我在我的示例模型中将该形状命名为 "square-outline"
。以下是相关代码:
to test
clear-all
ask patches [ sprout 1 [
set shape "square-outline"
if outline-color != black [
set color outline-color
]
stamp
die
]]
end
我使用 stamp
并让乌龟 die
,这样您就不必在代码的其余部分担心它们。如果这对您的模型有意义,您也可以让它们保持活动状态。
如果您想尝试它,可以在 NetLogo Web 上玩这个示例模型 并从中获取形状。您可以导出该 nlogo 文件,并在 乌龟形状编辑器 的 从模型导入... 功能中将形状导入到您的模型中。
选项2
编辑:我更新了模型,以添加隐藏/显示轮廓的每个边缘的选项。我通过添加一个旋转的 "patch-edge"
形状来实现这一点,该形状仅占据乌龟形状的一条边缘。然后,我可以通过设置乌龟的 heading
为 0(顶部)、90(右侧)、180(底部)或270(左侧)来设置每个边缘,然后进行印章。
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
选项2 扩展示例
我想制作一个更全面的示例来勾画补丁形状的轮廓,所以我扩展了示例模型,添加了一个 make-shapes
过程来创建灰色补丁的块,然后使用 neighbors4
原语和 towards
原语来绘制不同补丁之间的边界。示例模型代码已更新,以下是相关部分:
breed [decors decor]
to make-shapes
clear-all
; 设置用于绘制边缘的装饰器
create-decors 1
let decorator one-of decors
; 创建一些灰色块的形状,我们要勾画它们
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 ]
]
; 检测灰色和黑色补丁之间的边缘并勾画它们
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` 获取从黑色补丁返回的方向,所以一些简单的数学将其反转以印章边缘。
let h ((towards myself) + 180) mod 360
ask decorator [ stamp-edge h ]
]
]
; 较深的灰色看起来更好一些
ask patches with [pcolor = grey] [ set pcolor grey - 3 ]
; 防止装饰器干扰模型的其他部分
ask decorator [ die ]
end
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论