# Moore & Von Neumann Example

### 1 collaborator

Uri Wilensky (Author)

### Tags

code example

Tagged by Reuven M. Lerner over 9 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 628 times • Downloaded 35 times • Run 0 times

Click to Run Model

```globals [nearby]

to setup
clear-all
;; use a global variable to store the offsets list so we
;; don't have to compute the same list (for a particular
;; radius) over and over again
ifelse neighborhood-type = "Von Neumann"
[ set nearby von-neumann-offsets radius include-central-patch? ]
[ set nearby moore-offsets radius include-central-patch? ]
;; Now whenever a turtle or patch wants to refer
;; to its neighborhood, it says "patches at-points nearby".
;; A quick example:
sprout 1 [
;; see the entry for AT-POINTS in the NetLogo Dictionary
set pcolor [color] of myself
]
set color white
]
]
end

;; The next two procedures generate the lists of offsets that we pass to
;; AT-POINTS.
;;
;; When you paste this code into your model, if you know that you always
;; want to include the center, or never want to include it, you can remove
;; that parameter to make the code shorter.

;; examples of usage:
;; > print moore-offsets 1 true
;; [[-1 1] [1 1] [-1 -1] [-1 0] [1 0] [0 1] [0 0] [0 -1] [1 -1]]
;; > print von-neumann-offsets 1 true
;; [[0 1] [-1 0] [1 0] [0 0] [0 -1]]
;; for larger radii the results lists get long fast!

to-report moore-offsets [n include-center?]
let result [list pxcor pycor] of patches with [abs pxcor <= n and abs pycor <= n]
ifelse include-center?
[ report result ]
[ report remove [0 0] result ]
end

to-report von-neumann-offsets [n include-center?]
let result [list pxcor pycor] of patches with [abs pxcor + abs pycor <= n]
ifelse include-center?
[ report result ]
[ report remove [0 0] result ]
end

; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.
```

There are 10 versions of this model.

Uri Wilensky over 9 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky almost 10 years ago Updated version tag Download this version
Uri Wilensky over 10 years ago Updated to NetLogo 5.0 Download this version