I'm working on a NetLogo model where I'm trying to connect new "call" turtles to the closest edge defined by two "node" turtles in a network. To achieve this, I'm using the perpendicular-distance function to calculate the perpendicular distance from a call to a line defined by two nodes. However, I'm encountering an issue with the implementation. Below is my dummy code, the attempted solution, and the explanation of the problem:
breed [nodes node]
breed [calls call]
to setup
clear-all
create-nodes 15 [
setxy random-xcor random-ycor
set color gray
]
ask nodes [
create-links-with nodes [
set color gray
]
]
create-calls 5 [
setxy random-xcor random-ycor
]
reset-ticks
end
to-report perpendicular-distance [point node1 node2]
; Calculate the perpendicular distance from a point to a line defined by two nodes
let line-vector node2 - node1
let point-to-node1 point - node1
let projection (point-to-node1 * line-vector) / (line-vector * line-vector)
let projected-point node1 + (projection * line-vector)
report distance point projected-point
end
to connect-calls
ask calls [
let closest-edge nobody
let min-distance 99999
ask links [
let start-node end1
let end-node end2
let perpendicular-dist [perpendicular-distance myself start-node end-node]
if perpendicular-dist < min-distance [
set min-distance perpendicular-dist
set closest-edge self
]
]
if closest-edge != nobody [
let node-start-point-closest-edge end1
let node-end-point-closest-edge end2
ask node-start-point-closest-edge [
create-link-with node-end-point-closest-edge [set color blue]
]
]
]
end
Attempted Solution:
I tried using the perpendicular-distance function to calculate the perpendicular distance between a call and a line defined by two nodes. However, I encountered issues with the implementation. Specifically, I noticed that I cannot use perpendicular-distance within the context of a link, as it should be used within the context of a turtle. As a result, the code doesn't work as expected.
Question:
How to correctly calculate the perpendicular distance between a call and a line defined by two nodes in NetLogo? How should I adjust my code to properly use the perpendicular-distance function within the context of a turtle?