如何随机删除网格中的块边? [英] How to randomly remove block side in a grid?

查看:41
本文介绍了如何随机删除网格中的块边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继在调用reduce.不过,简而言之,它:

  • 将现有补丁集群和一个新补丁添加到该集群中
  • 寻找该新补丁的邻居,这些补丁也应该添加到集群中,即那些颜色相同但尚未成为集群一部分的邻居
  • 调用自己添加这些新补丁,以便他们的邻居可以添加到集群中(以及这些邻居的邻居等),直到找不到新补丁.

一旦你有了 grow-cluster,你就可以用它来完成你想要的事情,方法是用一个空的簇和你选择的随机补丁来播种:

删除随机方让随机补丁之一与 [pcolor = white]让边增长集群无补丁随机补丁问边 [设置 pcolor 棕色]结尾

警告:要使其正常工作,必须在模型设置中禁用世界包装.

Following on from How to control square size in a grid from square area?, I would like to randomly remove block side in a grid. Here my idea:

%% In a first time, I randomly select a white patch in the grid (figure below):

let random-patch one-of patches with [pcolor = white]

%% Then, I draw red patches to identify intersections between white roads (figure below):

ask patches with [ (pxcor mod (side + 1) = 0 ) and (pycor mod (side + 1) = 0 ) ] [set pcolor red]

%% Finally, I color in brown all white patches that are situated between two red patches and on the same side than the random patch. To identify these white patches, have I to calculate distance between the random patch and the nearest red patches and to color all white patches situated along these distances ?

Thanks in advance for your help.

解决方案

An alternative way to think about your problem is in terms of finding clusters of white patches: you're picking a white patch at random, and you want to turn all the contiguous white patches to brown.

You can look at the "Patch Clusters Example" in the Code Examples section of NetLogo's model library to see one way to do this.

Here is how I would do it. Let's start by defining a grow-cluster reporter:

to-report grow-cluster [ current-cluster new-patch ]
  let patches-to-add [
    neighbors4 with [
      pcolor = [ pcolor ] of myself and
      not member? self current-cluster
    ]
  ] of new-patch
  let new-cluster (patch-set current-cluster new-patch)
  report ifelse-value any? patches-to-add
    [ reduce grow-cluster fput new-cluster sort patches-to-add ]
    [ new-cluster ]
end

The code may be hard to understand if you're not used to functional programming because it uses recursion within a call to reduce. Still, in a nutshell, it:

  • takes an existing patch cluster and a new patch to add to this cluster
  • looks for neighbors of that new patch that should also be added to the cluster, i.e., those that are the same color but not already part of the cluster
  • calls itself for these new patches to add so that their neighbors can be added to the cluster (and those neighbors' neighbors, etc.) until no new patches are found.

Once you have grow-cluster, you can use it to accomplish exactly what you want by seeding it with an empty cluster and the random patch that you selected:

to remove-random-side
  let random-patch one-of patches with [pcolor = white]
  let side grow-cluster no-patches random-patch
  ask side [ set pcolor brown ]
end

Caveat: for this to work, world wrapping has to be disabled in your model settings.

这篇关于如何随机删除网格中的块边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆