NetLogo: Recording distance a turtle has traveled

我与影子孤独终老i 提交于 2019-12-18 07:00:37

问题


I have a NetLogo model that requires a turtle to record its distance travelled from point A to B.

It is important that the distance is measured by the turtle rather than simply calculating the distance between the two points.

I think something like turtles-own would be sufficient to store the distance it has travelled?


回答1:


I assume that you don't want to just use the distance from the original point because it's possible that your turtle has not traveled in a straight line?

In any case, it is certainly possible to use a turtles-own variable. Here is a complete example:

turtles-own [
  distance-traveled
]

to travel
  clear-all
  create-turtles 5
  repeat 100 [
    ask turtles [
      set heading random 360
      let d random 10
      forward d
      set distance-traveled distance-traveled + d
    ]
  ]
  ask turtles [ show distance-traveled ]
end

That assumes you're using forward to move the turtle. If you're using setxy to move the turtle, you'd need to replace the ask turtles block with:

    ask turtles [
      let old-xcor xcor
      let old-ycor ycor
      setxy ... ...
      set distance-traveled distance-traveled + distancexy old-xcor old-ycor 
    ]


来源:https://stackoverflow.com/questions/22488244/netlogo-recording-distance-a-turtle-has-traveled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!