neighbors4

 

neighbors4 reports an agentset that contains the four patches that surround an agent's current patch in the north, east, west, and south directions. Both turtles and patches are allowed to use this reporter. For example, if we wanted to create a model in which a fire spread from one patch to its neighbors, we would write the following code:

ask patches [
    if pcolor = red [
        ask neighbors4 [
            if pcolor = green [
                set pcolor red
            ]
        ]
    ]
]

Things to keep in mind when using neighbors4:

In the model example below, there is a fire spreading in a forest. A patch on fire will spread to its neighboring patches in cardinal directions that contain trees. If a fire patch has brown neighboring patches that indicate ground, it will turn the ground patches gray to indicate the edge of the forest fire.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the neighbors4 primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the neighbors4 primitive:
 
 
Similar primitives:
neighbors

Reports an agentset containing the eight neighboring patches.

Read more
patch-ahead

Reports the single patch that is the given distance “ahead” of this turtle

Read more
turtles-here

Reports the agentset of all the turtles on a caller's patch.

Read more
patches

Reports an agentset that contains all the patches in a model.

Read more
 
Learn another primitive: