Extended Exercise: Moving Pictures, Again

In sections~#secmovecircle#29363>, #secmoveshape#29364>, and~#secmovefig#29365>, we studied the problem of moving pictures across a canvas. The problem had two parts: moving individual shapes and moving a picture, which is a list of shapes. For the first part, we need functions to draw, clear, and translate a shape. For the second part, we need functions that draw all shapes on a list, that clear all shapes on a list, and that translate all shapes on a list. Even the most cursory look at the functions shows many repetitions. The following exercises aim to eliminate these repetitions via manual abstraction and Scheme's built-in operations.
<#29368#>Exercise 21.4.1<#29368#> Abstract the functions <#65530#><#29370#>draw-circle<#29370#><#65530#> and <#65531#><#29371#>clear-circle<#29371#><#65531#> into a single function <#65532#><#29372#>process-circle<#29372#><#65532#>. Define <#65533#><#29373#>translate-circle<#29373#><#65533#> using <#65534#><#29374#>process-shape<#29374#><#65534#>. <#29375#>Hint:<#29375#> \ If a primitive function doesn't quite fit an abstraction, we have to define auxiliary functions. For now, use <#65535#><#29376#>define<#29376#><#65535#> to do so. Intermezzo~4 introduces a handy and important short-hand for that purpose.~ external Solution<#65536#><#65536#> <#29382#>Exercise 21.4.2<#29382#> Abstract the functions <#65537#><#29384#>draw-rectangle<#29384#><#65537#> and <#65538#><#29385#>clear-rectangle<#29385#><#65538#> into a single function <#65539#><#29386#>process-rectangle<#29386#><#65539#>. Define <#65540#><#29387#>translate-rectangle<#29387#><#65540#> using <#65541#><#29388#>process-shape<#29388#><#65541#>.~ external Solution<#65542#><#65542#> <#29394#>Exercise 21.4.3<#29394#> Abstract the functions <#65543#><#29396#>draw-shape<#29396#><#65543#> and <#65544#><#29397#>clear-shape<#29397#><#65544#> into a single function <#65545#><#29398#>process-shape<#29398#><#65545#>. Compare the function with the template <#65546#><#29399#>fun-for-shape<#29399#><#65546#>. Define <#65547#><#29400#>translate-shape<#29400#><#65547#> using <#65548#><#29401#>process-shape<#29401#><#65548#>.~ external Solution<#65549#><#65549#> <#29407#>Exercise 21.4.4<#29407#> Use Scheme's <#65550#><#29409#>map<#29409#><#65550#> and <#65551#><#29410#>andmap<#29410#><#65551#> to define <#65552#><#29411#>draw-losh<#29411#><#65552#>, <#65553#><#29412#>clear-losh<#29412#><#65553#>, and <#65554#><#29413#>translate-losh<#29413#><#65554#>.~ external Solution<#65555#><#65555#>
rawhtml38 <#29419#>Figure: NASA's manned lunar lander<#29419#>
<#29421#>Exercise 21.4.5<#29421#> Modify the functions of exercises~#exmovingfigabs#29423> and~#exmovinglistmap#29424> so that pictures move up and down on a canvas. Modify the functions so that a shape can also be a line with a start position, an end position, and a color. Create a lunar lander picture (see figure~#figlunarlander#29425>) using a list of rectangles, circles, and lines. Develop the program <#65556#><#29426#>lunar-lander<#29426#><#65556#>, which shows the drop of a lunar lander from a certain height. More specifically, the program creates a canvas and moves the lunar lander from the top to the bottom. Use the teachpack <#29427#>arrow.ss<#29427#> to give users control over how fast and when the lunar lander should move:
<#29432#>(start<#29432#> <#29433#>500<#29433#> <#29434#>100)<#29434#>
<#29435#>(draw<#29435#> <#29436#>LUNAR)<#29436#> 
<#29437#>(control-up-down<#29437#> <#29438#>LUNAR<#29438#> <#29439#>10<#29439#> <#29440#>lunar-lander)<#29440#> 
If time permits, modify the function so that a player can move the lander up, down, left or right. Use <#65557#><#29444#>controller<#29444#><#65557#> from <#29445#>arrow.ss<#29445#> to control the movements.~ external Solution<#65558#><#65558#>