NetLogo:记录龟走过的距离 [英] NetLogo: Recording distance a turtle has traveled
问题描述
我有一个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屋!