Note: this wiki is now retired and will no longer be updated!

The static final versions of the pages are left as a convenience for readers. Note that meta-pages such as "discussion," "history," etc., will not work.

SICP exercise 2.50

From Drewiki
Jump to: navigation, search
Comment The solution for this exercise requires the use of the PLT Scheme DrScheme graphical environment. Ensure the DrScheme language is set to Module before running the program.
Comment The picture language implemented by the PLT Scheme SICP picture package is slightly different than the picture language described in the text. The PLT package lacks the wave and rogers pictures; the solution provided here will use the diagonal-shading and einstein pictures provided by the PLT package instead. Also, painters in the PLT picture package don't know how to draw themselves. In order to draw a painter in the DrScheme graphical environment, use the paint procedure: it takes a single argument, the painter to draw, e.g.,

(paint einstein)

The (require ...) expression at the beginning of the program automatically loads the PLT picture package from the DrScheme web site. It may not work if you're attempting to run the program from behind a proxy or outbound firewall.

For more information on the PLT SICP picture package, see here.

Problem

Define the transformation flip-horiz, which flips painters horizontally, and transformations that rotate painters counterclockwise by 180 degrees and 270 degrees.

Solution

Comment The PLT DrScheme picture language provides its own implementations of flip-horiz, rotate180 and rotate270. To avoid conflicts with those versions, our solution will use the names my-flip-horiz, my-rotate180 and my-rotate270.

Additionally, the PLT DrScheme version of transform-painter takes only 3 arguments -- the coordinates of the transformed frame -- and returns a procedure which takes a single argument, the painter to be transformed. This definition is slightly different than the definition given in the text, in which transform-painter takes the painter as an additional argument. We'll use the PLT version in our solution, so the general form and use of transformations given here will look slightly different than those in the text.

#lang scheme
(require (planet "sicp.ss" ("soegaard" "sicp.plt" 2 1)))
 
(define (my-flip-horiz painter)
  ((transform-painter (make-vect 1.0 0.0)
                      (make-vect 0.0 0.0)
                      (make-vect 1.0 1.0))
   painter))

Test:

(paint einstein)

Ex2.50-einstein.png

(paint (my-flip-horiz einstein))

Ex2.50-einstein-flip-horiz.png

(define (my-rotate180 painter)
  ((transform-painter (make-vect 1.0 1.0)
                      (make-vect 0.0 1.0)
                      (make-vect 1.0 0.0))
   painter))

Test:

(paint (my-rotate180 einstein))

Ex2.50-rotate180-einstein.png

(define (my-rotate270 painter)
  ((transform-painter (make-vect 0.0 1.0)
                      (make-vect 0.0 0.0)
                      (make-vect 1.0 1.0))
   painter))

Test:

(paint (my-rotate270 einstein))

Ex2.50-rotate270-einstein.png

Personal tools