turtleArt Thumbtack String Corkboard

This commented Logo source code creates a thumbtack, a cork board, and then draws random string art using them.

Read the ;comments to learn more about what the primitives do and what their shortcuts are.

Try some of the primitives out in turtleSpaces interactive mode (the myrtle prompt) and see what they do!

You can drag-select the source code and then paste it into the turtleSpaces editor (which you enter by typing ed and Enter or pressing control-shift-E) using control-shift-V (paste)

Then exit back to interactive mode using control-shift-O (save) and then type stringart and Enter to execute it.

Change elements of the code and see what happens!

TO tack
  ;create a thumbtack, or pushpin
  
  setfillcolor pick without 5 without 10 range [1 15]
  ;pick a number between 1 and 15 except 5 and 10
  
  cylinder 4 2 20
  ;cylinder takes width depth sides
  
  lower 2
  ;lowers the turtle eg moves beneath it
  
  cutcone 2 3 7 20
  ;cutcone takes topwidth bottomwidth depth sides
  
  lo 7
  ;lo is shortcut for lower
  
  cylinder 4 2 20
  lo 2
  setfc pick [5 10]
  ;setfc is shortcut for setfillcolor, the color
  ;used for painting shapes
  
  cylinder 1 5 8
  lo 5
  cone 1 1 8
  raise 16
  ;raises the turtle, eg moves above it
  ;all done!
  
END

TO corkboard
  ;create a corkboard to push our pins into
  
  penup
  setpos [-205 -116]
  
  setfillcolor 8 setfillshade -4
  ;setfillshade takes a range of -15 to 15
  
  forward 5 lo 3 slideright 5
  voxeloid 400 222 4
  ;voxeloid takes width height depth
  ;this creates the surface of our corkboard
  
  slideleft 5 ra 3 back 5
  setfs 6
  ; setfs is shortcut for setfillshade
  
  voxeloid 10 232 10
  ;this and subsequent voxeloids
  ;create the frame
  
  right 90
  sl 10
  ;sl is shortcut for slideleft
  
  voxeloid 10 410 10
  fd 400
  ;fd is shortcut for forward
  
  left 90
  bk 10
  ;bk is shortcut for back
  
  voxeloid 10 232 10
  fd 222 lt 90 bk 10
  ;lt is shortcut for left
  
  voxeloid 10 410 10
  ;that's all folks!
  
END

TO stringart
  reset
  ;resets the workspace
  
  snappy:run pick [
    [pullout 10]
    [pullout 30 orbitdown 70]
    [orbitleft 20 orbitdown 45 rollright 10 pullout 60 rr 10 sl 25 lo 40]
    ;rr is shortcut for rollright
    [orbitright 20 orbitdown 45 rollleft 10 pullout 60 rl 10 sr 25 lo 40]
    ;rl is shortcut for rollleft
  ]
  ;pick a camera sequence and execute it as
  ;snappy, the camera turtle
  
  ;if we need snappy to do more sophisticated
  ;things, we can store procedures inside him
  
  ;logo is all about having the freedom to do
  ;things in multiple ways!
  
  setpenwidth 1 + random 5
  ;sets the 'width' of the pen, from 1 to 5
  
  randps
  randfs
  ;randps and randfs (aka randompenshade and randomfillshade)
  ;set a random shade between -12 and 12
  
  penup
  corkboard
  ;execute corkboard procedure
  
  repeat 20 + random 20  [
    ;do the following 20 + 0-19 times:
    
    randpc
    ;shortcut for randompencolor
    
    setpos {-190 + random 380 -100 + random 200}
    ;move to a random position
    
    pu
    ;pu is shortcut for penup
    
    pushturtle
    ;save the turtle state on the 'stack'
    ;you can have multiple states on the stack
    
    raise 10 + random 4 up -10 + random 20 rr -10 + random 20
    ;move into a semi-random position
    ;to give our tacks a more natural-looking
    ;placement
    
    ;moving in negative values moves the opposite
    ;direction, eg left -90 turns right 90 degrees
    
    tack
    ;execute tack procedure
    ;(create a tack)
    
    popturtle
    ;load the turtle state from the 'stack'
    ;this puts it back where and how it was when
    ;we pushed it
    
    pendown
    ;pd is shortcut for pendown
    ;this will cause us to create a line to the
    ;next random position
    
  ]
  ;repeat the above however many times
  ;specified by 20 + random 20
  
  hideturtle
  ;ta da!
  
  ;for extra credit, get some tacks, a corkboard, and some
  ;colored string and make some string art of your own!
  
  ;consider the angles the turtle would need to turn to travel
  ;along the line of string created between all the pins
  ;were the turtle to travel the string like driving
  ;down a road
  
END