Putting Turtles in Boxes

How do you start turtles off in a confined space, like a room or a box?

Solution: 

Making a box and Finding CoordinatesFirst draw your box. You can do this through the Edit Terrain button at the bottom right corner of Spaceland. You'll also need to find the x and y coordinates of your box's corners. When you're in Edit Terrain mode, the x and y coordinates of your mouse are displayed at the bottom left of the window. Hover over any patch you'd like to see the position of.
The coordinates of my box are -11x, -4y for the top right corner, and -39x, -28y for the bottom left corner.

To force a position for a turtle, you can use set xy. To figure out what we want to put in the values for xcor and ycor, first we'll describe the size of the box by finding the difference between the minimum and maximum coordinates of the box for both x and y. This means we subtract the smaller x coordinate from the larger, and the same with the y coordinate. My smaller x coordinate is -39 and my larger is -11.
(-11) - (-39) = 28 for my x coordinates, and (-4) - (-28) = 24 for my y coordinates. This means my box is 28 patches wide, and 24 patches tall. We want the turtles to end up anywhere within this box, so we'll use random 28 in the xcor slot, and random 24 in the ycor slot.

Go to BoxThis will make the agents arrange themselves with a positive xcor and ycor. My box's coordinates are all negative. We need to align the corner that's automatically at 0,0 (the bottom left corner) to the bottom left corner of the box where we want the turtles to be. We do this by subtracting the absolute value of lowest x coordinate of our box from the random number in the xcor slot, and the same for the y coordinate. (An Absolute Value is the distance a number is from 0.) If our coordinates were positive, we'd be adding to the random numbers instead of subtracting.

You can call this from anywhere in your code. This example I put in the Setup code of an epidemic model. Of course, this doesn't stop agents from leaping over walls.

See also: 
Submitted by talaya on 15. January 2009 - 22:12