NetLogo 乌龟留下随时间褪色的轨迹

11
我有一些乌龟在画布上移动,我希望能够通过让它们留下一个轨迹来跟踪它们的移动,就像它们在前进时释放烟雾一样。当然,我可以使用turtle笔(pen-down),但由于有很多乌龟,画布很快就会被旧轨迹填满。解决方法可能是持续时间只有几个ticks 的轨迹,然后会逐渐消失。但我不知道如何实现。
具体来说: 1)有没有一种技术使得在pen-down命令之后绘制的线条经过一段时间逐渐消失? 2)如果没有,是否有一种方法在绘制几个ticks之后删除使用笔绘制的线条? 3)如果没有,是否有其他技术可以达到类似的视觉效果?
3个回答

8

无法使绘图层中的轨迹随时间逐渐消失。如果您想要渐变消失的轨迹,您需要使用海龟来代表轨迹。

以下是一个示例代码,可以让“头”乌龟在其后跟随着十个“尾巴”的轨迹:

breed [heads head]
breed [tails tail]
tails-own [age]

to setup
  clear-all
  set-default-shape tails "line"
  create-heads 5
  reset-ticks
end

to go
  ask tails [
    set age age + 1
    if age = 10 [ die ]
  ]
  ask heads [
    hatch-tails 1
    fd 1
    rt random 10
    lt random 10
  ]
  tick
end

我只是彻底清除旧的轨迹,但你也可以添加代码,使它们的颜色随时间淡化。(一个做到这一点的模型例子是NetLogo模型库中地球科学部分的火模型。)


我认为你也可以在询问头部使用这个:ask patch-here [ Sprout-tails 1 ] - Marzy
如果你将海龟的初始颜色设置为白色,那么在每次移动中,你可以设置颜色为color - 1,这将呈现出一种渐变效果。 - Marzy
1
要求补丁程序“sprout”尾部会导致失去标题和精确的xcor和ycor值,而“hatch”则可以保留。 - Seth Tisue
谢谢,我不知道 :) - Marzy
作为 NetLogo 领域的新手,让我问一个关于你的代码的初学者问题:有没有特定的 ABM 推理来优先执行 ( set age age + 1 ) 并仅在下一步测试 ( if age = 10 [ die ] )?我的直觉告诉我,在性能方面更好的顺序应该是首先减少代理集的数量 ( if age = 9 [ die ] ),这样我们才能在仍然活跃的剩余部分上执行任何进一步的操作(可能很昂贵)。有没有任何原因(阻塞、gc 或其他性能相关的推理)不这样做?谢谢。 - user3666197

4
这是一种基于与@SethTisue相同原理的版本,但其尾巴会渐渐消失:
globals [ tail-fade-rate ]
breed [heads head]    ; turtles that move at random
breed [tails tail]    ; segments of tail that follow the path of the head

to setup
  clear-all              ;; assume that the patches are black
  set-default-shape tails "line"
  set tail-fade-rate 0.3 ;; this would be better set by a slider on the interface
  create-heads 5
  reset-ticks
end

to go
  ask tails [
    set color color - tail-fade-rate ;; make tail color darker
    if color mod 10 < 1  [ die ]     ;; die if we are almost at black
  ]
  ask heads [
    hatch-tails 1        
    fd 1
    rt random 10
    lt random 10
  ]
  tick
end

2

这里有另一种方法,但不使用额外的乌龟。我为了变化而包括它 - 我建议首先采用Seth的方法。

在这种方法中,每个乌龟保留一个固定长度的前位置和标题列表,并盖上最后一个位置。这种方法存在一些不需要的副作用,并且不像使用额外乌龟那样灵活,但我认为它使用更少的内存可能有助于更大的模型。

turtles-own [tail]

to setup
  ca
  crt 5 [set tail n-values 10 [(list xcor ycor heading)] ] 
end

to go
  ask turtles [
    rt random 90 - 45 fd 1
    stamp

    ; put current position and heading on head of tail
    set tail fput (list xcor ycor heading) but-last tail

    ; move to end of tail and stamp the pcolor there
    let temp-color color
    setxy (item 0 last tail) (item 1 last tail)
    set heading (item 2 last tail)
    set color pcolor set size 1.5   stamp

    ; move back to head of tail and restore color, size and heading
    setxy (item 0 first tail) (item 1 first tail) 
    set heading item 2 first tail
    set size 1  set color temp-color
    ]
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接