2.51 Exercise 2.51
When defining below using calls to transform-painter, I have chosen to diverge from the style used by the given definition of beside. I believe that beside makes incomplete use of definition: All vectors are defined in terms of endpoints except for split-point, including a vector that is the sum of split-point and another vector.
As I usually do, I opted for more definitions and abstraction in my implementation. I’ve given all of the vectors names, and defined split-point (now named mid-left instead of a generic name) using scale-vect for fun. Also, in line with my definition stub seen earlier, I have named the painter arguments bottom and top instead of using the nonspecific names painter1 and painter2.
I believe this procedure is easier to read than the beside procedure it is supposed to be based on.
(define (below painter-bottom painter-top) (let ((bottom-left (make-vect 0.0 0.0)) (bottom-right (make-vect 1.0 0.0)) (top-left (make-vect 0.0 1.0)) (mid-left (scale-vect 0.5 top-left))) (let ((paint-bottom (transform-painter painter-bottom bottom-left bottom-right mid-left)) (paint-top (transform-painter painter-top mid-left (add-vect mid-left bottom-right) top-left))) (lambda (frame) (paint-bottom frame) (paint-top frame)))))
To define below in terms of beside, we have to rotate the whole painter resulting from beside and also rotate the painter given to beside.
We know that the first argument of beside is painted on the left and the second on the right. We can imagine then rotating the image by 90 degrees counterclockwise such that the first painter given to beside ends up on the bottom. We can use rotate90 for this.
Having rotated the whole painter counterclockwise by 90 degrees, we need to rotate the painters given to beside in such a way that they are facing upright at the end. This means turning them 90 degrees clockwise (or 270 counterclockwise), which can be done with rotate270.
The whole procedure is as follows:
(define (below bottom top) (rotate90 (beside (rotate270 bottom) (rotate270 top))))