Draw a semicircle in Netlogo - netlogo

I want to create a semicircle, the center of my semicircle is at the end of the world, for example I want that the center of my semicircle is in the 50,20 position, and goes from 20 to -20.
I try with this, but I got an exception:
The point [ 60 , 20 ] is outside of the boundaries of the world and
wrapping is not permitted in one or both directions.
to mysemicircle
let cx 50 ;; x coordinate of patch you want to circle
let cy 20 ;; y coordinate of patch you want to circle
let r 10 ;; radius of the circle you want
let p2r ( 2 * pi * r ) ;; get circumference of the circle
let step p2r / 360 ;; make step lengths 1/360th of the circumference
crt 1 [ ;; create a single drawing turtle
setxy cx + r cy ;; move it to the highlight patch + the radius
pd ;; put the pen down
set heading 0 ;; make it face along the tangent
while [ p2r > 0 ] [ ;; make the turtle continue to move until the circle is drawn
lt 1
fd step
set p2r p2r - step
set color white
]
]
end
And If i use cx 40 and cy 20 the circle is not from y -20 to y 20, but is only from y 0 to y 20.
How Can I do that?

Maybe a procedure with arguments would be better for this, something like:
to setup
ca
resize-world -20 20 -20 20
draw-semi-circle 0 0 15 45 blue
draw-semi-circle 5 5 5 270 red
draw-semi-circle -10 -10 8 135 green
reset-ticks
end
to draw-semi-circle [ x y r a col ]
; Give arguments for x, y, radius, and angle of the semicircle
let half-p ( pi * r )
let step half-p / 180
ask patch x y [
set pcolor col
]
crt 1 [
setxy x y
set heading a
lt 90
set color col
back r
pd
fd 2 * r
rt 90
while [ half-p > step ] [
rt 1
fd step
set half-p half-p - step
]
]
end
To get an output like:

Related

how to lineup the agents in the ascending or descending order of their who number in netlogo?

I am creating an patient-surgeon-operation bed model, wherein I need to show surgeons lined up on the left side of patch awaiting to enter operation room in the center and the patients awaiting in the queue from the right side.
I want the surgeons and patients to be located on the patch as per their who number
S1 S2 S3 --> Operation room < -- P1 P2 P3
I use the below query, I am not sure where to incorporate the who number
to lineup-patients
LET gapp 10
LET directions
[45 90 230 180 45 90 230 180 45 90 45 90 230 180 45 90 230 180 45 90 45 90 ]
LET jj 0 ; counter / index
REPEAT initial-number-patients
[ create-PATIENTS 1
[ SETXY (0 + jj * gapp) 20
set shape "person"
SET size 1.2
SET label who
SET label-color black
SET heading item jj directions
]
SET jj jj + 1
ASK patients [
MOVE-TO ONE-OF PATCHES WITH [ PCOLOR = yellow ]
] ]
END
You have a move-to after you line them up. And it always moves all existing patients. To keep things cleaner, write a separate lineup proc.
to lineup [#patients #patch #gap]
let _x ([pxcor] of #patch)
let _y ([pycor] of #patch)
let _xqs n-values (count #patients) [[n] -> _x + n * #gap]
(foreach sort #patients _xqs [
[p x] -> ask p [setxy x _y]
])
end
You can test this with a new instance of NetLogo as follows:
to test
ca
crt 20
lineup turtles one-of patches 0.5
end

Drawing a super-ellipse with a turtle

Obviously, any shape drawable by other means can be drawn by a turtle. Circles and squares are easy
rt 1 fd .0
and
if ticks mod 100 = 0 [rt 90]
fd 1
Super-ellipses not so much. (regular ellipses are not trivial either.)
The Wikipedia article on super-ellipses if you need to be refreshed on the topic.
Any input is appreciated.
Using a pendown turtle is there way to make a super-ellipse that emerges from turtle movement?
I have 1/4 of it, I suppose you could piece-wise put the other three together. Other values of n are not tested here. (using the Wiki notation, plus phi as an angle of rotating the whole thing.) And the placement of reset-ticks, pen-down, is sloppy, I know.
to go2
clear-all
reset-ticks
let a 6
let b 5
let phi 0
let n 3.5
create-turtles 1 [
let iNdx 1
repeat 90 [
show iNdx
show cos(iNdx)
if cos(iNdx) > 0 and sin(iNdx) > 0 [
let tx (a * (cos(iNdx) ^ (2 / n)))
let ty (b * (sin(iNdx) ^ (2 / n)))
let tx2 tx * cos(phi) - ty * sin(phi)
let ty2 tx * sin(phi) + ty * cos(phi)
setxy tx2 ty2
]
pen-down
set iNdx iNdx + 1
]
]
end
The ellipse looks simpler, but you be the judge
to go
clear-all
reset-ticks
let a 6
let b 5
let phi 45
create-turtles 1 [
let iNdx 1
repeat 360 [
let tx (a * cos(iNdx))
let ty (b * sin(iNdx))
let tx2 tx * cos(phi) - ty * sin(phi)
let ty2 tx * sin(phi) + ty * cos(phi)
setxy tx2 ty2
pen-down
set iNdx iNdx + 1
]
]
end
a generalization and simplification as a procedure.
to Super-ellipse [x y a b m n]
create-turtles 1 [
let iNdx 1
repeat 360 [
setxy (x + (abs cos iNdx)^(2 / m) * a * (sgn cos iNdx))
(y + (abs sin iNdx)^(2 / n) * b * (sgn sin iNdx))
pendown
set iNdx iNdx + 1]
]
end
The generalized form of another answer seems to produce the sort of thing I was thinking of. the closer the pen starts to one of the foci the closer the drawing is to a square. the n<1 super-ellipses are not achieved.
globals[c]
breed [pens pen]
breed [foci focus]
foci-own [dist distx disty]
to setup
ca
create-pens 1 [set heading 45 fd 10 pendown set C self]
;create-foci 1 [setxy (random-xcor / 2) (random-ycor / 2)]
create-foci 1 [setxy 10 10]
create-foci 1 [setxy 10 -10]
create-foci 1 [setxy -10 -10]
create-foci 1 [setxy -10 10]
end
to go
repeat 5100
[
ask foci [
set dist distance c
set distx xcor - [xcor] of c
set disty ycor - [ycor] of c
]
ask c
[
set heading 90 + atan ( sum [distx / dist] of foci / sum [dist] of foci)
( sum [disty / dist] of foci / sum [dist] of foci)
FD .0125
]
]
end

Drawing an Emergent ellipse with a turtle.

This is an answer to a question posed in the comments in my possibly poorly worded question about super-ellipses.
in Netlogo it is natural to draw geometric shapes in ways that may seem strange in other languages.
ask turtle 1 [pendown
let d (pi * distance turtle 2) / 360
repeat 360 [face turtle 2 rt 90 fd d]
]
for instance inscribes makes turtle 1 draw a circle [360-gon] around turtle 2. I did not invoke any of the standard circle formulas but still get a circle.
Is it possible to draw an ellipse in this same vernacular with say one turtle drawing an ellipse (or super-ellipse)round two other turtles using them as the foci?
Essentially to make an ellipse you set the turtles heading to the weighted mean heading of the foci and update each step. It could be done in one line but that would be one ugly line.
globals [a b c]
to setup
ca
crt 1 [set heading 90 fd 10 pendown set C self]
crt 1 [setxy 5 10 set A self]
crt 1 [setxy 0 -10 set B self]
end
to go
repeat 5100 ;; ad hoc number
[
ask c
[
let Ax [xcor] of A - xcor
let Ay [ycor] of A - ycor
let Bx [xcor] of B - xcor
let By [ycor] of B - ycor
let da 1 / distance a
let db 1 / distance B
set heading 90 + atan ((ax * da + bx * dB) / (da + db))
((ay * da + by * db) / (da + db))
FD .0125 ;;
]
]
end

How to measure distance covered across patches in NetLogo

I have a model setup in NetLogo where the simulation space is a GIS map with a 20 km radius that I constructed.
The max xcor and ycor are set to 20 and min xcor and ycor -20. This gives a torus of 41 x 41.
I have my agents at the centre of the map at patch 0 0 and want the maximum distance they can cover as the 20 km i.e. the extent of the GIS map.
ask turtles [
set heading 360
fd 1 ]
Am I right in saying if I iterate this code 20 times they'll be at the centre of the last patch (xcor 0 ycor 20) which isn't quite 20 km?
If that's right, how do I code it up so the agents move the appropriate distance. I feel like I'm trying to square a circle here.
I should say I can add the following line to get a patch scale that comes out at about 975.6
set patch-scale (item 1 gis:world-envelope - item 0 gis:world-envelope ) / world-width
If I multiply the patch scale by the world width I get 40000 which looks right given the radius of the circle is 20 km.
Thanks
globals [km]
to test
ca
let extent 40 ;desired world-width, in kilometers
set km (world-width / extent)
crt 1 [set heading 360]
ask turtle 0 [fd (20 * km)]
end

Problems with defining heading pitch and roll in 3D-netlogo

i'd like to simulate sun rays on a building. For the angle of the sun rays, i have the right elevation and azimuth angles. I was calculating the corresponding values for heading and pitch. But heading, pitch and roll seem to affect each other. When heading changes, also pitch changes. I have no idea how to calculate the corresponding values to my angles.
I was checking out the "Turtle and Observer Motion" code example in the netlogo library, but that was no help.
This is my code so far:
angles shall change every 60 ticks (position in the list changes)
lists contain converted heading and pitch values respectevly
globals [h ; heading
p ; pitch
r ; roll
i ; instead of tick
data_az ; a list with the azimuth angles
data_el ; a list with the elevation angles
hour ; is the position of the angles in the list, stands for the hour
i_test
i_round
]
breed [rays ray]
to setup
ca
file-open "heading.txt"
set data_az file-read
file-close
file-open "pitch.txt"
set data_el file-read
file-close
check-date
set r 0
set i 1
set hour hour - 1
set-default-shape rays "line half"
reset-ticks
end
to go
tick
if i = 1440 ; after 24x60 i stop
[
stop
]
set i_test i / 60
set i_round round i_test
if i_test = i_round or i = 1
[
set hour hour + 1
set h item hour data_az
set p item hour data_el
]
if h != 0
[
create-sunshine
run-sunshine
]
set i i + 1
close
end
to create-sunshine
create-rays 5 [ setxyz random-pxcor max-pycor random-pzcor
set color yellow
set heading h
set pitch p
set roll r
set size 2
]
end
to run-sunshine
ask rays [fd 1]
ask rays [if pxcor = 0 [die]] ;rays stop at the edges
ask rays [if pxcor = 199 [die]]
ask rays [if pycor = 0 [die]]
ask rays [if pycor = 49 [die]]
ask rays [if pzcor = 0 [die]]
ask rays [if pzcor = 199 [die]]
end
to close
file-close-all
end
to check-date
if date = "march-equinox" [set hour 0]
if date = "summer-solstice" [set hour 24]
if date = "september-equinox" [set hour 48]
if date = "winter-solstice" [set hour 72]
end
looking forward to your recommendations!
thanks in advance
Jana
From our discussion
Idea 1
Have one of the turtles facexyz a point that gives you the correct elevation and azimuth and copying those values for the rest of the turtles. Something like
Ask one-of turtles
[
Facexyz 1000 45663 4663; or something
Ask other turtles[
Set heading [heading] of myself
Set pitch [pitch] of myself.
]
]
Idea 2
If roll is set to 0 pitch should equal elevation and heading should equal azimuth.
So it worked with idea 1. Thanks!
This is the code now:
globals [x
y
z
data_x
data_y
data_z
hour
i
i_test
i_round
leader
]
breed [rays ray]
to setup
ca
file-open "x.txt" ; three files are loaded with x y and z coordinates respectevely.
set data_x file-read
file-close
file-open "y.txt"
set data_y file-read
file-close
file-open "z.txt"
set data_z file-read
file-close
set hour hour - 1
set i 1
set-default-shape rays "line half"
reset-ticks
end
to go
tick
if i = 1440 [
stop]
set i_test i / 60
set i_round round i_test
if i_test = i_round or i = 1 [
set hour hour + 1
set x item hour data_x
set y item hour data_y
set z item hour data_z]
if i_test = i_round or i = 1 [
if y != 0 [
create-leader
create-follower
run-sunshine]]
set i i + 1
close
end
to create-leader
crt 1 [setxyz x y z
facexyz 45 1 45
set leader who
]
end
to create-follower
crt 5 [setxyz random-xcor 70 random-zcor
set heading [heading] of turtle leader
set pitch [pitch] of turtle leader
]
end
to run-sunshine
ask turtles [fd 1
pendown]
end
to close
file-close-all
end