Netlogo笔记06:狼羊追逐

向量定向法,更新狼的移动方向 

可以看出狼与羊的追逐总是会趋于稳定,狼羊距离总体是减小的趋势。

globals[flag]
breed[cats cat]
breed[dogs dog]
to update_heading;red to find black
  let x1 1
  let y1 1
  let x2 1
  let y2 1
  let hea 0
  ask turtles with [color = red][
    set x1 pxcor
    set y1 pycor 
  ]
  ask turtles with [color = white][
    set x2 pxcor - x1
    set y2 pycor - y1
  ]
  if x2 = 0 and y2 = 0
  [
    set flag 1
    stop
  ]
  let turn_corn 0
  show x2
  show y2
  set turn_corn acos( y2 / sqrt( x2 * x2 + y2 * y2 ) )
  ifelse x2 >= 0[
     ask turtles with [color = red][set heading turn_corn]
  ][
     ask turtles with [color = red][set heading 360 - turn_corn]
  ]
end

to setup
  clear-all
  reset-ticks
  set flag 0
  set-default-shape turtles "wolf"
  create-dogs 1[
    set color red
    setxy random-xcor random-ycor 
  ]
  set-default-shape turtles "sheep"
  create-cats 1[
    set color white
    setxy random-xcor random-ycor 
  ]
  ask turtles with [color = red][
    create-links-with other turtles
    if links-two = false [ask links [hide-link] ]
  ]
end 

to go
  update_heading
  ask turtles[
    if color = white[
      right random 90
      left random 90
      fd -0.3
    ]
    fd 1 
  ]
  if flag = 1[stop]
  let d 0
  ask turtles[
    if color = white[ask other turtles[set d distance myself]]
  ]
  set-current-plot "Distance"
  set-current-plot-pen "catch"
  plot d
  tick
end

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尼卡尼卡尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值