Moore & Von Neumann Example
Model was written in NetLogo 5.0.4
•
Viewed 1085 times
•
Downloaded 153 times
•
Run 0 times
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This model shows how to make Moore (square) and Von Neumann (diamond) neighborhoods of any radius. The built-in primitive neighbors
gives you a Moore neighborhood of radius 1, and the built-in primitive neighbors4
gives you a Von Neumann neighborhood of radius 1, but for other radii you have to use the code like that given here.
THINGS TO NOTICE
World wrapping can be on or off; the same code works fine either way.
Comments and Questions
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: ask n-of 5 patches [ sprout 1 [ ;; see the entry for AT-POINTS in the NetLogo Dictionary ask patches at-points nearby [ 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.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Moore & Von Neumann Example.png | preview | Preview for 'Moore & Von Neumann Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.