Convert angle to vector NetLogo - netlogo

I have angle A in netlogo. Which is the turtle's heading?
I wish to convert to it to unit vector.
How should I do it given the turtle's axis are different from that normal?(Heading 0 corresponds to looking forward).

Assuming that by "unit vector" you want a x component and a y component, the dx and dy primitives do this (see dictionary). So for example:
observer> crt 1 [ set heading 30 print (list dx dy) ]
[0.49999999999999994 0.8660254037844387]
Note that dx is just sin heading and dy is just cos heading.

Related

Understand the use of dx and dy commands in NetLogo?

I'm new to NetLogo and I'm in doubt about 2 Netlogo commands.
Could someone help me understand the dx and dy commands? I read the description in the NetLogo dictionary. But, I didn't quite understand what it does.
What's the difference between using a move with dx and dy and without?
Thanks in advance!
A simplified example taken from a book with application in NetLogo:
turtles-own
[
real_x ;; the real x coordinate dist from origin ie ignoring wrapping around world
real_y ;; the real y coordinate dist from origin ie ignoring wrapping around world
]
to setup
here
create-turtles 1
ask turtles
[
setxy random-xcor random-ycor
; set real-x xcor
; set real-y ycor
]
reset ticks
end
to go
move
tick
end
to move
;; using dy and dx
; ask turtles
; [
; set heading random-float 360
; set real_x real_x + dx
; set real_y real_y + dy
; fd 1
; ]
ask turtles
[
set heading random-float 360
fd 1
]
end
dx is the difference in the x-coordinate if the turtle move forward 1 unit at its current heading. Same thing for dy and the y-coordinate.
Mathematically: tan(heading) = dy/dx
Geometrically:

mean and SD of directions (in degrees) in NetLogo

I'm working on a model in NetLogo where I would like to report the mean and standard deviation of a set of turtle dispersal directions (bearings from 0-360 degrees) in each year of the simulation. Of course there's no default command in NetLogo for these circular statistics, so I'll need to write out the calculations by hand. I'm wondering if anyone has worked out a similar custom function in NetLogo before?
I've come across this set of code for calculating the mean:
to-report mean-of-headings [headings]
let x-mean mean map [sin ?] headings
let y-mean mean map [cos ?] headings
if x-mean = 0 and y-mean = 0 [ report random 360 ]
report atan x-mean y-mean
end
But I'm not positive if that will do for the mean, and haven't seen code for the SD. My thought was to translate this R code into NetLogo language, and create a reporter similar to the one above?
Tester <-c(340, 360, 20) # list of bearings
sine = sum(sin(Tester * pi/180)) # sin of each angle, convert to radian first
cosine = sum(cos(Tester * pi/180)) # cos of each angle, convert to radian first
Tester_mean = (atan2(sine, cosine) * 180/pi) %% 360
mu = (Tester - Tester_mean + 180) %% 360 - 180 # Difference of each angle from mean
Tester_sd = sqrt(sum(mu^2)/(length(Tester) - 1)) # Standard Deviation
Tester_mean # mean bearing
Tester_sd # sd bearing

netlogo - radial direction of the turtle

I have an ellipse shape. (created having vertical direction)
I want to place several such shapes on the circle contour
They should be placed having same distance one from another
;x,y - circle center coordinates
;r - radius
to draw-ellipses [x y r elNum ]
if elNum > 0 [
let theta 0
let delta 360 / elNum
loop[
if elNum = 0 [ stop ]
crt 1 [
setxy (x + r * cos theta) (y + r * sin theta)
set shape "ellipse"
set heading 90 + theta
set size 7
]
set theta (theta + delta)
set elNum (elNum - 1)
]
]
end
The ellipse shapes ate placed correctly but their direction is wrong. I want the ellipses to have radial direction. I.e to be the continues of the radius to the point, where shape is placed. but directions are not good.
Only for 0 and 180 degrees the directions are ok.
Some advices what am I missing?
Do I understand correctly that you want your turtles to face away from the radius? If that's true, try replacing your heading line with:
set heading atan xcor ycor
to do it in a way that is consistent with your angle math. Alternatively, you can use facexy, and replace the heading line instead with something like:
facexy x y
rt 180

Rotating a turtle through cone of vision

I wish to write to where I have a cone-of-vision cone angle for the turtle.
And I rotate the turtle through the cone-of-vision.
Therefore, first it's heading the start of cone-of-vision and with a increment of 0.05 degree it changes till it reaches the end of cone-of-vision
let max-head heading + (cone-angle / 2)
set heading subtract-headings heading (zero-dash / 2)
while[(subtract-headings heading max-head ) < 0]
[
set heading heading + .05
;wait 0.1
]
The above code I wrote isnt correct. But I can't find the mistake.
I think part of your difficulty is using subtract-headings. This finds the size of the shortest angle between two headings and that's not what you want for a cone of vision (which could potentially be wide). Since your code rotates clockwise, I have assumed you want to start at the most anti-clockwise point and rotate. The only difficulty then is if the heading goes through 360, so you need to separate the cone sweeping counter and the actual heading, and use mod arithmetic to convert from the counter.
globals
[ cone-angle]
to setup
clear-all
create-turtles 1
set cone-angle 25
end
to go
ask turtles
[ let max-head heading + (cone-angle / 2)
let fake-head heading - (cone-angle / 2)
while [fake-head < max-head]
[ print fake-head
set fake-head fake-head + 1
set heading fake-head mod 360
wait 0.1
]
]
end

Rotating a vector denoting turtle position by an angle Netlogo

I set/update each turtle's position as follows:
set xcor xcor + item 0 vector
set ycor ycor + item 0 vector
Therefore I add a vector to the current agent's coordinates.
PROBLEM:
I wish to rotate the added vector by angle x. Thus the vector "vector" should be rotated by angle x.
The angle should be taken from a Gaussian distribution with a specified deviation.
I am trying to achieve something similar to Couzin's model.
http://www.csim.scu.edu.tw/~chiang/course/ComputerGameAdvance/Collective%20Memory%20and%20Spatial%20Sorting%20in%20Animal%20Groups.pdf
Thanks in advance!
You seem to have two questions here; I'll address the one you used for the title. The matrix extension allows matrix multiplication, so you could just create a standard rotation matrix once you have the ange of rotation. But standard advice in NetLogo would be to use a more turtle-centric approach. Then you need to decide whether to use the NetLogo heading conventions (0 degrees for north, 90 degrees for east, etc.) If so you could do something like this:
to move [#dx #dy]
let %dist 0
ask patch 0 0 [set %dist distancexy #dx #dy]
facexy (xcor + #dx) (ycor + dy)
let %theta random-rotation
rt %theta
jump %dist
end
to-report random-rotation
report (random-float 360) - 180
end
Here the random rotation is not Gaussian distributed because I was not sure what you meant. Perhaps a von Mises distribution? In any case, you should clarify and ask as a separate question.
Just to emphasize Alan's point: Unless you have a good reason to use vectors, it's usually much easier and clearer to avoid them in NetLogo. If all you want to do is turn the turtle by a random amount drawn from a Gaussian distribution, you can just do:
right-turn random-normal 0 <std-dev>
where <std-dev> is your desired standard deviation. Then, you can tell the turtle to go forward by what would have been the magnitude of the vector: forward <distance>.
If you absolutely need to do a vector rotation, you can do so without the matrix extension fairly easily:
to-report rotate-vector [ vec angle ]
let x first vec
let y last vec
let mag sqrt (x * x + y * y)
let old-angle atan x y
let new-angle angle + old-angle
report (list (mag * sin new-angle) (mag * cos new-angle))
end
Remember that angles in NetLogo are flipped around 45º so that 0º is north and 90º is east; thus, sin and cos are flipped when dealing with angles.
Somewhat simple, convert vector to angle, rotate (randomize), then convert back. For good coding style and such, break into modules.
to-report rotate [ #vector #angle ]
let $dx first #vector
let $dy last #vector
let $magnitude sqrt ($dx * $dx + $dy * $dy)
set #angle #angle + atan $dx $dy
report (list $magnitude * sin #angle $magnitude * cos #angle)
end
to-report nudge-vector [ #vector #std-dev ]
report rotate #vector random-normal 0 #std-dev
end
to move-inaccurately [ #vector #std-deviation ]
set #vector nudge-vector #vector #std-deviation
setxy (xcor + first #vector) (ycor + last #vector)
end