Simple turtle graphics

From Rosetta Code
Revision as of 01:21, 31 July 2021 by GordonCharlton (talk | contribs) (New draft task, Simple turtle graphics)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Simple turtle graphics is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The first turtle graphic discussed in Mindstorms: Children, Computers, and Powerful Ideas by Seymour Papert is a simple drawing of a house. It is a square with a triangle on top for the roof.

For a slightly more advanced audience, a more practical introduction to turtle graphics might be to draw a bar chart.

See image here: https://i.imgur.com/B7YbTbZ.png

Task
  • Create a function (or subroutine) that uses turtle graphics to draw a house of a specified size as described above. Optionally make it lovely by adding details such as, for example, doors and windows.
  • Create a function (or subroutine) that takes a list (array, vector) of numbers and draws a bar chart from them, scaled to fit exactly in a square of a specified size. The square need not be drawn.
  • Both functions should return the turtle to the location it was at and facing in the same direction as it was immediately before the function was executed.

Quackery

<lang Quackery> [ $ "turtleduck.qky" loadfile ] now!

 [ tuck put put ]             is 2put      (   x x s -->     )
 [ dup take swap take ]       is 2take     (       s --> x x )
 [ dup dip [ 2take 2dup ]
   2put ]                     is 2share    (       s --> x x )
 [ dup release release ]      is 2release  (       s -->     )
 [ behead do
   rot witheach
     [ do 2over 2over
       v< if 2swap 
       2drop ] ]              is vmax      (       [ --> n/d )
 [ 2 times 
   [ 2dup walk
     -1 4 turn
     2over walk 
     -1 4 turn ] 
   2drop 2drop ]              is rectangle ( n/d n/d -->     ) 
 [ 2dup rectangle ]           is square    (     n/d -->     )
 [ 3 times 
    [ 2dup walk 
      1 3 turn ] 
   2drop ]                    is triangle  (    n/d  -->     )
 [ 1 2 turn 
   2dup square triangle 
   1 2 turn ]                 is house     (     n/d -->     )
 [ stack ]                    is bar.width (         --> s   )
 [ stack ]                    is bar.scale (         --> s   )
 [ temp 2put
   dup size n->v 
   temp 2share 2swap v/
   bar.width 2put
   dup vmax 
   temp 2share 2swap v/
   bar.scale 2put    
   witheach 
     [ do 
       bar.scale 2share v*
       bar.width 2share 
       rectangle
       bar.width 2share fly ]
   temp 2take -v fly
   bar.width 2release
   bar.scale 2release ]       is barchart  (   [ n/d -->     )
  turtle
  150 1 house
  10 1 fly
  ' [ [ 1 2 ] [ 1 3 ] [ 2 1 ] [ 13 10 ] [ 1 2 ] ] 200 1 barchart
  -10 1 fly</lang>