NetLogo:记录龟走过的距离 [英] NetLogo: Recording distance a turtle has traveled

查看:195
本文介绍了NetLogo:记录龟走过的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NetLogo模型,该模型要求乌龟记录它从点A到点B的距离.

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?

推荐答案

我假设您不想只使用

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?

在任何情况下,肯定都可以使用turtles-own变量.这是一个完整的示例:

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

假定您正在使用forward来移动乌龟.如果您使用setxy移动乌龟,则需要将ask turtles块替换为:

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:记录龟走过的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆