我有以下代码来检测和着色社区:
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我想鼠标点击一个特定的集群,并显示每一只海龟的数量,如果可能的话,浮动不重叠的数字。我用以下代码创建了一个按钮:
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

但什么都没发生。对如何改进代码有什么建议吗?
这也是可能的,我可以把海龟的数字放在一个显示器,无论什么是更可行的。
甚至是一个观察者命令来获取特定集群中的海龟。
发布于 2022-07-11 14:25:59
代码的问题是,您只检查鼠标是否已关闭,但不检查鼠标实际指向的位置。为此,您可以使用mouse-xcor和mouse-ycor。我为您编写了这个快速示例代码,在这里我按降序签入:
最近
从此,您就可以开始发出命令,显示标签,并隐藏自己或网络中所有海龟的标签。
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 ""]
]
endhttps://stackoverflow.com/questions/72938867
复制相似问题