turtle-typewriter


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.


WHAT IS IT?


-------
A model to demonstrate layout of text using a turtle to "print" the text into the drawing layer.

HOW IT WORKS


--------
Each printable character has a defined turtle shape. This is the "font" that the turtle uses to imprint characters into the view.
If a particular character is not defined, then the turtle label is set to the character needed.

Procedures

NetLogo Version: NetLogo 4.0.4

globals
[ ospace
  oscale
  oline
  default
]
breed [ typewriters typewriter ]
breed [ letters letter ]

typewriters-own
[ text
  dels
  bx by
]

letters-own
[ bx by
]

to startup test end

to test
   ca
   ask patches
   [ set pcolor gray - 2 + 1 * ((pxcor + pycor) mod 2) ]
   
   set ospace char-space
   set oline line-space
   set oscale scale
   
   let this nobody
   set default (word "This is a test of using turtle shapes for text from a \"string.\""
                      "\n\nDid it work correctly? @#$%^&*()_+-={}[]/\\"
                      "\n\nabcdefghijklmnopqrstuvwxyz 193.254.67.80" 
                 )
   create-typewriters 1
   [ set shape "_blank"
     set size scale
     set color white
     set heading 0
     
     pagefeed
     set this self
   ]
   ask this
   [ let string sample
     if string = "" [ set string default ]
     set dels " -\n"
     set text split string dels
     
     print text
     
     foreach text
     [ ifelse ? = "\n"
       [ newline ]
       [ if xcor > 0 and ( (xcor + (length ? - 1) * scale * char-space >= max-pxcor )  and not ( length ? = 1 and member? ? dels and xcor <= max-pxcor))
         [ newline ]
         let i 0
         repeat length ?
         [ let char lower (item i ?)
           let new-shape ""
           ifelse member? char shapes
           [ set new-shape char ]
           [ set new-shape "[x]" ]
           hatch-letters 1
           [ ifelse new-shape = "[x]"
             [ set shape "_blank" set label char ]
             [ set shape new-shape ]
             ask other letters-here with [ bx = [ bx ] of myself and by = [ by ] of myself ]
             [ die ]
           ]
           advance
           set i i + 1
         ]
         
       ]
     ]
     die
   ]
end

to applybxby
   
end

to advance
   set bx ( xcor + char-space * scale ) 
   ifelse bx + scale > max-pxcor + .49
   [ newline ]
   [ setxy bx by ]
end

to newline ;; typewriter procedure
   set bx (min-pxcor + char-space * scale)
   set by (ycor + line-space * scale )
   ifelse by - scale < min-pycor - .49
   [ pagefeed ]
   [ setxy bx by ]
end
   

to pagefeed
   set bx (min-pxcor + char-space * scale)
   set by (max-pycor + line-space * scale)
   setxy bx by
end

to-report split [ string delims ]
   let result []
   let piece ""
   let char ""
   
   repeat length string
   [ set char first string
     ;; is char a word delimiter?
     ifelse member? char delims
     [ ;; yes, add current piece to result
       if piece != ""
       [ set result fput piece result 
         set piece ""
       ]
       ;; add delimiter to result
       set result fput char result
     ]
     [ ;; no, add to piece
       set piece (word piece char)
     ]
     ;; remove first character
     set string but-first string
   ]
   ;; is there a piece in progress?, if so, add it to the result
   if piece != "" [ set result fput piece result set piece "" ]
   ;; reverse the result
   set result reverse result
   ;; report the result
   report result
end   
     
to-report lower [ char ]
   let c position char "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   ifelse c = false 
   [ set c char ]
   [ set c item c      "abcdefghijklmnopqrstuvwxyz" ]
   report c
end

to-report upper [ char ]
   let c position char "abcdefghijklmnopqrstuvwxyz"
   ifelse c = false 
   [ set c char ]
   [ set c item c      "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ]
   report c
end
   
   
   
   
   

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download turtle-typewriter --