Getting closest link in NetLogo

此生再无相见时 提交于 2019-12-13 02:15:45

问题


What's the best way to get the link closest to a particular point in netlogo?

The best would be a link reporter link-distancexy that takes an xcor and ycor and outputs the closest distance from that point to a point on the link. Next best would just be a general reporter closest-link-xy that takes and xcor and ycor and reports the closest link.

This problem is complicated by wrapping boundaries, but an imperfect solutions would still be appreciated.


回答1:


This function might work for your purposes. It uses the link var both-ends and returns the difference between the link's length and the sum of the distances from the link's two ends from a given point:

to-report dist [ #x #y ] ;-- by link --
  report sum [distancexy #x #y] of both-ends - link-length
end

Here is a short test program that shows it in action. For best results turn off wrapping:

to test
  clear-all
  ask n-of 12 patches [ sprout 1 [ set color red ] ]
  ask turtles [ create-links-with n-of 2 other turtles ]
end

to go  ;-- by observer, forever --
  ask links [ set color gray ]
  ask min-one-of links [dist mouse-xcor mouse-ycor] [ set color yellow ]
  display
end



回答2:


Jim Lyon's answer pointed me to the exact solution using basic triangle geometry:

to-report link-distance [ x y ]
  let a [ distancexy x y ] of end1
  let b [ distancexy x y ] of end2
  let c link-length
  let d (0 - a ^ 2 + b ^ 2 + c ^ 2) / (2 * c)
  if d > c [
    report a
  ]
  if d < 0 [
    report b
  ]
  report sqrt (b ^ 2 - d ^ 2)
end

This works for both wrapping and non-wrapping worlds.



来源:https://stackoverflow.com/questions/22135384/getting-closest-link-in-netlogo

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