Wandering Letters

Wandering Letters preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)


computer science 

Tagged by Reuven M. Lerner almost 11 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 375 times • Downloaded 78 times • Run 0 times
Download the 'Wandering Letters' modelDownload this modelEmbed this model

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


This model illustrates how to build a word processor where each of the letters acts independently. Each letter knows only which letter comes before it and how long its word is. When the letters or margins are moved, the letters find their own ways back to their proper locations.


Each letter looks both for changes in the margins and for being out of sync with its "leader", which is the letter before it in the text. If it is not in its proper place relative to its leader, it takes a step in its leader's direction.


SETUP initializes the margins and some sample sentences for the text.

GO starts and stops the simulation.

The LEFT-MARGIN and RIGHT-MARGIN sliders let you move the sliders. You can move these sliders while GO is running and the letters will adjust on the fly.

SCATTER LETTERS scatters the letters randomly about the View. You can press this even while GO is running.

LINE-SPACING controls how much space there is between lines.


How do the letters find their way home? In what ways is this different from the behavior of letters in a standard word processor?


While GO is pressed try:

  • Moving the margin sliders
  • Changing the spacing
  • Pushing the scatter button
  • Combinations of all three


Can you extend the model so the user can type his/her own message? You might want to use the user-input primitive for this.

Sometimes a space will end up at the beginning of a new line. Try to fix it so that spaces are ignored at the ends of lines.


Note the use of a blank shape to make a turtle that is invisible except for its label.


This model is based on a Smalltalk program of the same name developed by Ted Kaehler of Disney Interactive.


If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:


Copyright 1997 Uri Wilensky.


This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.

This model was developed at the MIT Media Lab using CM StarLogo. See Wilensky, U. (1993). Thesis - Connected Mathematics: Building Concrete Relationships with Mathematical Knowledge. Adapted to StarLogoT, 1997, as part of the Connected Mathematics Project. Adapted to NetLogo, 2001, as part of the Participatory Simulations Project.

This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2001.

Comments and Questions

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

Click to Run Model

  text        ;; text to be written
  left-edge   ;; pxcor of leftmost column where letters can go
  right-edge  ;; ditto for rightmost

  word-length ;; the length of the word the turtle is in
  num-word    ;; the number the turtle's word is in the text
  leader      ;; the turtle with the character before this turtle's character
  new-line?   ;; whether we should start a new line or not

;; setup the variables and the View

to setup
  set text (word "These are the times that try men's souls. "
                ;; the next two sentences each contain all 26
                ;; letters of the alphabet!  (which makes them
                ;; good for testing typewriters)
                "The quick brown fox jumped over the lazy dog. "
                "Pack my box with five dozen liquor jugs.")
  ;; make the turtles "disappear" using a shape that has nothing
  ;; in it.  we can't use hide-turtle since we want to be able to see the
  ;; turtle's label
  set-default-shape turtles "blank"
  ;; create enough turtles to have one for every character in text
  crt (length text)
    set word-length 0
    set leader nobody
    set new-line? false

to scatter  ;; turtle procedure
  setxy random-xcor random-ycor

to draw-margins
  ask patches
    [ ifelse (pxcor = left-margin + min-pxcor) or
             (pxcor = max-pxcor - right-margin)
        [ set pcolor red ]
        [ set pcolor black ] ]

;; assign the letters to the turtles, determine who leads them,
;; and set how long each word is

to setup-letters
  let remaining-text text
  let word-count 1
  let prev-letter nobody

  ask turtles
    set label first remaining-text
    set leader prev-letter
    set num-word word-count
    if label = " "
      set word-count word-count + 2
      set num-word num-word + 1
    set remaining-text but-first remaining-text
    set prev-letter self

  let index 1
  repeat word-count
    let turtles-in-word turtles with [num-word = index]
    ask turtles-in-word
      [ set word-length count turtles-in-word ]
    set index index + 1

;; the main procedure called by the GO forever button

to go
  ask turtles
    set new-line? false
    pick-heading  ;; choose the heading to the appropriate patch
    ;; if the turtle is not satisfied with where it is in
    ;; relation to its leader, go forward a bit
    ifelse happy?
      [ move-to patch-here ]  ;; move to center of patch
      [ fd 0.5 ]

;; updates the left-edge and right-edge variables to match the margin
;; sliders, and redraws the margins if necessary

to update-margins
  if left-edge != left-margin + min-pxcor + 1
    [ set left-edge left-margin + min-pxcor + 1
      draw-margins ]
  if right-edge != max-pxcor - right-margin - 1
    [ set right-edge max-pxcor - right-margin - 1
      draw-margins ]

;; set the heading of the turtle to the appropriate patch

to pick-heading
  ifelse leader = nobody
    ;; first letter goes to upper-leftmost patch
    [ face patch left-edge max-pycor ]
    ;; other patches head toward patch to the right of leader's patch
      let p [patch-at 1 0] of leader
      if p != nobody
      [ face p ]
      ifelse right-edge - left-edge < word-length
        ;; if our word is too long for the width of text,
        ;; check to see if the patch to the right of the leader is on or over
        ;; the right margin
        if [pxcor] of leader >= right-edge
          ;; if it is change the heading to the beginning of the next line
          set new-line? true
          face patch left-edge new-line-pycor
        ;; if our word is short enough to fit on a single line but there is not
        ;; enough room on this particular line, try to word wrap.  that is to say,
        ;; if the turtle is at the beginning of a word and the word is too long to
        ;; be completed on a single line given the starting point of the word,
        ;; have the leader of the word, move to the next line.
        if (num-word != [num-word] of leader) and
           ([pxcor] of leader + word-length >= right-edge)
          ;; if it is change the heading to the beginning of the next line
          set new-line? true
          face patch left-edge new-line-pycor

;; reports pycor of the new line the turtle is supposed to go to
;; based on the location of its leader and the width of the text

to-report new-line-pycor  ;; turtle procedure
  ifelse abs ([pycor] of leader - line-spacing) > max-pycor
    [ report [pycor] of leader ]
    [ report [pycor] of leader - line-spacing ]

;; reports true if turtle is satisifed with its current position

to-report happy?  ;; turtle procedure
  if leader = nobody  ;; if the turtle is the first letter...
    ;; ...is it on the upper-left-most patch that it can be?
    [ report (pxcor = left-edge) and (pycor = max-pycor) ]
  ifelse new-line?  ;; do we want to start a new-line?
    ;; is the turtle at the beginning of the next line?
    [ report (pxcor = left-edge) and (pycor = new-line-pycor) ]
      ;; is the turtle on the patch to the right of its leader?
    [ report patch-at -1 0 = [patch-here] of leader ]

; Copyright 1997 Uri Wilensky.
; See Info tab for full copyright and license.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky almost 11 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 11 years ago Updated version tag Download this version
Uri Wilensky over 11 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky over 12 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 14 years ago Model from NetLogo distribution Download this version
Uri Wilensky almost 14 years ago Wandering Letters Download this version

Attached files

File Type Description Last updated
Wandering Letters.png preview Preview for 'Wandering Letters' about 11 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.