;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; ;; Langton's Ant on the Infinite Plane ;; ;; ----------------------------------- ;; ;; ;; ;; Langton's Ant discovered by ;; ;; Chris Langton (1980s) ;; ;; ;; ;; Implementation by ;; ;; Ivan Filippenko (February 2006) ;; ;; ;; ;; Credit to Firouz Bharthania, ;; ;; a student of Professor Russ Abbott ;; ;; at Cal. State Los Angeles, ;; ;; for the core idea of turtle-based ;; ;; unbounded NetLogo worlds ;; ;; exposed by a scrolling View. ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;; Variables ;; ;;;;;;;;;;;;;;; globals [ ;; Constants. SETUP-WIDTH-HEIGHT SETUP-VIEW-ONLY WORLD-TORUS WORLD-BOUNDED-RECTANGLE WORLD-INFINITE-PLANE world ;; Indicates whether execution should be terminated. ;; Termination is final, unlike just clicking the ;; "go" button for a stop which can be restarted. terminate? ticks langton ;; an ant: specifically, Langton's Ant ant-color asphalt-color ;; (viewcenter-x, viewcenter-y) are the Cartesian ;; coordinates of the point in the (infinite) real plane ;; at which the NetLogo View should be centered -- ;; that is, of the origin of the View's coordinate system. ;; ;; Thus, a turtle's (xcor, ycor) View coordinates ;; map to its intrinsic (x, y) coordinates ;; according to the equation: ;; ;; (x, y) = (viewcenter-x + xcor, viewcenter-y + ycor). ;; ;; See the reporters "get-intrinsic-x" and "get-intrinsic-y". viewcenter-x viewcenter-y initial-max-x initial-min-x initial-max-y initial-min-y max-x min-x max-y min-y mouse-button-down old-mouse-x old-mouse-y cumulative-delta-x cumulative-delta-y ] ;; The order of declaration of the breeds determines ;; the order in which turtles of those breeds are layered ;; on top of each other when they occupy the same patch -- ;; breeds declared earlier appear nearer to the bottom, ;; while those declared later appear nearer to the top. ;; So, since we want Langton's ant to appear on top of ;; each turf rather than underneath it, the "ants" breed ;; must be declared after the "turfs" breed. breeds [ turfs ants ] turtles-own [ ;; (x, y) are the turtle's intrinsic Cartesian coordinates ;; on the virtual board, i.e. in the (infinite) real plane. ;; Thus, the following equations express these intrinsic ;; coordinates in terms of its View coordinates (xcor, ycor) ;; and the intrinsic coordinates (viewcenter-x, viewcenter-y) ;; of the View's origin: ;; x = viewcenter-x + xcor ==> xcor = x - viewcenter-x ;; y = viewcenter-y + ycor ==> ycor = y - viewcenter-y ;; ;; See the reporters "get-intrinsic-x" and "get-intrinsic-y", ;; as well as the inverse reporters "get-view-x" and "get-view-y". x y ] turfs-own [ ;; A piece of turf may be of type "grass" or "asphalt". turf-type grass-color ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Setup and Initialization ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to setup terminate clear-all ;; alias: ca init setup-patches ; display ; ; no-display ;; Order of breed setup is important: ;; see the comments in the "step" procedure. ;; In particular, create Langton's ant as (turtle 0) ;; before creating any of the turfs, which will therefore ;; have ids starting with (turtle 1). setup-ants ;; for debugging print "Ants created: " + count ants ifelse (initial-setup = SETUP-VIEW-ONLY or world = WORLD-TORUS) [ setup-turfs-view-only ] [ ;; initial-setup = SETUP-WIDTH-HEIGHT setup-turfs ] ;; for debugging print "Initial turfs created: " + count turfs update-view ;; ends with "display" unterminate end ;; setup to init set SETUP-WIDTH-HEIGHT "By initial world width and height" set SETUP-VIEW-ONLY "View only" set WORLD-TORUS "torus (NetLogo default)" set WORLD-BOUNDED-RECTANGLE "bounded rectangle" set WORLD-INFINITE-PLANE "infinite plane" set world world-topology setup-mouse set-default-shape turfs "patch-fill" set-default-shape ants "ant 2" ;; Color 15 is the color named "red". ;; "- 1" makes a bit darker, while + 1 makes it a bit lighter. set ant-color red - 1 ;; red = 15 ;; Color 0 is the color named "black". set asphalt-color black set viewcenter-x 0 set viewcenter-y 0 set cumulative-delta-x 0 set cumulative-delta-y 0 set ticks 0 end ;; init to init-world-boundary set initial-max-x screen-edge-x + (initial-world-width - screen-size-x) / 2 set initial-min-x (- initial-max-x) set initial-max-y screen-edge-y + (initial-world-height - screen-size-y) / 2 set initial-min-y (- initial-max-y) set-world-boundary initial-max-x initial-min-x initial-max-y initial-min-y end ;; init-world-boundary to set-world-boundary [ new-max-x new-min-x new-max-y new-min-y ] set max-x new-max-x set min-x new-min-x set max-y new-max-y set min-y new-min-y end ;; set-world-boundary to-report margin-x report max-x - initial-max-x end ;; margin-x to-report margin-y report max-y - initial-max-y end ;; margin-y to setup-mouse set mouse-button-down false end ;; setup-mouse to setup-patches ask patches [ set pcolor white ] end ;; setup-patches to setup-ants ask (patch 0 0) [ sprout-ants 1 [ set color ant-color set size 2 set heading ifelse-value (ant-heading = "north") [ 0 ] [ ifelse-value (ant-heading = "south") [ 180 ] [ ifelse-value (ant-heading = "east") [ 90 ] [ ;; ant-heading = "west" 270 ] ] ] set langton self ] ] end ;; setup-ants to setup-turfs-view-only set initial-world-width screen-size-x set initial-world-height screen-size-y setup-turfs end ;; setup-turfs-view-only to setup-turfs init-world-boundary let row max-y while [ row >= min-y ] [ let col min-x while [ col <= max-x ] [ ask (patch 0 0) [ ;; col and row will be the intrinsic x and y coordinates, ;; respectively, of the sprouted turf. sprout-a-turf col row ] set col col + 1 ] set row row - 1 ] end ;; setup-turfs to sprout-a-turf [ intrinsic-x intrinsic-y ] ;; patch procedure sprout-turfs 1 [ set grass-color (54 + random-float 2.0) set-grass set heading 0 set x intrinsic-x set y intrinsic-y ] end ;; sprout-a-turf to set-grass ;; turf procedure set turf-type "grass" set color grass-color end ;; set-grass to set-asphalt ;; turf procedure set turf-type "asphalt" set color asphalt-color end ;; set-asphalt ;;;;;;;;;;;;;;;;;;;;;;;;; ;; The Simulation Step ;; ;;;;;;;;;;;;;;;;;;;;;;;;; to go ;; "stop" exits from its enclosing procedure. ;; It stops a forever button if the button ;; invokes the procedure directly. if terminate? [ stop ] if fast-forward-to-10000? [ no-display while [ ticks < 10000 ] [ step ] display ] step end ;; go to terminate set terminate? true end to unterminate set terminate? false end to step set ticks ticks + 1 if (world = WORLD-TORUS) [ step-torus ] if (world = WORLD-BOUNDED-RECTANGLE) [ step-rectangle ] if (world = WORLD-INFINITE-PLANE) [ step-plane ] end ;; step to step-torus ask langton [ without-interruption [ ;; One-one correspondence of turfs to patches. let ant-turf one-of turfs-here ant-action ant-turf ;; No need for any special "move-torus" ;; when living in the torus geometry. forward 1 ] ] end ;; step-torus to ant-action [ turf ] ask turf [ without-interruption [ ifelse is-grass? [ set-asphalt ask langton [ left 90 ] ] [ ;; here, turf-type = "asphalt" set-grass ask langton [ right 90 ] ] ] ] end ;; ant-action to step-rectangle ask langton [ without-interruption [ ;; Every in-bounds patch has a unique turf. let ant-turf one-of turfs-here ant-action ant-turf move-rectangle 1 ] ] end ;; step-rectangle to move-rectangle [ units ] ;; ant procedure without-interruption [ if at-world-edge? [ rt 180 ] forward units adjust-intrinsic-coordinates units ] end ;; move-rectangle to-report at-world-edge? ;; ant reporter report (y = max-y and heading = 0) or (x = max-x and heading = 90) or (y = min-y and heading = 180) or (x = min-x and heading = 270) end ;; at-world-edge? to step-plane let ant-turf ;; Efficient: constant-time lookup! turtle get-ant-turf-id ;; Very inefficient: O(n^2) ; one-of turfs with [ x = langton-x and y = langton-y ] ;; Not an option: If the ant is outside the View ;; then it's hidden on some patch which does not ;; correspond to the ant's intrinsic location ;; in the world. There may be many turfs also ;; at this patch (only one of which is showing), ;; but it's very likely that none of them are the ;; intrinsically correct one -- and even if one were, ;; there would be no guarantee of retrieving it ;; using the "one-of turtles-here" call. ; one-of turfs-here ant-action ant-turf ask langton [ move-plane 1 ] ;; This block can't be incorporated into the ant's ;; move-plane procedure, because potentially it involves ;; the creation of new turfs , which the ant can't do ;; since turtle creation is observer-only. if ( value-from langton [ out-of-bounds? ] ) [ ;; Expand the world's boundary by creating new turfs. ;; Add a new margin of turfs, exactly one turf wide, ;; all the way around the current world boundary. expand-world ] end ;; step-plane to-report get-ant-turf-id report get-turf-id (x-of langton) (y-of langton) end ;; get-ant-turf-id ;; An efficient (i.e. constant-time) technique for ;; identifying the turtle ID of a turf, given its intrinsic ;; (absolute, non-View-referenced) x and y coordinates. ;; ;; A requirement is to avoid referring to patches: ;; See the comment in the "step-plane" procedure. ;; ;; Recall: The ant is (turtle 0), having been created first. to-report get-turf-id [ turf-x turf-y ] ;; It's reasonably straightforward to map the intrinsic ;; (x, y) coordinates to the ID number of the turf with ;; those coordinates, for two reasons: ;; (1) "setup-turfs" deterministically orders the initial ;; turfs from top to bottom and left to right, ;; and the initial world width and height are known; ;; (2) the mapping also depends inherently on the order ;; of generation of new turfs when the world is expanded, ;; as implemented by the procedure "expand-world". ;; ;; NOTE: Langton's Ant is (turtle 0) since it's created first. let turf-id -1 ifelse (within-initial-world-bounds? turf-x turf-y) [ set turf-id (initial-max-y - turf-y) * initial-world-width + (turf-x - initial-min-x) + 1 ] [ ;; (turf-x, turf-y) are the intrinsic coordinates ;; of a turf that was created after the construction ;; of the initial world: such a turf's ordinal ;; must be calculated differently from the above. ;; In each case, compute the turf-id by subtracting, ;; from the total number of turfs upon the expansion ;; that generated the specified turf, the number of ;; turfs generated AFTER this turf, according to the ;; "expand-world" algorithm. ;; The number of "expand-world" calls leading up to ;; the generation of the specified turf determines ;; the size of the world at that point, as stored ;; in the local variables turf-creation-world-width ;; and turf-creation-world-height, below. ;; Each case requires its own specific computation ;; of the number of these expansions. let num-expansions 0 ifelse (turf-y > initial-max-y) [ ;; The ant is in the top margin above the initial world. if (turf-x >= initial-min-x and turf-x <= initial-max-x) [ set num-expansions turf-y - initial-max-y ] if (turf-x < initial-min-x) [ set num-expansions max list (turf-y - initial-max-y) (initial-min-x - turf-x) ] if (turf-x > initial-max-x) [ set num-expansions max list (turf-y - initial-max-y) (turf-x - initial-max-x) ] ] [ ifelse (turf-y < initial-min-y) [ ;; The ant is in the bottom margin below the initial world. if (turf-x >= initial-min-x and turf-x <= initial-max-x) [ set num-expansions initial-min-y - turf-y ] if (turf-x < initial-min-x) [ set num-expansions max list (initial-min-y - turf-y) (initial-min-x - turf-x) ] if (turf-x > initial-max-x) [ set num-expansions max list (initial-min-y - turf-y) (turf-x - initial-max-x) ] ] [ ;; Here, turf-y <= initial-max-y and turf-y >= initial-min-y, ;; so the turf is between the initial vertical (y) bounds. ;; But since we already know that the turf is ;; not within the initial world bounds, ;; it must be out of bounds horizontally, ;; either to the left or to the right. ifelse (turf-x < initial-min-x) [ ;; The ant is in the margin to the left of the initial world. set num-expansions initial-min-x - turf-x ] [ ;; Here, turf-x > initial-max-x. ;; The ant is in the margin to the right of the initial world. set num-expansions turf-x - initial-max-x ] ] ] ;; Having determined the number of world expansions ;; (calls to "expand-world") that were needed to generate ;; the turf with intrinsic coordinates (turf-x, turf-y), ;; we can tell the size of the world at that point. ;; Knowing the number of turfs in that world and the ;; position of the specified turf, we can figure out ;; its turtle ID. let turf-creation-world-width (initial-world-width + 2 * num-expansions) let turf-creation-world-height (initial-world-height + 2 * num-expansions) ;; Finally, compute the turf-id. ifelse (turf-y = initial-max-y + num-expansions) [ set turf-id ;; total turfs upon the expansion that generated current turf turf-creation-world-width * turf-creation-world-height ;; minus the turfs following the current turf in top row - (initial-max-x + num-expansions - turf-x) ;; minus the subsequently created turfs in the bottom row - turf-creation-world-width ;; minus the subsequently created turfs in the left ;; and right columns (taking care, in the left column, ;; to count neither the previously created top turf ;; nor the already counted bottom turf, and in the ;; right column not to count the already counted ;; top and bottom turfs) - 2 * (turf-creation-world-height - 2) ] [ ifelse (turf-y = initial-min-y - num-expansions) [ set turf-id ;; total turfs upon the expansion that generated current turf turf-creation-world-width * turf-creation-world-height ;; minus the turfs following the current turf in bottom row - (initial-max-x + num-expansions - turf-x) ;; minus the subsequently created turfs in the ;; left and right columns (taking care, in each column, ;; to count neither the previously created top turf ;; nor the already counted bottom turf) - 2 * (turf-creation-world-height - 2) ] [ ifelse (turf-x = initial-min-x - num-expansions) [ set turf-id ;; total turfs upon the expansion that generated current turf turf-creation-world-width * turf-creation-world-height ;; minus the turfs following the current turf in left column - (turf-y - (initial-min-y - (num-expansions - 1))) ;; minus the subsequently created turfs in the right column ;; (all but the top and bottom two, which were created previously) - (turf-creation-world-height - 2) ] [ ifelse (turf-x = initial-max-x + num-expansions) [ set turf-id ;; total turfs upon the expansion that generated current turf turf-creation-world-width * turf-creation-world-height ;; minus the turfs following the current turf in right column, ;; except for the bottom turf which was created previously - (turf-y - (initial-min-y - (num-expansions - 1))) ] [ user-message "ERROR in procedure \"get-turf-id\", computing the turtle ID\n" + "of a turf from its intrinsic coordinates:\n\n" + "turf-x = " + turf-x + ", turf-y = " + turf-y ] ] ] ] ] ;; for debugging ; print "turf-id = " + turf-id report turf-id end ;; get-turf-id to move-plane [ units ] ;; ant procedure without-interruption [ forward units adjust-intrinsic-coordinates units ifelse in-view? [ ;; The ant is logically within the View: ;; set its View-referenced (xcor, ycor) location ;; and show it. expose-in-view ] [ ;; The ant has moved out of the View: ;; hide it. hideturtle ] ] end ;; move-plane to adjust-intrinsic-coordinates [ units ] ;; ant procedure if heading = 0 [ set y y + units ] if heading = 90 [ set x x + units ] if heading = 180 [ set y y - units ] if heading = 270 [ set x x - units ] end ;; adjust-intrinsic-coordinates ;; A turtle falls within the current "scope" of the View ;; precisely when the absolute distances between ;; its intrinsic coordinates and those of the View ;; are at most half the screen (View) size. ;; ;; NOTE: "in-view?" relies on the intrinsic coordinates ;; (x, y) of the turtle, and (viewcenter-x, viewcenter-y) ;; of the View's origin, being integers. Otherwise the ;; inequality comparisons can produce erroneous results. to-report in-view? ;; turtle procedure report (abs get-view-x x <= screen-edge-x) and (abs get-view-y y <= screen-edge-y) end ;; in-view? ;; Expand the world, adding a margin of notional patches -- ;; implemented as turtle "turfs" -- exactly one unit wide ;; all the way around the current world boundary. ;; ;; The order in which the intrinsic coordinates are chosen ;; for sprouting new turfs is important, as it determines ;; their turtle ID numbers and provides a way to map ;; intrinsic coordinate positions to IDs for the duration ;; of the simulation: see procedure "get-turf-id". ;; ;; The turf generation algorithm, which makes deriving the ;; intrinsic coordinates --> ID mapping reasonably straightforward ;; (though care must taken with the details), is as follows: ;; 0. Increment/decrement the max/min x and y dimensions by one, ;; as appropriate. ;; 1. Add the entire top margin from left to right ;; (increasing x), starting with the new min-x ;; all the way through to the new max-x. ;; 2. Add the entire bottom margin from left to right ;; (increasing x), starting with the new min-x ;; all the way through to the new max-x. ;; 3. Add the left margin from top to bottom ;; (decreasing y), except for the two turfs ;; already created in the top and bottom margins: ;; thus, starting with the former max-y down ;; to the former min-y. ;; 4. Add the right margin from top to bottom ;; (decreasing y), except for the two turfs ;; already created in the top and bottom margins: ;; thus, starting with the former max-y down ;; to the former min-y. ;; ;; All new turfs are initially sprouted on (patch 0 0). ;; Finally, a call to "update-view" migrates to the correct ;; patches those turfs that fall within the scope of the View, ;; and exposes them. to expand-world no-display ;; Remember the current (about to be former) ;; world dimensions. let old-max-x max-x let old-min-x min-x let old-max-y max-y let old-min-y min-y ;; Expand the world dimensions by one unit ;; along each of the four edges. set-world-boundary max-x + 1 min-x - 1 max-y + 1 min-y - 1 ;; col and row will be the intrinsic x and y coordinates, ;; respectively, of the sprouted turfs. ;; Add the entire top margin. let col min-x while [ col <= max-x ] [ ask (patch 0 0) [ sprout-a-turf col max-y ] set col (col + 1) ] ;; Add the entire bottom margin. set col min-x while [ col <= max-x ] [ ask (patch 0 0) [ sprout-a-turf col min-y ] set col (col + 1) ] ;; Add the left margin, except for its top and ;; bottom turfs which have already been added (above). let row old-max-y while [ row >= old-min-y ] [ ask (patch 0 0) [ sprout-a-turf min-x row ] set row (row - 1) ] ;; Add the right margin, except for its top and ;; bottom turfs which have already been added (above). set row old-max-y while [ row >= old-min-y ] [ ask (patch 0 0) [ sprout-a-turf max-x row ] set row (row - 1) ] ;; for debugging print "Total turfs: " + count turfs ;; Update the View to expose the new turfs, ;; migrating them to their correct View locations ;; when appropriate. update-view ;; ends with "display" end ;; expand-world ;; An alternate turf generation algorithm that appears ;; not to be as convenient as "expand-world" for supporting ;; a mapping from intrinsic turtle coordinates to turtle ID numbers. ;; ;; See the procedure "get-turf-id", which relies on the order ;; of turf generation implemented by procedure "expand-world". to expand-world-alternate no-display ;; Remember the current (about to be former) ;; world dimensions. let old-max-x max-x let old-min-x min-x let old-max-y max-y let old-min-y min-y ;; Expand the world dimensions by one unit ;; along each of the four edges. set-world-boundary max-x + 1 min-x - 1 max-y + 1 min-y - 1 ;; col and row will be the intrinsic x and y coordinates, ;; respectively, of the sprouted turfs. ;; Add the entire top margin. let col min-x while [ col <= max-x ] [ ask (patch 0 0) [ sprout-a-turf col max-y ] set col (col + 1) ] ;; For each row between the old world top and bottom, ;; add two new turfs -- one on the left end and one ;; on the right. let row old-max-y while [ row >= old-min-y ] [ ;; Here row <= old-max-y and row >= old-min-y: ;; only sprout new turfs in the left and right ;; vertical margins. ask (patch 0 0) [ sprout-a-turf min-x row sprout-a-turf max-x row ] set row (row - 1) ] ;; Add the entire bottom margin. set col min-x while [ col <= max-x ] [ ask (patch 0 0) [ sprout-a-turf col min-y ] set col (col + 1) ] ;; for debugging print "Total turfs: " + count turfs ;; Update the View to expose the new turfs, ;; migrating them to their correct View locations ;; when appropriate. update-view ;; ends with "display" end ;; expand-world-alternate to-report current-world-width report max-x - min-x + 1 end ;; current-world-width to-report current-world-height report max-y - min-y + 1 end ;; current-world-height to-report num-initial-turfs report initial-world-width * initial-world-height end ;; num-initial-turfs to-report within-initial-world-bounds? [ intrinsic-x intrinsic-y ] report intrinsic-x >= initial-min-x and intrinsic-x <= initial-max-x and intrinsic-y >= initial-min-y and intrinsic-y <= initial-max-y end ;; within-initial-world-bounds? to-report out-of-bounds? ;; ant procedure report x < min-x or x > max-x or y < min-y or y > max-y ; report ; beyond-min-x? or beyond-max-x? ; or beyond-min-y? or beyond-max-y? end ;; out-of-bounds? to-report beyond-min-x? report x < min-x end ;; beyond-min-x? to-report beyond-max-x? report x > max-x end ;; beyond-max-x? to-report beyond-min-y? report y < min-y end ;; beyond-min-y? to-report beyond-max-y? report y > max-y end ;; beyond-max-y? to-report is-grass? ;; turf procedure report turf-type = "grass" end ;; is-grass? to-report is-asphalt? ;; turf procedure report turf-type = "asphalt" end ;; is-asphalt? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Turning Turfs "ON" (asphalt) and "OFF" (grass) ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; User adds asphalt turfs with the mouse. to add-asphalt while [ mouse-down? ] [ ask patch-at mouse-xcor mouse-ycor [ let intrinsic-x get-intrinsic-x pxcor let intrinsic-y get-intrinsic-y pycor let turf-id get-turf-id intrinsic-x intrinsic-y let turf turtle turf-id ask turf [ set-asphalt ] ] ] end ;; add-asphalt ;; User removes asphalt turfs with the mouse. to remove-asphalt while [ mouse-down? ] [ ask patch-at mouse-xcor mouse-ycor [ let intrinsic-x get-intrinsic-x pxcor let intrinsic-y get-intrinsic-y pycor let turf-id get-turf-id intrinsic-x intrinsic-y let turf turtle turf-id ask turf [ set-grass ] ] ] end ;; remove-asphalt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; The Infinite Plane Implementation ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to drag-left-mouse-button-to-scroll-view if (world != WORLD-BOUNDED-RECTANGLE and world != WORLD-INFINITE-PLANE) [ stop ] if (mouse-down? and not mouse-button-down) [ set mouse-button-down true set old-mouse-x mouse-xcor set old-mouse-y mouse-ycor ] if (mouse-button-down and mouse-inside?) [ ;; Compute the difference between the mouse's current ;; (View) x-coordinate and its old x-coordinate on the ;; previous iteration of this "forever" procedure. let delta-x mouse-xcor - old-mouse-x ;; Compute the difference between the mouse's current ;; (View) y-coordinate and its old y-coordinate on the ;; previous iteration of this "forever" procedure. let delta-y mouse-ycor - old-mouse-y set cumulative-delta-x cumulative-delta-x + delta-x set cumulative-delta-y cumulative-delta-y + delta-y let rounded-cumulative-delta-x round cumulative-delta-x let rounded-cumulative-delta-y round cumulative-delta-y if ( abs rounded-cumulative-delta-x >= 1 or abs rounded-cumulative-delta-y >= 1 ) [ ;; Translate (shift) the View's origin horizontally. set viewcenter-x viewcenter-x + rounded-cumulative-delta-x ;; Translate (shift) the View's origin vertically. set viewcenter-y viewcenter-y + rounded-cumulative-delta-y set cumulative-delta-x 0 set cumulative-delta-y 0 ;; Show the appropriate turtles in the View. update-view ] ;; Finally, prepare for the next iteration of this ;; procedure by storing the mouse's current coordinates. set old-mouse-x mouse-xcor set old-mouse-y mouse-ycor ] ;; Detect raising the mouse button. if (mouse-button-down and not mouse-down?) [ set mouse-button-down false set cumulative-delta-x 0 set cumulative-delta-y 0 ; update-view ;; unnecessary ?? ] end ;; drag-left-mouse-button-to-scroll-view to scroll-view-up if (world != WORLD-BOUNDED-RECTANGLE and world != WORLD-INFINITE-PLANE) [ stop ] ;; Translate (shift) the View's origin up. set viewcenter-y viewcenter-y + scroll-increment ;; Show the appropriate turtles in the View. update-view end ;; scroll-view-up to scroll-view-left if (world != WORLD-BOUNDED-RECTANGLE and world != WORLD-INFINITE-PLANE) [ stop ] ;; Translate (shift) the View's origin left. set viewcenter-x viewcenter-x - scroll-increment ;; Show the appropriate turtles in the View. update-view end ;; scroll-view-left to scroll-view-right if (world != WORLD-BOUNDED-RECTANGLE and world != WORLD-INFINITE-PLANE) [ stop ] ;; Translate (shift) the View's origin right. set viewcenter-x viewcenter-x + scroll-increment ;; Show the appropriate turtles in the View. update-view end ;; scroll-view-right to scroll-view-down if (world != WORLD-BOUNDED-RECTANGLE and world != WORLD-INFINITE-PLANE) [ stop ] ;; Translate (shift) the View's origin down. set viewcenter-y viewcenter-y - scroll-increment ;; Show the appropriate turtles in the View. update-view end ;; scroll-view-down ;; This procedure performs the job of exposing ;; the portion of the virtual world to which ;; the View has been "scrolled". to update-view no-display ;; Hide all the turtles. ask turtles [ hideturtle ] ;; Now, expose precisely those turtles (turfs or ant) ;; whose intrinsic (x, y) coordinates fall within ;; the current "scope" of the View -- that is, ;; within screen-edge-x and screen-edge-y distances ;; from viewcenter-x and viewcenter-y, respectively. ;; Expose a turtle by first translating it to its ;; View-referenced location (patch) and then showing it. ask turtles [ without-interruption [ if in-view? [ expose-in-view ] ] ] display end ;; update-view ;; Expose a turtle in the View, by positioning it ;; at the correct View-referenced location -- ;; determined by the differences between its intrinsic ;; coordinates and those of the View -- and then showing it. to expose-in-view ;; turtle procedure ;; See comment on the turtles-own x, y variables, ;; which hold its intrinsic (x, y) coordinates. ;; ;; Remember that "setxy" sets the turtle's ;; View-referenced (xcor, ycor) coordinates. setxy get-view-x x ;; x - viewcenter-x get-view-y y ;; y - viewcenter-y ;; center-on-patch is unnecessary because ;; the turtle's View coordinates are always ;; maintained as integers (by making sure that ;; the same is true of viewcenter-x and viewcenter-y). ; center-on-patch showturtle end ;; expose-in-view to recenter-view restore-viewcenter-origin ;; Show the appropriate turtles in the View. update-view end ;; recenter-view to restore-viewcenter-origin set viewcenter-x 0 set viewcenter-y 0 end ;; restore-viewcenter-origin ;; Not currently used (or needed: see expose-in-view). to center-on-patch ;; turtle procedure setxy pxcor pycor end ;; center-on-patch to center-turtles-on-patches ask turtles with [ not hidden? ] [ center-on-patch ] end ;; center-turtles-on-patches ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Mapping between View-referenced and intrinsic coordinates ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Reporters "get-intrinsic-x" and "get-intrinsic-y" ;; map View-referenced (xcor, ycor) coordinates to ;; intrinsic (x, y) coordinates. to-report get-intrinsic-x [ view-x ] report viewcenter-x + view-x end ;; get-intrinsic-x to-report get-intrinsic-y [ view-y ] report viewcenter-y + view-y end ;; get-intrinsic-y ;; Reporters "get-view-x" and "get-view-y" provide the ;; inverse mapping, of intrinsic (x, y) coordinates to ;; View-referenced (xcor, ycor) coordinates. to-report get-view-x [ intrinsic-x ] report intrinsic-x - viewcenter-x end ;; get-view-x to-report get-view-y [ intrinsic-y ] report intrinsic-y - viewcenter-y end ;; get-view-y @#$#@#$#@ GRAPHICS-WINDOW 429 10 985 587 45 45 6.0 1 10 1 1 1 0 1 1 1 CC-WINDOW 5 601 994 696 Command Center 0 BUTTON 12 151 76 184 setup setup NIL 1 T OBSERVER T NIL BUTTON 89 151 152 184 go go T 1 T OBSERVER T NIL MONITOR 40 251 145 300 NIL ticks 0 1 BUTTON 166 151 243 184 go once go NIL 1 T OBSERVER T NIL SWITCH 13 198 179 231 fast-forward-to-10000? fast-forward-to-10000? 0 1 -1000 BUTTON 237 243 381 276 Drag left mouse button drag-left-mouse-button-to-scroll-view T 1 T OBSERVER T NIL CHOOSER 11 95 213 140 initial-setup initial-setup "By initial world width and height" "View only" 0 SLIDER 9 10 415 43 initial-world-width initial-world-width 61 201 61 2 1 NIL SLIDER 10 53 415 86 initial-world-height initial-world-height 61 201 61 2 1 NIL BUTTON 263 452 358 485 Recenter View recenter-view NIL 1 T OBSERVER T NIL CHOOSER 324 148 416 193 ant-heading ant-heading "north" "south" "east" "west" 0 BUTTON 230 324 308 357 < LEFT scroll-view-left T 1 T OBSERVER T NIL BUTTON 317 324 395 357 RIGHT > scroll-view-right T 1 T OBSERVER T NIL BUTTON 272 285 350 318 UP /\ scroll-view-up T 1 T OBSERVER T NIL BUTTON 271 363 348 396 DOWN \/ scroll-view-down T 1 T OBSERVER T NIL SLIDER 228 407 393 440 scroll-increment scroll-increment 1 10 2 1 1 NIL TEXTBOX 265 220 364 239 SCROLL THE VIEW CHOOSER 252 95 416 140 world-topology world-topology "torus (NetLogo default)" "bounded rectangle" "infinite plane" 2 BUTTON 30 324 156 357 add black cells add-asphalt T 1 T OBSERVER T NIL BUTTON 30 366 156 399 remove black cells remove-asphalt T 1 T OBSERVER T NIL @#$#@#$#@ WHAT IS IT? ----------- This section could give a general understanding of what the model is trying to show or explain. HOW IT WORKS ------------ This section could explain what rules the agents use to create the overall behavior of the model. HOW TO USE IT ------------- This section could explain how to use the model, including a description of each of the items in the interface tab. THINGS TO NOTICE ---------------- This section could give some ideas of things for the user to notice while running the model. THINGS TO TRY ------------- This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model. EXTENDING THE MODEL ------------------- This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc. NETLOGO FEATURES ---------------- This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features. RELATED MODELS -------------- This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest. CREDITS AND REFERENCES ---------------------- This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references. @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 airplane true 0 Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15 ant true 0 Polygon -7500403 true true 136 61 129 46 144 30 119 45 124 60 114 82 97 37 132 10 93 36 111 84 127 105 172 105 189 84 208 35 171 11 202 35 204 37 186 82 177 60 180 44 159 32 170 44 165 60 Polygon -7500403 true true 150 95 135 103 139 117 125 149 137 180 135 196 150 204 166 195 161 180 174 150 158 116 164 102 Polygon -7500403 true true 149 186 128 197 114 232 134 270 149 282 166 270 185 232 171 195 149 186 Polygon -7500403 true true 225 66 230 107 159 122 161 127 234 111 236 106 Polygon -7500403 true true 78 58 99 116 139 123 137 128 95 119 Polygon -7500403 true true 48 103 90 147 129 147 130 151 86 151 Polygon -7500403 true true 65 224 92 171 134 160 135 164 95 175 Polygon -7500403 true true 235 222 210 170 163 162 161 166 208 174 Polygon -7500403 true true 249 107 211 147 168 147 168 150 213 150 ant 2 true 0 Polygon -7500403 true true 150 19 120 30 120 45 130 66 144 81 127 96 129 113 144 134 136 185 121 195 114 217 120 255 135 270 165 270 180 255 188 218 181 195 165 184 157 134 170 115 173 95 156 81 171 66 181 42 180 30 Polygon -7500403 true true 150 167 159 185 190 182 225 212 255 257 240 212 200 170 154 172 Polygon -7500403 true true 161 167 201 150 237 149 281 182 245 140 202 137 158 154 Polygon -7500403 true true 155 135 185 120 230 105 275 75 233 115 201 124 155 150 Line -7500403 true 120 36 75 45 Line -7500403 true 75 45 90 15 Line -7500403 true 180 35 225 45 Line -7500403 true 225 45 210 15 Polygon -7500403 true true 145 135 115 120 70 105 25 75 67 115 99 124 145 150 Polygon -7500403 true true 139 167 99 150 63 149 19 182 55 140 98 137 142 154 Polygon -7500403 true true 150 167 141 185 110 182 75 212 45 257 60 212 100 170 146 172 arrow true 0 Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 box false 0 Polygon -7500403 true true 150 285 285 225 285 75 150 135 Polygon -7500403 true true 150 135 15 75 150 15 285 75 Polygon -7500403 true true 15 75 15 225 150 285 150 135 Line -16777216 false 150 285 150 135 Line -16777216 false 150 135 15 75 Line -16777216 false 150 135 285 75 bug true 0 Circle -7500403 true true 96 182 108 Circle -7500403 true true 110 127 80 Circle -7500403 true true 110 75 80 Line -7500403 true 150 100 80 30 Line -7500403 true 150 100 220 30 butterfly true 0 Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240 Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240 Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163 Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165 Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225 Circle -16777216 true false 135 90 30 Line -16777216 false 150 105 195 60 Line -16777216 false 150 105 105 60 car false 0 Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180 Circle -16777216 true false 180 180 90 Circle -16777216 true false 30 180 90 Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89 Circle -7500403 true true 47 195 58 Circle -7500403 true true 195 195 58 circle false 0 Circle -7500403 true true 0 0 300 circle 2 false 0 Circle -7500403 true true 0 0 300 Circle -16777216 true false 30 30 240 cow false 0 Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167 Polygon -7500403 true true 73 210 86 251 62 249 48 208 Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123 cylinder false 0 Circle -7500403 true true 0 0 300 dot false 0 Circle -7500403 true true 90 90 120 face happy false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240 face neutral false 0 Circle -7500403 true true 8 7 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Rectangle -16777216 true false 60 195 240 225 face sad false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183 fish false 0 Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166 Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165 Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60 Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166 Circle -16777216 true false 215 106 30 flag false 0 Rectangle -7500403 true true 60 15 75 300 Polygon -7500403 true true 90 150 270 90 90 30 Line -7500403 true 75 135 90 135 Line -7500403 true 75 45 90 45 flower false 0 Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135 Circle -7500403 true true 85 132 38 Circle -7500403 true true 130 147 38 Circle -7500403 true true 192 85 38 Circle -7500403 true true 85 40 38 Circle -7500403 true true 177 40 38 Circle -7500403 true true 177 132 38 Circle -7500403 true true 70 85 38 Circle -7500403 true true 130 25 38 Circle -7500403 true true 96 51 108 Circle -16777216 true false 113 68 74 Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218 Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240 house false 0 Rectangle -7500403 true true 45 120 255 285 Rectangle -16777216 true false 120 210 180 285 Polygon -7500403 true true 15 120 150 15 285 120 Line -16777216 false 30 120 270 120 leaf false 0 Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195 Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195 line true 0 Line -7500403 true 150 0 150 300 line half true 0 Line -7500403 true 150 0 150 150 patch-fill false 0 Rectangle -7500403 true true 0 0 300 300 pentagon false 0 Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120 person false 0 Circle -7500403 true true 110 5 80 Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90 Rectangle -7500403 true true 127 79 172 94 Polygon -7500403 true true 195 90 240 150 225 180 165 105 Polygon -7500403 true true 105 90 60 150 75 180 135 105 plant false 0 Rectangle -7500403 true true 135 90 165 300 Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285 Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285 Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210 Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135 Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135 Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60 Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90 square false 0 Rectangle -7500403 true true 30 30 270 270 square 2 false 0 Rectangle -7500403 true true 30 30 270 270 Rectangle -16777216 true false 60 60 240 240 star false 0 Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108 target false 0 Circle -7500403 true true 0 0 300 Circle -16777216 true false 30 30 240 Circle -7500403 true true 60 60 180 Circle -16777216 true false 90 90 120 Circle -7500403 true true 120 120 60 tree false 0 Circle -7500403 true true 118 3 94 Rectangle -6459832 true false 120 195 180 300 Circle -7500403 true true 65 21 108 Circle -7500403 true true 116 41 127 Circle -7500403 true true 45 90 120 Circle -7500403 true true 104 74 152 triangle false 0 Polygon -7500403 true true 150 30 15 255 285 255 triangle 2 false 0 Polygon -7500403 true true 150 30 15 255 285 255 Polygon -16777216 true false 151 99 225 223 75 224 truck false 0 Rectangle -7500403 true true 4 45 195 187 Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194 Rectangle -1 true false 195 60 195 105 Polygon -16777216 true false 238 112 252 141 219 141 218 112 Circle -16777216 true false 234 174 42 Rectangle -7500403 true true 181 185 214 194 Circle -16777216 true false 144 174 42 Circle -16777216 true false 24 174 42 Circle -7500403 false true 24 174 42 Circle -7500403 false true 144 174 42 Circle -7500403 false true 234 174 42 turtle true 0 Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210 Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105 Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105 Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87 Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210 Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99 wheel false 0 Circle -7500403 true true 3 3 294 Circle -16777216 true false 30 30 240 Line -7500403 true 150 285 150 15 Line -7500403 true 15 150 285 150 Circle -7500403 true true 120 120 60 Line -7500403 true 216 40 79 269 Line -7500403 true 40 84 269 221 Line -7500403 true 40 216 269 79 Line -7500403 true 84 40 221 269 x false 0 Polygon -7500403 true true 270 75 225 30 30 225 75 270 Polygon -7500403 true true 30 75 75 30 270 225 225 270 @#$#@#$#@ NetLogo 3.0.2 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ @#$#@#$#@