NetLogo turtles leaving a trail that fades with time

◇◆丶佛笑我妖孽 提交于 2019-12-05 16:23:50

问题


I have turtles moving across the view, and I'd like to be able to follow where they go by making them leave a trail behind them, as though they were emitting smoke as they went. Of course, I could use the turtle pen (pen-down), but since there are many turtles, the view rapidly gets filled with old trails. The solution could be trails that last only for a few ticks before they dissipate. But I don't know how to achieve that.

To be more specific: 1) Is there a technique for making the line drawn following a pen-down command gradually fade away over the period of some ticks? 2) If not, is there a way of removing the line drawn using the pen a few ticks after it was drawn? 3) If not, is there some other technique that would have a similar visual effect?


回答1:


There is no way to fade the trails in the drawing layer over time. If you want trails that fade, you'll need to represent the trails using turtles instead.

Here's sample code for having "head" turtles that trail ten-turtle "tails" behind them:

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

I'm just killing off the old trails outright, but you could also add code that fades their color over time. (An example of a model that does that is the Fire model, in the Earth Science section of the NetLogo Models Library.)




回答2:


Here's a version based on the same principle as the one by @SethTisue, but the tails fade away:

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



回答3:


Here's another approach but without using additional turtles. I include it for variety sake - I would recommend going with Seth's approach first.

In this approach, each turtle keeps a fixed length list of previous locations and headings and stamps out the last position. There's some unwanted artifacts with this approach and is not as flexible as using additional turtles, but I think it uses less memory which may help on larger models.

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


来源:https://stackoverflow.com/questions/21074186/netlogo-turtles-leaving-a-trail-that-fades-with-time

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