一个快速的解决方案可能是使用patch-at-heading-and-distance函数。
to create-forests
ask patches[set pcolor black]
repeat 5
[
ask one-of patches
[
set pcolor green
repeat 20
[
let a random 360
let b random 2
ask patch-at-heading-and-distance a b
[
ask neighbors4 [ set pcolor green]
set pcolor green
]
]
]
]
end
使用此功能,您可以移动到另一个补丁邻居。使用 a(角度)、b(距离)的随机值范围以及这部分代码重复的次数,可以实现不同的森林密度。
对于邻居 4,我们尝试填充森林内可能存在的黑色斑块,(请注意,此技巧的成功将取决于森林的大小)
更复杂的选择可能是:
to create-forests2
ask patches[set pcolor black]
repeat 5
[
ask one-of patches
[
let size_f (random 2) + 1
let border 2
let big_area [list pxcor pycor] of patches with [abs pxcor <= size_f + border and abs pycor <= size_f + border]
let forested [list pxcor pycor] of patches with [abs pxcor <= size_f + random border and abs pycor <= size_f + random border]
ask patches at-points forested[set pcolor green]
ask patches at-points big_area with[pcolor != green]
[ if count(neighbors with [pcolor = green]) > 6 [ set pcolor green ] ]
]
]
end
在列表的帮助下确定了 6/8 森林中包围的斑块,因此也被转换为森林