NetLogo: Recording distance a turtle has traveled

懵懂的女人 提交于 2019-11-29 11:42:56
Nicolas Payette

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