NetLogo: Recording distance a turtle has traveled

2020-02-13 02:00发布

问题:

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 
    ]


标签: netlogo