Reputation: 12580
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.
Upvotes: 1
Views: 631
Reputation: 12580
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.
Upvotes: 2
Reputation: 41
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
Upvotes: 4