Get mean heading of neighboring turtles

前端 未结 2 1490
太阳男子
太阳男子 2020-12-22 02:36

I was trying to program my turtles to move with a heading that\'s the mean heading of its neighbors (turtles within a specific radius). Should I use in-radius to achieve thi

2条回答
  •  春和景丽
    2020-12-22 02:46

    Using in-radius would work. So something like

    ask turtles [
        let neighbor-turtles other turtles in-radius 3
        if any? neighbor-turtles [
            set heading mean [heading] of neighbor-turtles]
        ]
    

    would work.

    Depending on how many turtles you have, you might find that it runs a bit slow. In-radius finds all patches that are within the radius, and then calculates the distance to each of the turtles on that patch. This means that sometimes it will have to do calculations on turtles that are outside the radius.

    If that becomes a problem, another and slightly faster way is to find turtles on neighboring patches.

    ask turtles[
        ;; create a turtle set of all turtles on same and neighboring patches as turtle
        let neighbor-turtles (turtle-set other turtles-here [turtles-here] of neighbors)
        if any? neighbor-turtles[
            set heading mean [heading] of neighbor-turtles
            ]
        ]
    

    This offers a bit less flexibility though, since you can only find turtles on the neighboring patches. But if that is a high enough granularity for you, then that's at least an option.

提交回复
热议问题