Next Patch Example

Next Patch Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 408 times • Downloaded 24 times • Run 0 times
Download the 'Next Patch Example' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;; This stuff is just for demoing purposes.  The NEXT-PATCH procedure,
;; below, is the real meat of the code example.

turtles-own [next prev]

to setup
  clear-all
  crt 1
    [ set color green
      set next patch-here
      set prev patch-here
      set pcolor gray ]
  reset-ticks
end 

to go
  ask turtles
    [ ask next [ set pcolor black ]
      ask prev [ set pcolor black ]
      set prev patch-here
      ;; Note that even going fd 0.01 might still cross two patch boundaries.
      ;; No matter how small a finite distance you move forward, you might
      ;; jump right over the corner of a patch.  But this is fine for demo
      ;; purposes.
      fd 0.01
      ask prev [ set pcolor gray ]
      set next next-patch
      ask next [ set pcolor red ] ]
  tick
end 

;; Here's the important procedure.  The idea behind it is to
;; compare the turtle's current heading to the headings to the
;; corners of the patch it is standing on.

to-report next-patch  ;; turtle procedure
  if heading < towardsxy (pxcor + 0.5) (pycor + 0.5)
    [ report patch-at 0 1 ]
  if heading < towardsxy (pxcor + 0.5) (pycor - 0.5)
    [ report patch-at 1 0 ]
  if heading < towardsxy (pxcor - 0.5) (pycor - 0.5)
    [ report patch-at 0 -1 ]
  if heading < towardsxy (pxcor - 0.5) (pycor + 0.5)
    [ report patch-at -1 0 ]
  report patch-at 0 1
end 

;; The above code is good enough for most practical purposes, but it glosses
;; over a few subtleties that you might conceivably care about.
;;
;; The above code always returns one of the four neighboring patches
;; (north, south, east, west), aka NEIGHBORS4 or the "Von Neumann
;; neighborhood".  It never returns a diagonal neighbor.
;;
;; If the turtle is headed directly for the corner of its patch, though,
;; it would be more correct to return a diagonal neighbor in that
;; case.  If the turtle's heading was determined by RANDOM-FLOAT, then
;; such a situation is vanishingly unlikely.  Anyway, the inherent imprecision
;; of floating point math and the lack of continuous motion in NetLogo means
;; that depending on what you're doing, it is probably not useful or meaningful
;; to talk about whether the turtle is headed exactly at a corner.
;;
;; There's an even further subtlety in that NetLogo patches are closed on
;; their south and west boundaries and open on their north and east
;; boundaries.  That means that the point where four patches meet always
;; belongs to the northeastern patch.  So if you want the diagonal patches,
;; you have to decide how to handle the case where the turtle is pointed
;; exactly at a corner.  Do you want to always return the northeastern
;; patch at that corner, even though the turtle's path may only intersect
;; that patch at a single point?  Or do you want to ignore the corner point
;; and look at the patch that lies just beyond that point?  The following
;; code implements the latter choice.
;;
;; This version does more work than the four-neighbor version,
;; so it will run more slowly.  In most situations, we suggest
;; just sticking with the four-neighbor version.

to-report next-patch-8  ;; turtle procedure
  let diff heading - towardsxy (pxcor + 0.5) (pycor + 0.5)
  if diff < 0 [ report patch-at 0 1 ]
  if diff = 0 [ report patch-at 1 1 ]
  set diff heading - towardsxy (pxcor + 0.5) (pycor - 0.5)
  if diff < 0 [ report patch-at 1 0 ]
  if diff = 0 [ report patch-at 1 -1 ]
  set diff heading - towardsxy (pxcor - 0.5) (pycor - 0.5)
  if diff < 0 [ report patch-at 0 -1 ]
  if diff = 0 [ report patch-at -1 -1 ]
  set diff heading - towardsxy (pxcor - 0.5) (pycor + 0.5)
  if diff < 0 [ report patch-at -1 0 ]
  if diff = 0 [ report patch-at -1 1 ]
  report patch-at 0 1
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.

Uploaded by When Description Download
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
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Model from NetLogo distribution Download this version
Uri Wilensky over 12 years ago Next Patch Example Download this version
Uri Wilensky over 12 years ago Next Patch Example Download this version

Attached files

File Type Description Last updated
Next Patch Example.png preview Preview for 'Next Patch Example' over 9 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.