Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

netlogo - Get mean heading of neighboring turtles

I was trying to program my turtles to move with a heading that's the mean heading of its neighbors (turtles within a specific radius). Should I use in-radius to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given Nicolas' response to Arthur's answer, here's the code to get what wikipedia considers to be the mean of angles:

to-report mean-heading [ headings ]
  let mean-x mean map sin headings
  let mean-y mean map cos headings
  report atan mean-x mean-y
end

Note that because up is 0 is NetLogo angles, sin heading is the x instead of y. Next, we can use that to set the heading of our turtles:

ask turtles [
  set heading mean-heading [ heading ] of turtles in-radius 3
]

where you'd replace 3 with the radius of your choice of course. You didn't say if you wanted a turtle to take into account its own heading when computing the mean or not. Here, they do take their own heading into account, which means that we don't have to do an any? check (since turtles in-radius r will always include the turtle itself!).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...