Extended Exercise: Moving Pictures

In sections~#secmovecircle#12117> and~#secmoveshape#12118>, we studied how to move individual shapes. A picture, however, isn't just a single shape but a whole collection of them. Considering that we have to draw, translate, clear pictures, and that we may wish to change a picture or manage several pictures at the same time, it is best to collect all of the parts of a picture into a single piece of data. Because pictures may consist of a varying number of items, a list representation for pictures suggests itself naturally.
<#12121#>Exercise 10.3.1<#12121#> Provide a data definition that describes the class of lists of <#62511#><#12123#>shape<#12123#><#62511#>s. The class of <#62512#><#12124#>shape<#12124#><#62512#>s was defined in exercise~#exmovingfigdat#12125>. Create a sample list that represents the face of figure~#exmoving#12126> and name it <#62513#><#12127#>FACE<#12127#><#62513#>. Its basic dimensions are gathered in the following table:

#tabular12129#

The table assumes a canvas of size 300 by 100. Develop the template <#62514#><#12133#>fun-for-losh<#12133#><#62514#>, which outlines functions that consume a <#62515#><#12134#>list-of-shapes<#12134#><#62515#>.~ external Solution<#62516#><#62516#> <#12140#>Exercise 10.3.2<#12140#> Use the template <#62517#><#12142#>fun-for-losh<#12142#><#62517#> to develop the function <#62518#><#12143#>draw-losh<#12143#><#62518#>. It consumes a <#62519#><#12144#>list-of-shapes<#12144#><#62519#>, draws each item on the list, and returns <#62520#><#12145#>true<#12145#><#62520#>. Recall to use <#62521#><#12146#>(start<#12146#>\ <#12147#>n<#12147#>\ <#12148#>m)<#12148#><#62521#> to create the canvas before the function is used.~ external Solution<#62522#><#62522#> <#12154#>Exercise 10.3.3<#12154#> Use the template <#62523#><#12156#>fun-for-losh<#12156#><#62523#> to develop <#62524#><#12157#>translate-losh<#12157#><#62524#>. The function consumes a <#62525#><#12158#>list-of-shapes<#12158#><#62525#> and a number <#62526#><#12159#>delta<#12159#><#62526#>. The result is a list of shapes where each of them has been moved by <#62527#><#12160#>delta<#12160#><#62527#> pixels in the x-direction.~ external Solution<#62528#><#62528#> <#12166#>Exercise 10.3.4<#12166#> Use the template <#62529#><#12168#>fun-for-losh<#12168#><#62529#> to develop <#62530#><#12169#>clear-losh<#12169#><#62530#>. The function consumes a <#62531#><#12170#>list-of-shapes<#12170#><#62531#>, erases each item on the list from the canvas, and returns <#62532#><#12171#>true<#12171#><#62532#>.~ external Solution<#62533#><#62533#> <#12177#>Exercise 10.3.5<#12177#> Develop the function <#62534#><#12180#>move-picture<#12180#><#62534#>. It consumes a number (<#62535#><#12181#>delta<#12181#><#62535#>) and a <#62536#><#12182#>picture<#12182#><#62536#> and translates it by <#62537#><#12183#>delta<#12183#><#62537#> pixels on a canvas. The function should wait for one second between drawing and clearing the new picture. The result of the function is the translated picture. Test the function with expressions like these:

<#12188#>(start<#12188#> <#12189#>500<#12189#> <#12190#>100)<#12190#>
<#12191#>(d<#12191#><#12192#>raw-losh<#12192#> 
  <#12193#>(m<#12193#><#12194#>ove-picture<#12194#> <#12195#>-5<#12195#> 
    <#12196#>(m<#12196#><#12197#>ove-picture<#12197#> <#12198#>23<#12198#> 
      <#12199#>(move-picture<#12199#> <#12200#>10<#12200#> <#12201#>FACE))))<#12201#> 
<#12202#>(stop)<#12202#> 
This moves <#62538#><#12206#>FACE<#12206#><#62538#> (see exercise~#exmovinglistdat#12207>) by <#62539#><#12208#>10<#12208#><#62539#>, <#62540#><#12209#>23<#12209#><#62540#>, and <#62541#><#12210#>-5<#12210#><#62541#> pixels in the x-direction.~ external Solution<#62542#><#62542#>
When the function is fully tested, use the teachpack <#12218#>arrow.ss<#12218#> and evaluate the expression:
<#12223#>(start<#12223#> <#12224#>500<#12224#> <#12225#>100)<#12225#>
<#12226#>(control-left-right<#12226#> <#12227#>FACE<#12227#> <#12228#>100<#12228#> <#12229#>move<#12229#> <#12230#>draw-losh)<#12230#> 
The last one creates a graphical user interface that permits users to move the shape <#62543#><#12234#>FACE<#12234#><#62543#> by clicking on arrows. The shape then moves in increments of <#62544#><#12235#>100<#12235#><#62544#> (right) and <#62545#><#12236#>-100<#12236#><#62545#> (left) pixels. The teachpack also provides arrow controls for other directions. Use them to develop other moving pictures.