獲得最接近netlogo中某個特定點的鏈接的最佳方式是什麼?在NetLogo中獲得最接近的鏈接
最好的將是一個鏈接記者link-distancexy
接受一個xcor
和並輸出從該點距離最近的鏈路上的一個點。接下來最好的將只是一個總記者closest-link-xy
,它需要和xcor
和並報告最接近的鏈接。
這個問題很複雜的包裝邊界,但不完善的解決方案仍將不勝感激。
獲得最接近netlogo中某個特定點的鏈接的最佳方式是什麼?在NetLogo中獲得最接近的鏈接
最好的將是一個鏈接記者link-distancexy
接受一個xcor
和並輸出從該點距離最近的鏈路上的一個點。接下來最好的將只是一個總記者closest-link-xy
,它需要和xcor
和並報告最接近的鏈接。
這個問題很複雜的包裝邊界,但不完善的解決方案仍將不勝感激。
吉姆·里昂的回答向我指出使用基本的三角幾何形狀的精確解:
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
這既適用於包裝和非包裝的世界。
此功能可能適用於您的目的。它使用鏈接VAR both-ends
並從給定的點返回鏈路的長度和距離從鏈路的兩端的總和之間的區別:
to-report dist [ #x #y ] ;-- by link --
report sum [distancexy #x #y] of both-ends - link-length
end
下面是一個短的測試程序,它顯示在行動。爲了達到最佳效果關閉包裝:
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
非常簡單!出於某種原因,我甚至沒有想過使用三角幾何。缺點是這些單位最終沒有得到補救(不是那麼重要),而且小鏈接也受到懲罰。但是,這是一個很好的答案,對絕大多數情況來說絕對夠用。另外,它看起來和包裝一樣好。 –