Reputation: 3
In a formation robots are linked with eachother,number of robots in a neighbourhood may vary. If one robot have 5 neighbours how can I find the angle of that one robot with its other neighbour?
Upvotes: 0
Views: 198
Reputation: 2926
(Following a comment, I replaced the sequence of <face
+ read heading
> with just using towards
, wich I had overlooked as an option. For some reason the comment I am referring to has been deleted quickly so I don't know who gave the suggestion, but I read enough of it from the cell notification)
In NetLogo it is often possible to use turtles' heading
to know degrees.
Since your agents are linked, a first thought could be to use link-heading
, which directly reports the heading in degrees from end1 to end2.
However note that this might not be ideal: using link-heading
will work spotlessly only if you are interested in knowing the heading from end1 to end2, which means:
If that's something that you are interested in, fine. But it might not be so! For example, if you have undirected links and are interested in knowing the angle from turtle 1
to turtle 0
, using link-heading
will give you the wrong value:
to setup
clear-all
create-turtles 2 [
setxy random-xcor random-ycor
set color black
set label who
]
ask turtle 0 [
create-link-with turtle 1
]
end
to go
ask link 0 1 [
show link-heading
]
end
... while we know, by looking at the two turtles' positions, that the degrees from turtle 1
to turtle 0
must be in the vicinity of 45.
An approach that better fits all possible cases is to directly look into the heading
of the turtle you are interested in, regardless of the nature or direction of the link. You can let your reference turtle face
the target turtle, and then read heading
of the reference turtle. Or better: you can directly use towards
, which reports just the same information but without having to make turtles actually change their heading. Copy and run the code below to see how this approach always gives the right answer!
to setup
clear-all
create-turtles 5 [
setxy random-xcor random-ycor
set color black
set label who
]
end
to go
let reference sort turtles
foreach reference [
r ->
ask r [
print "---------------------------------------------------------------------------------------------------------------------------------------------"
let targets sort other turtles
foreach targets [
t ->
let direction (towards t)
type "I am " type self type ". The NetLogo angle between me and " type t type " is " type direction type ", while the normal mathematical angle is " print heading-to-angle direction
]
print "---------------------------------------------------------------------------------------------------------------------------------------------"
]
]
end
to-report heading-to-angle [ h ]
report (90 - h) mod 360
end
In your case, the target group (that I have set just as other turtles
in my brief example above) could be based on the actual links and so be constructed as (list link-neighbors)
or sort link-neighbors
(because if you want to use foreach
, the agentset must be passed as a list - see here).
Update: I actually ended up also making a toy model that represents your case more closely, i.e. with links and using link-neighbors
. See below:
to setup
clear-all
create-turtles 100 [
move-to one-of patches with [not any? turtles-here]
set color black
set label who
]
ask n-of 2 turtles [
create-links-with n-of 5 other turtles
]
end
to go
let reference-turtles sort turtles with [count my-links > 2]
foreach reference-turtles [
r ->
ask r [
print "-----------------------------------------------------------------------------------------------------------------------------------------------"
let targets sort link-neighbors
foreach targets [
t ->
let direction (towards t)
type "I am " type self type ". The NetLogo angle between me and " type t type " is " type direction type ", while the normal mathematical angle is " print heading-to-angle direction
]
print "-----------------------------------------------------------------------------------------------------------------------------------------------"
]
]
end
to-report heading-to-angle [ h ]
report (90 - h) mod 360
end
Final note: you surely noticed the heading-to-angle
procedure, taken directly from the atan
entry here. It is a useful way to convert degrees expressed in the NetLogo geometry (where North is 0 and East is 90) to degrees expressed in the usual mathematical way (where North is 90 and East is 0). I don't know what degrees you're interested in, so it's worth to leave this hint here.
Upvotes: 4