Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netlogo - How to move a turtle to top?

Tags:

netlogo

For example, I have 10 turtles at one patch, how do I move a specific turtle (turtle with [color = red]) to the top? Thank you for your help!

like image 655
user3765587 Avatar asked Oct 26 '25 05:10

user3765587


2 Answers

I will assume that the question is about the "z-order" of the turtles and that "moving a turtle to the top", means "have it painted on top of the other ones".

There are two factors that determine the painting order in NetLogo: breeds, and ẁho numbers. Breeds have precedence. As per the Breeds section in the Programming Guide:

The order in which breeds are declared is also the order in which they are layered in the view. So breeds defined later will appear on top of breeds defined earlier;

Turtles within the same breed are painted in their order of creation (inferable in NetLogo by their who number): the older ones are painted first, and the newer ones are painted on top.

The order of creation is not modifiable, but if nothing in your code is holding on to turtle references or who numbers (the latter being inadvisable anyway), you could use hatch to create a clone of a turtle and then kill the old one immediately. For example:

to setup
  clear-all
  create-ordered-turtles 10 [ set size 10 ]
  ask turtles with [ color = red ] [
    hatch 1
    die
  ]
end

The last line would bring all the red turtles (only one in this case) on top.

What if you can't do that, for some reason? Then you can use breeds:

breed [ background-turtles background-turtle ]
breed [ foreground-turtles foreground-turtle ]

to setup
  clear-all
  create-ordered-background-turtles 10 [ set size 10 ]
  ask turtles with [ color = red ] [
    set breed foreground-turtles
  ]
end

You would need as many breeds as you want "layers" of turtles. This may or may not be convenient. The best approach will depend on your specific use case.

like image 79
Nicolas Payette Avatar answered Oct 29 '25 08:10

Nicolas Payette


There is some ambiguity.

To move the turtle to the top of the patch

 Set ycor pycor + .5

To move it to the top of the view

 Set ycor max-pycor

To make it the top of the stack in the photoshop kind of way. Not so easy. turtles are displayed in order of their who ids. Who ids can not be changed. So if you want red to be on top either create it last or have it swap values with the turtle on top. Sorry.

like image 22
King-Ink Avatar answered Oct 29 '25 09:10

King-Ink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!