首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向海龟展示社区集群的组成

向海龟展示社区集群的组成
EN

Stack Overflow用户
提问于 2022-07-11 12:48:42
回答 1查看 39关注 0票数 0

我有以下代码来检测和着色社区:

代码语言:javascript
复制
to community-detection
  nw:set-context turtles links
  color-clusters nw:louvain-communities
end

to color-clusters [ clusters ]
  ; reset all colors
  ask turtles [ set color gray - 3 ]
  ask links [ set color gray - 3 ]
  let n length clusters
  let colors ifelse-value (n <= 12)
    [ n-of n remove gray remove white base-colors ] ;; choose base colors other than white and gray
    [ n-values n [ approximate-hsb (random 255) (255) (100 + random 100) ] ] ; too many colors - pick random ones
    ; loop through the clusters and colors zipped together
    (foreach clusters colors [ [cluster cluster-color] ->
      ask cluster [ ; for each node in the cluster
        ; give the node the color of its cluster
        set color cluster-color
        ; colorize the links from the node to other nodes in the same cluster
        ; link color is slightly darker...
        ask my-links [ set color cluster-color - 1 ]
      ]
    ])
end

我想鼠标点击一个特定的集群,并显示每一只海龟的数量,如果可能的话,浮动不重叠的数字。我用以下代码创建了一个按钮:

代码语言:javascript
复制
to identify-turtles
  nw:set-context turtles links
  if mouse-down? and member? nw:louvain-communities ([who] of turtles) [ 
      ask turtles [ set label who ] 
  ]
end

但什么都没发生。对如何改进代码有什么建议吗?

这也是可能的,我可以把海龟的数字放在一个显示器,无论什么是更可行的。

甚至是一个观察者命令来获取特定集群中的海龟。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-11 14:25:59

代码的问题是,您只检查鼠标是否已关闭,但不检查鼠标实际指向的位置。为此,您可以使用mouse-xcormouse-ycor。我为您编写了这个快速示例代码,在这里我按降序签入:

  • ,如果鼠标向下,
  • ,如果有一只海龟靠近鼠标
  • ,那么海龟离鼠标

最近

从此,您就可以开始发出命令,显示标签,并隐藏自己或网络中所有海龟的标签。

代码语言:javascript
复制
to setup

  ca
  crt 5 [setxy random-xcor random-ycor]
  
end

to go ;Set as a forever button
  
  if mouse-down? [inspect-turtle]
  
end

to inspect-turtle
  
  ifelse any? turtles with [distancexy mouse-xcor mouse-ycor < 1] [
    ask min-one-of turtles [distancexy mouse-xcor mouse-ycor] [
      set label who
      ask other turtles [set label ""]
    ]
  ] [
    ask turtles [set label ""]
  ]
  
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72938867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档