max-pxcor
reports the pxcor
of the rightmost patches in a model. This primitive, and its siblings min-pxcor
, max-pycor
, min-pycor
, are very useful in modeling the agent behavior that involves the boundaries of an environment. For example, if we wanted to build a model where we had a wall at the edges, we would write the following code:
ask patches [
if pxcor = max-pxcor or
pxcor = min-pxcor or
pycor = max-pycor or
pycor = min-pycor [
set pcolor gray
]
]
Or if we wanted turtles to not walk beyond the world's borders, we would write the following code:
ask turtles [
if [pxcor] of patch-ahead 1 < max-pxcor [
forward 1
]
]
Things to keep in mind when using max-pxcor
:
max-pxcor
,min-pxcor
,max-pycor
, and min-pycor
are not variables; they are constant reporters. That is, a code such as set max-pxcor 30
would show an error message. resize-world
primitive.In the model example below, we use max-pxcor
and its siblings to create walls that represent a container. The balls inside the container bounce off of the green wall but they stick to the red wall.
xxxxxxxxxx
to setup
clear-all
ask patches with [pxcor = max-pxcor][
set pcolor green
]
ask patches with [pycor = max-pycor][
set pcolor green
]
ask patches with [pxcor = min-pxcor][
set pcolor red
]
ask patches with [pycor = min-pycor][
set pcolor red
]
create-turtles 5 [
set shape "circle"
]
reset-ticks
end
to go
ask turtles [
if pcolor = black [
forward 0.3
]
if pcolor = green [
right 180
forward 0.3
]
]
tick
end
Once you mastered the max-pxcor
primitive, don't stop there. Check out the resources below to improve your NetLogo skills.
max-pxcor
primitive:neighbors4
Reports an agentset containing the four neighboring patches in cardinal directions (north, west, south, east).